#include <G4VUIshell.hh>
Inheritance diagram for G4VUIshell:
Public Member Functions | |
G4VUIshell (const G4String &prompt="> ") | |
virtual | ~G4VUIshell () |
void | SetNColumn (G4int ncol) |
void | SetPrompt (const G4String &prompt) |
void | SetCurrentDirectory (const G4String &ccd) |
virtual void | SetLsColor (TermColorIndex, TermColorIndex) |
virtual void | ShowCurrentDirectory () const |
virtual void | ListCommand (const G4String &input, const G4String &candidate="") const |
virtual G4String | GetCommandLineString (const char *msg=0)=0 |
virtual void | ResetTerminal () |
Protected Member Functions | |
virtual void | MakePrompt (const char *msg=0) |
G4UIcommandTree * | GetCommandTree (const G4String &dir) const |
G4String | GetAbsCommandDirPath (const G4String &apath) const |
G4String | GetCommandPathTail (const G4String &apath) const |
Protected Attributes | |
G4String | promptSetting |
G4String | promptString |
G4int | nColumn |
G4bool | lsColorFlag |
TermColorIndex | directoryColor |
TermColorIndex | commandColor |
G4String | currentCommandDir |
Definition at line 59 of file G4VUIshell.hh.
G4VUIshell::G4VUIshell | ( | const G4String & | prompt = "> " |
) |
Definition at line 46 of file G4VUIshell.cc.
00047 : promptSetting(prompt), promptString(""), nColumn(80), 00048 lsColorFlag(FALSE), directoryColor(BLACK), commandColor(BLACK), 00049 currentCommandDir("/") 00051 { 00052 }
G4VUIshell::~G4VUIshell | ( | ) | [virtual] |
Definition at line 147 of file G4VUIshell.cc.
References G4String::index(), G4String::last(), and G4String::remove().
Referenced by G4UItcsh::CompleteCommand(), and G4UItcsh::ListMatchedCommand().
00149 { 00150 if(apath.empty()) return apath; // null string 00151 00152 // if "apath" does not start with "/", 00153 // then it is treared as relative path 00154 G4String bpath= apath; 00155 if(apath[(size_t)0] != '/') bpath= currentCommandDir + apath; 00156 00157 // parsing... 00158 G4String absPath= "/"; 00159 for(G4int indx=1; indx<=G4int(bpath.length())-1; ) { 00160 G4int jslash= bpath.index("/", indx); // search index begin with "/" 00161 if(indx == jslash) { // skip first '///' 00162 indx++; 00163 continue; 00164 } 00165 if(jslash != G4int(G4String::npos)) { 00166 if(bpath.substr(indx,jslash-indx) == ".."){ // directory up 00167 if(absPath == "/") { 00168 indx = jslash+1; 00169 continue; 00170 } 00171 if(absPath.length() >= 2) { 00172 absPath.remove(absPath.length()-1); // remove last "/" 00173 G4int jpre= absPath.last('/'); 00174 if(jpre != G4int(G4String::npos)) absPath.remove(jpre+1); 00175 } 00176 } else if(bpath.substr(indx,jslash-indx) == "."){ // nothing to do 00177 } else { // add 00178 if( !(jslash==indx && bpath(indx)=='/') ) // truncate "////" 00179 absPath+= bpath(indx, jslash-indx+1); 00180 // better to be check directory existence. (it costs!) 00181 } 00182 indx= jslash+1; 00183 } else { // directory ONLY (ignore non-"/" terminated string) 00184 break; 00185 } 00186 } 00187 00188 return absPath; 00189 }
virtual G4String G4VUIshell::GetCommandLineString | ( | const char * | msg = 0 |
) | [pure virtual] |
Definition at line 193 of file G4VUIshell.cc.
Referenced by G4UItcsh::CompleteCommand().
00195 { // xxx/xxx/zzz -> zzz, trancate /// -> / 00196 if(apath.empty()) return apath; 00197 00198 G4int lstr= apath.length(); 00199 00200 // for trancating "/" 00201 G4bool Qsla= FALSE; 00202 if(apath[(size_t)(lstr-1)]=='/') Qsla= TRUE; 00203 00204 // searching last '/' from tail 00205 G4int indx= -1; 00206 for(G4int i=lstr-1; i>=0; i--) { 00207 if(Qsla && apath[(size_t)i]!='/') Qsla= FALSE; // break "/" flag!! 00208 if(apath[(size_t)i]=='/' && !Qsla) { 00209 indx= i; 00210 break; 00211 } 00212 } 00213 00214 if(indx==-1) return apath; // not found 00215 00216 if(indx==0 && lstr==1) { // "/" 00217 G4String nullStr; 00218 return nullStr; 00219 } else { 00220 //G4String newPath= apath(indx+1,lstr-indx-1); 00221 G4String newPath= apath; 00222 newPath= newPath(indx+1,lstr-indx-1); 00223 return newPath; 00224 } 00225 }
G4UIcommandTree * G4VUIshell::GetCommandTree | ( | const G4String & | dir | ) | const [protected] |
Definition at line 118 of file G4VUIshell.cc.
References G4String::both, G4UImanager::GetUIpointer(), and G4String::strip().
Referenced by G4UItcsh::CompleteCommand().
00120 { 00121 G4UImanager* UI= G4UImanager::GetUIpointer(); 00122 00123 G4UIcommandTree* cmdTree= UI-> GetTree(); // root tree 00124 00125 G4String absPath= input; // G4String::strip() CONST !! 00126 absPath= GetAbsCommandDirPath(absPath.strip(G4String::both)); 00127 00128 // parsing absolute path ... 00129 if(absPath.length()==0) return NULL; 00130 if(absPath[absPath.length()-1] != '/') return NULL; // error?? 00131 if(absPath=="/") return cmdTree; 00132 00133 for(G4int indx=1; indx<G4int(absPath.length())-1; ) { 00134 G4int jslash= absPath.index("/", indx); // search index begin with "/" 00135 if(jslash != G4int(G4String::npos)) { 00136 if(cmdTree != NULL) 00137 cmdTree= cmdTree-> GetTree(G4String(absPath(0,jslash+1))); 00138 } 00139 indx= jslash+1; 00140 } 00141 00142 if(cmdTree == NULL) return NULL; 00143 else return cmdTree; 00144 }
void G4VUIshell::ListCommand | ( | const G4String & | input, | |
const G4String & | candidate = "" | |||
) | const [virtual] |
Definition at line 231 of file G4VUIshell.cc.
References G4String::both, FALSE, G4cout, G4endl, G4String::index(), G4UIArrayString::Show(), G4String::strip(), and TRUE.
Referenced by G4UItcsh::ListMatchedCommand().
00234 { 00235 // specified directpry 00236 G4String input= dir; // ... 00237 input= input.strip(G4String::both); 00238 00239 // command tree of "user specified directory" 00240 G4String vpath= currentCommandDir; 00241 G4String vcmd; 00242 00243 G4int len= input.length(); 00244 if(! input.empty()) { 00245 G4int indx= -1; 00246 for(G4int i=len-1; i>=0; i--) { // search last '/' 00247 if(input[(size_t)i]=='/') { 00248 indx= i; 00249 break; 00250 } 00251 } 00252 // get abs. path 00253 if(indx != -1) vpath= GetAbsCommandDirPath(input(0,indx+1)); 00254 if(!(indx==0 && len==1)) vcmd= input(indx+1,len-indx-1); // care for "/" 00255 } 00256 00257 // check "vcmd" is directory? 00258 G4String inputpath= vpath+vcmd; 00259 if(! vcmd.empty()){ 00260 G4String tmpstr= inputpath + "/"; 00261 if(GetCommandTree(tmpstr) != NULL) { 00262 vpath= tmpstr; 00263 vcmd= ""; 00264 } 00265 } 00266 00267 // check "vpath" directory exists? 00268 G4UIcommandTree* atree= GetCommandTree(vpath); 00269 if(atree == NULL) { 00270 G4cout << "<" << input << ">: No such directory" << G4endl; 00271 return; 00272 } 00273 00274 // list matched directories/commands 00275 G4String stream; 00276 G4bool isMatch= FALSE; 00277 00278 G4int Ndir= atree-> GetTreeEntry(); 00279 G4int Ncmd= atree-> GetCommandEntry(); 00280 if(Ndir==0 && Ncmd==0) return; // no contents 00281 00282 // directory ... 00283 for(G4int idir=1; idir<=Ndir; idir++) { 00284 if(idir==1 && lsColorFlag) stream+= TermColorString[directoryColor]; 00285 G4String fpdir= atree-> GetTree(idir)-> GetPathName(); 00286 // matching test 00287 if(candidate.empty()) { // list all 00288 if(vcmd=="" || fpdir==inputpath) { 00289 stream+= GetCommandPathTail(fpdir); stream+= " "; 00290 isMatch= TRUE; 00291 } 00292 } else { // list only matched with candidate 00293 if( fpdir.index(candidate, 0) == 0) { 00294 stream+= GetCommandPathTail(fpdir); stream+= " "; 00295 } 00296 } 00297 } 00298 00299 // command ... 00300 for(G4int icmd=1; icmd<=Ncmd; icmd++){ 00301 if(icmd==1 && lsColorFlag) stream+= TermColorString[commandColor]; 00302 G4String fpcmd= atree-> GetPathName() + 00303 atree-> GetCommand(icmd) -> GetCommandName(); 00304 // matching test 00305 if(candidate.empty()) { // list all 00306 if(vcmd=="" || fpcmd==inputpath) { 00307 stream+= GetCommandPathTail(fpcmd); stream+= "* "; 00308 isMatch= TRUE; 00309 } 00310 } else { // list only matched with candidate 00311 if( fpcmd.index(candidate, 0) == 0) { 00312 stream+= GetCommandPathTail(fpcmd); stream+= "* "; 00313 } 00314 } 00315 } 00316 00317 // waring : not matched 00318 if(!isMatch && candidate.empty()) 00319 G4cout << "<" << input 00320 << ">: No such directory or command" << std::flush; 00321 00322 // display 00323 G4UIArrayString arrayString(stream); 00324 arrayString.Show(nColumn); 00325 }
void G4VUIshell::MakePrompt | ( | const char * | msg = 0 |
) | [protected, virtual] |
Reimplemented in G4UItcsh.
Definition at line 61 of file G4VUIshell.cc.
References G4String::append(), currentCommandDir, G4StateManager::GetCurrentState(), G4StateManager::GetStateManager(), promptSetting, and promptString.
Referenced by G4UIcsh::GetCommandLineString().
00063 { 00064 if(promptSetting.length()<=1) { 00065 promptString= promptSetting; 00066 return; 00067 } 00068 00069 promptString=""; 00070 G4int i; 00071 for(i=0; i<G4int(promptSetting.length())-1; i++){ 00072 if(promptSetting[(size_t)i]=='%'){ 00073 switch (promptSetting[(size_t)(i+1)]) { 00074 case 's': // current application status 00075 { 00076 G4String stateStr; 00077 if(msg) 00078 { stateStr = msg; } 00079 else 00080 { 00081 G4StateManager* statM= G4StateManager::GetStateManager(); 00082 stateStr= statM-> GetStateString(statM->GetCurrentState()); 00083 } 00084 promptString.append(stateStr); 00085 i++; 00086 } 00087 break; 00088 case '/': // current working directory 00089 promptString.append(currentCommandDir); 00090 i++; 00091 break; 00092 default: 00093 promptString.append(G4String(promptSetting[(size_t)i])); 00094 break; 00095 } 00096 } else { 00097 promptString.append(G4String(promptSetting[(size_t)i])); 00098 } 00099 } 00100 00101 // append last chaacter 00102 if(i == G4int(promptSetting.length())-1) 00103 promptString.append(G4String(promptSetting[(size_t)i])); 00104 }
void G4VUIshell::ResetTerminal | ( | ) | [virtual] |
void G4VUIshell::SetCurrentDirectory | ( | const G4String & | ccd | ) | [inline] |
Definition at line 115 of file G4VUIshell.hh.
References currentCommandDir.
00116 { 00117 currentCommandDir= dir; 00118 }
void G4VUIshell::SetLsColor | ( | TermColorIndex | , | |
TermColorIndex | ||||
) | [inline, virtual] |
void G4VUIshell::SetNColumn | ( | G4int | ncol | ) | [inline] |
Definition at line 105 of file G4VUIshell.hh.
References nColumn.
00106 { 00107 nColumn= ncol; 00108 }
void G4VUIshell::SetPrompt | ( | const G4String & | prompt | ) | [inline] |
Definition at line 110 of file G4VUIshell.hh.
References promptSetting.
00111 { 00112 promptSetting= prompt; 00113 }
void G4VUIshell::ShowCurrentDirectory | ( | ) | const [inline, virtual] |
Definition at line 124 of file G4VUIshell.hh.
References currentCommandDir, G4cout, and G4endl.
00125 { 00126 G4cout << currentCommandDir << G4endl; 00127 }
TermColorIndex G4VUIshell::commandColor [protected] |
G4String G4VUIshell::currentCommandDir [protected] |
Definition at line 73 of file G4VUIshell.hh.
Referenced by G4UItcsh::CompleteCommand(), G4UItcsh::ListMatchedCommand(), MakePrompt(), G4UItcsh::MakePrompt(), SetCurrentDirectory(), and ShowCurrentDirectory().
TermColorIndex G4VUIshell::directoryColor [protected] |
G4bool G4VUIshell::lsColorFlag [protected] |
G4int G4VUIshell::nColumn [protected] |
G4String G4VUIshell::promptSetting [protected] |
Definition at line 61 of file G4VUIshell.hh.
Referenced by MakePrompt(), G4UItcsh::MakePrompt(), and SetPrompt().
G4String G4VUIshell::promptString [protected] |
Definition at line 62 of file G4VUIshell.hh.
Referenced by G4UItcsh::ClearScreen(), G4UItcsh::GetCommandLineString(), G4UIcsh::GetCommandLineString(), G4UItcsh::ListMatchedCommand(), MakePrompt(), and G4UItcsh::MakePrompt().