source: trunk/FACT++/src/ReadlineColor.cc@ 11520

Last change on this file since 11520 was 11326, checked in by tbretz, 13 years ago
Added .! command to be able to execute shell commands.
File size: 7.9 KB
Line 
1// **************************************************************************
2/** @namespace ReadlineColor
3
4@brief A fewer helper functions to apply color attributes and redirect the output
5
6 */
7// **************************************************************************
8#include "ReadlineColor.h"
9
10#include <boost/filesystem.hpp>
11
12#include "Time.h"
13#include "Readline.h"
14#include "WindowLog.h"
15
16using namespace std;
17
18// --------------------------------------------------------------------------
19//
20//! @returns
21//! always true
22//
23bool ReadlineColor::PrintBootMsg(ostream &out, const string &name, bool interactive)
24{
25 const string n = boost::filesystem::path(name).filename();
26
27 out << kBlue << kBold << kUnderline << "\n Master Control Program (compiled "__DATE__" " __TIME__ << ") " << endl;
28 out << kBlue <<
29 "\n"
30 " ENCOM MX 16-923 USER # 0" << int(Time().Mjd()) << Time::fmt(" %H:%M:%S") << Time() << Time::reset << " INFORMATION\n"
31 "\n"
32 " TELESCOPE CONTROL PROGRAM: " << n << "\n"
33 " ANNEXED BY FACT COLLABORATION\n"
34 " ORIGINAL PROGRAM WRITTEN BY T.BRETZ\n"
35 " THIS INFOMATION " << kUnderline << "PRIORITY ONE"
36 << endl;
37 out << kBlue << " END OF LINE\n" << endl;
38
39 if (!interactive)
40 return true;
41
42 out << "Enter 'h' for help." << endl;
43 out << endl;
44
45 return true;
46}
47
48// --------------------------------------------------------------------------
49//
50//! @returns
51//! always true
52//
53bool ReadlineColor::PrintCommands(ostream &out)
54{
55 out << endl;
56 out << " " << kUnderline << " Commands:" << endl;
57 out << " No application specific commands defined." << endl;
58 out << endl;
59
60 return true;
61}
62
63// --------------------------------------------------------------------------
64//
65//! Displays the available ncurses attributes, like color.
66//!
67//! @returns
68//! always true
69//
70bool ReadlineColor::PrintAttributes(ostream &out)
71{
72 out << endl;
73 out << " Attributes:" << endl;
74 out << " " << kReset << "kReset" << endl;
75 out << " " << kNormal << "kNormal" << endl;
76 out << " " << kHighlight << "kHighlight" << endl;
77 out << " " << kReverse << "kReverse" << endl;
78 out << " " << kUnderline << "kUnderline" << endl;
79 out << " " << kBlink << "kBlink" << endl;
80 out << " " << kDim << "kDim" << endl;
81 out << " " << kBold << "kBold" << endl;
82 out << " " << kProtect << "kProtect" << endl;
83 out << " " << kInvisible << "kInvisible" << endl;
84 out << " " << kAltCharset << "kAltCharset" << kReset << " (kAltCharset)" << endl;
85 out << endl;
86 out << " Colors:" << endl;
87 out << " " << kDefault << "kDefault " << kBold << "+ kBold" << endl;
88 out << " " << kRed << "kRed " << kBold << "+ kBold" << endl;
89 out << " " << kGreen << "kGreen " << kBold << "+ kBold" << endl;
90 out << " " << kYellow << "kYellow " << kBold << "+ kBold" << endl;
91 out << " " << kBlue << "kBlue " << kBold << "+ kBold" << endl;
92 out << " " << kMagenta << "kMagenta " << kBold << "+ kBold" << endl;
93 out << " " << kCyan << "kCyan " << kBold << "+ kBold" << endl;
94 out << " " << kWhite << "kWhite " << kBold << "+ kBold" << endl;
95 out << " " << endl;
96
97 return true;
98}
99
100// --------------------------------------------------------------------------
101//
102//! Displays the keybindings available due to the Shell class
103//!
104//! @returns
105//! always true
106//!
107//! @todo
108//! Update output
109//
110bool ReadlineColor::PrintKeyBindings(ostream &out)
111{
112 out << endl;
113 out << " " << kUnderline << "Key bindings:" << endl << endl;;
114 out << " Default key-bindings are identical with your bash." << endl;
115 out << endl;
116 out << kBold << " Page-up " << kReset << "Search backward in history" << endl;
117 out << kBold << " Page-dn " << kReset << "Search forward in history" << endl;
118 out << kBold << " Ctrl-left " << kReset << "One word backward" << endl;
119 out << kBold << " Ctrl-right " << kReset << "One word forward" << endl;
120 out << kBold << " Ctrl-d " << kReset << "Quit" << endl;
121 out << kBold << " Ctrl-y " << kReset << "Delete line" << endl;
122 out << kBold << " Alt-end/Ctrl-k " << kReset << "Delete until the end of the line" << endl;
123 out << endl;
124
125 return true;
126}
127
128// --------------------------------------------------------------------------
129//
130//! Print a general help text which also includes the commands pre-defined
131//! by the Shell class.
132//!
133//! @returns
134//! always true
135//!
136//! @todo
137//! Get it up-to-date
138//
139bool ReadlineColor::PrintGeneralHelp(ostream &out, const string &name)
140{
141 out << endl;
142 out << " " << kUnderline << "General help:" << endl << endl;
143 out << " The command history is automatically loaded and saves to" << endl;
144 out << " and from " << name << endl;
145 out << endl;
146 out << kBold << " h,help " << kReset << "Print this help message" << endl;
147 out << kBold << " clear " << kReset << "Clear history buffer" << endl;
148 out << kBold << " lh,history " << kReset << "Dump the history buffer to the screen" << endl;
149 out << kBold << " v,variable " << kReset << "Dump readline variables" << endl;
150 out << kBold << " f,function " << kReset << "Dump readline functions" << endl;
151 out << kBold << " m,funmap " << kReset << "Dump readline funmap" << endl;
152 out << kBold << " c,command " << kReset << "Dump available commands" << endl;
153 out << kBold << " k,keylist " << kReset << "Dump key bindings" << endl;
154 out << kBold << " a,attrs " << kReset << "Dump available stream attributes" << endl;
155 out << kBold << " .! command " << kReset << "Execute a shell command";
156 out << kBold << " .w n " << kReset << "Sleep n milliseconds";
157 out << kBold << " .x filename " << kReset << "Execute a script of commands" << endl;
158 out << kBold << " .q,quit " << kReset << "Quit" << endl;
159 out << endl;
160
161 return true;
162}
163
164// --------------------------------------------------------------------------
165//
166//! Execute a shell command through a pipe. Write stdout to rl_outstream
167//!
168//! @param cmd
169//! Command to be executed
170//!
171//! @returns
172//! always true
173//
174bool ReadlineColor::ExecuteShellCommand(ostream &out, const string &cmd)
175{
176 FILE *pipe = popen(cmd.c_str(), "r");
177 if (!pipe)
178 {
179 out << kRed << "ERROR - Could not create pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
180 return true;
181 }
182
183 while (1)
184 {
185 char buf[1024];
186
187 const size_t sz = fread(buf, 1, 1024, pipe);
188 out.write(buf, sz);
189
190 if (feof(pipe) || ferror(pipe))
191 break;
192 }
193
194 out << endl;
195
196 if (ferror(pipe))
197 out << kRed << "ERROR - Reading from pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
198
199 pclose(pipe);
200
201 return true;
202}
203
204
205bool ReadlineColor::Process(ostream &out, const string &str)
206{
207 // ----------- Readline -----------
208
209 if (str.substr(0, 2)==".!")
210 return ExecuteShellCommand(out, str.substr(2));
211
212 if (str=="lh" || str=="history")
213 {
214 out << endl << kBold << "History:" << endl;
215 return Readline::RedirectionWrapper(out, Readline::DumpHistory);
216 }
217
218 if (str=="v" || str=="variable")
219 {
220 out << endl << kBold << "Variables:" << endl;
221 return Readline::RedirectionWrapper(out, Readline::DumpVariables);
222 }
223
224 if (str=="f" || str=="function")
225 {
226 out << endl << kBold << "Functions:" << endl;
227 return Readline::RedirectionWrapper(out, Readline::DumpFunctions);
228 }
229
230 if (str=="m" || str=="funmap")
231 {
232 out << endl << kBold << "Funmap:" << endl;
233 return Readline::RedirectionWrapper(out, Readline::DumpFunmap);
234 }
235
236 // ------------ ReadlineWindow -------------
237
238 if (str=="a" || str=="attrs")
239 return PrintAttributes(out);
240
241 return false;
242}
Note: See TracBrowser for help on using the repository browser.