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

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