Geant4  10.00.p03
G4UItcsh.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4UItcsh.hh 66892 2013-01-17 10:57:59Z gunter $
28 //
29 
30 #ifndef G4UItcsh_h
31 #define G4UItcsh_h 1
32 
33 #ifndef WIN32
34 
35 #include <termios.h>
36 #include <vector>
37 #include "G4VUIshell.hh"
38 #include "G4UIcommand.hh"
39 #include "G4UIcommandTree.hh"
40 
41 // ====================================================================
42 // Description:
43 // This class gives tcsh-like shell.
44 //
45 // If your terminal supports color code, colored strings are available
46 // in ListCommand(). For activating color support,
47 // e.g.
48 // tcsh-> SetLsColor(GREEN, CYAN); // (dir, command) color
49 //
50 // [key binding]
51 // ^A ... move cursor to the top
52 // ^B ... backward cursor ([LEFT])
53 // ^D ... delete/exit/show matched list
54 // ^E ... move cursor to the end
55 // ^F ... forward cursor ([RIGHT])
56 // ^K ... clear after the cursor
57 // ^L ... clear screen (not implemented)
58 // ^N ... next command ([DOWN])
59 // ^P ... previous command ([UP])
60 // TAB... command completion
61 // DEL... backspace
62 // BS ... backspace
63 //
64 // [prompt string substitution]
65 // %s ... current application status
66 // %/ ... current working directory
67 // %h ... history# (different from G4 history#)
68 //
69 // ====================================================================
70 
71 class G4UItcsh : public G4VUIshell {
72 protected:
73  virtual void MakePrompt(const char* msg=0);
74 
75  G4String commandLine; // command line string;
76  G4int cursorPosition; // cursor position
77  G4String commandLineBuf; // temp. command line;
78  G4bool IsCursorLast() const;
79  // Is cursor position at the last of command line ?
80 
81  void InitializeCommandLine();
83  void InsertCharacter(char cc); // insert character
84  void BackspaceCharacter(); // backspace character
85  void DeleteCharacter(); // delete character
86  void ClearLine(); // clear command line
87  void ClearAfterCursor(); // clear after the cursor
88  void ClearScreen(); // clear screen
89 
90  void ForwardCursor(); // move cursor forward
91  void BackwardCursor(); // move cursor backward
92  void MoveCursorTop(); // move cursor to the top
93  void MoveCursorEnd(); // move cursor to the end
94 
95  void NextCommand(); // next command
96  void PreviousCommand(); // previous command
97 
98  void ListMatchedCommand(); // list matched commands
99  void CompleteCommand(); // complete command
100 
101  // utilities...
103  const G4String& str2) const;
104 
105  // history functionality (history# is managed in itself)
106  std::vector<G4String> commandHistory;
107  G4int maxHistory; // max# of histories stored
109  G4int relativeHistoryIndex; // local index relative to current history#
110 
111  void StoreHistory(G4String aCommand);
112  G4String RestoreHistory(G4int index); // index is global history#
113 
114 
115  // (re)set termios
116  termios tios; // terminal mode (prestatus)
117  G4String clearString; // "clear code (^L)"
118  void SetTermToInputMode();
119  void RestoreTerm();
120 
121 public:
122  G4UItcsh(const G4String& prompt="%s> ", G4int maxhist=100);
123  ~G4UItcsh();
124 
125  void SetLsColor(TermColorIndex dirColor, TermColorIndex cmdColor);
126  virtual G4String GetCommandLineString(const char* msg=0);
127 
128  virtual void ResetTerminal();
129 };
130 
131 // ====================================================================
132 // inline functions
133 // ====================================================================
135 {
136  if(cursorPosition == G4int(commandLine.length()+1)) return TRUE;
137  else return FALSE;
138 }
139 
140 inline void G4UItcsh::SetLsColor(TermColorIndex dirColor,
141  TermColorIndex cmdColor)
142 {
143  lsColorFlag= TRUE;
144  directoryColor= dirColor;
145  commandColor= cmdColor;
146 }
147 
148 #endif
149 #endif
150 
virtual G4String GetCommandLineString(const char *msg=0)
Definition: G4UItcsh.cc:648
G4bool IsCursorLast() const
Definition: G4UItcsh.hh:134
TermColorIndex directoryColor
Definition: G4VUIshell.hh:69
void ClearLine()
Definition: G4UItcsh.cc:269
G4int relativeHistoryIndex
Definition: G4UItcsh.hh:109
void InsertCharacter(char cc)
Definition: G4UItcsh.cc:196
void BackspaceCharacter()
Definition: G4UItcsh.cc:220
int G4int
Definition: G4Types.hh:78
void BackwardCursor()
Definition: G4UItcsh.cc:326
G4String commandLineBuf
Definition: G4UItcsh.hh:77
void SetTermToInputMode()
Definition: G4UItcsh.cc:739
std::vector< G4String > commandHistory
Definition: G4UItcsh.hh:106
G4String GetFirstMatchedString(const G4String &str1, const G4String &str2) const
Definition: G4UItcsh.cc:688
G4String RestoreHistory(G4int index)
Definition: G4UItcsh.cc:724
~G4UItcsh()
Definition: G4UItcsh.cc:99
virtual void MakePrompt(const char *msg=0)
Definition: G4UItcsh.cc:123
TermColorIndex commandColor
Definition: G4VUIshell.hh:70
virtual void ResetTerminal()
Definition: G4UItcsh.cc:177
bool G4bool
Definition: G4Types.hh:79
G4bool lsColorFlag
Definition: G4VUIshell.hh:68
void ClearScreen()
Definition: G4UItcsh.cc:302
void ForwardCursor()
Definition: G4UItcsh.cc:316
#define FALSE
Definition: globals.hh:52
void CompleteCommand()
Definition: G4UItcsh.cc:439
void DeleteCharacter()
Definition: G4UItcsh.cc:248
#define TRUE
Definition: globals.hh:55
G4UItcsh(const G4String &prompt="%s> ", G4int maxhist=100)
Definition: G4UItcsh.cc:66
G4String commandLine
Definition: G4UItcsh.hh:75
void ListMatchedCommand()
Definition: G4UItcsh.cc:398
G4String ReadLine()
Definition: G4UItcsh.cc:548
G4int currentHistoryNo
Definition: G4UItcsh.hh:108
termios tios
Definition: G4UItcsh.hh:116
void MoveCursorTop()
Definition: G4UItcsh.cc:336
void RestoreTerm()
Definition: G4UItcsh.cc:755
void PreviousCommand()
Definition: G4UItcsh.cc:358
TermColorIndex
Definition: G4VUIshell.hh:54
void NextCommand()
Definition: G4UItcsh.cc:378
void SetLsColor(TermColorIndex dirColor, TermColorIndex cmdColor)
Definition: G4UItcsh.hh:140
void InitializeCommandLine()
Definition: G4UItcsh.cc:188
G4String clearString
Definition: G4UItcsh.hh:117
void MoveCursorEnd()
Definition: G4UItcsh.cc:347
void StoreHistory(G4String aCommand)
Definition: G4UItcsh.cc:713
G4int cursorPosition
Definition: G4UItcsh.hh:76
G4int maxHistory
Definition: G4UItcsh.hh:107
void ClearAfterCursor()
Definition: G4UItcsh.cc:285