FACT++  1.0
ReadlineColor.cc
Go to the documentation of this file.
1 // **************************************************************************
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 
17 using namespace std;
18 
19 // --------------------------------------------------------------------------
20 //
23 //
24 bool 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 INFORMATION " << 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 //
57 //
58 bool 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 //
74 //
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 //
114 //
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 << " Home " << kReset << "Beginning of line" << endl;
126  out << kBold << " End " << kReset << "End of line" << endl;
127  out << kBold << " Ctrl-d " << kReset << "Quit" << endl;
128  out << kBold << " Ctrl-y " << kReset << "Delete line" << endl;
129  out << kBold << " Alt-end/Ctrl-k " << kReset << "Delete until the end of the line" << endl;
130  out << endl;
131 
132  return true;
133 }
134 
135 // --------------------------------------------------------------------------
136 //
145 //
146 bool ReadlineColor::PrintGeneralHelp(ostream &out, const string &name)
147 {
148  out << endl;
149  out << " " << kUnderline << "General help:" << endl << endl;
150  out << " The command history is automatically loaded and saves to" << endl;
151  out << " and from " << name << endl;
152  out << endl;
153  out << kBold << " h,help " << kReset << "Print this help message\n";
154  out << kBold << " clear " << kReset << "Clear history buffer\n";
155  out << kBold << " lh,history " << kReset << "Dump the history buffer to the screen\n";
156  out << kBold << " v,variable " << kReset << "Dump readline variables\n";
157  out << kBold << " f,function " << kReset << "Dump readline functions\n";
158  out << kBold << " m,funmap " << kReset << "Dump readline funmap\n";
159  out << kBold << " c,command " << kReset << "Dump available commands\n";
160  out << kBold << " k,keylist " << kReset << "Dump key bindings\n";
161  out << kBold << " a,attrs " << kReset << "Dump available stream attributes\n";
162  out << kBold << " .! command " << kReset << "Execute a shell command\n";
163  out << kBold << " .w n " << kReset << "Sleep n milliseconds\n";
164  out << kBold << " .x filename " << kReset << "Execute a script of commands (+optional arguments)\n";
165  out << kBold << " .x file:N " << kReset << "Execute a script of commands, start at label N\n";
166  out << kBold << " .j N " << kReset << "Forward jump to label N\n";
167  out << kBold << " .lt f0 f1 N " << kReset << "If float f0 lower than float f1, jump to label N\n";
168  out << kBold << " .gt f0 f1 N " << kReset << "If float f0 greater than float f1, jump to label N\n";
169  out << kBold << " .eq i0 i1 N " << kReset << "If int i0 equal int i1, jump to label N\n";
170  out << kBold << " : N " << kReset << "Defines a label (N=number)\n";
171  out << kBold << " # comment " << kReset << "Ignored\n";
172  out << kBold << " .q,quit " << kReset << "Quit" << endl;
173  out << endl;
174 
175  return true;
176 }
177 
178 // --------------------------------------------------------------------------
179 //
187 //
188 bool ReadlineColor::ExecuteShellCommand(ostream &out, const string &cmd)
189 {
190  FILE *pipe = popen(cmd.c_str(), "r");
191  if (!pipe)
192  {
193  out << kRed << "ERROR - Could not create pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
194  return true;
195  }
196 
197  while (1)
198  {
199  char buf[1024];
200 
201  const size_t sz = fread(buf, 1, 1024, pipe);
202  out.write(buf, sz);
203 
204  if (feof(pipe) || ferror(pipe))
205  break;
206  }
207 
208  out << endl;
209 
210  if (ferror(pipe))
211  out << kRed << "ERROR - Reading from pipe '" << cmd << "': " << strerror(errno) << " [" << errno << "]" << endl;
212 
213  pclose(pipe);
214 
215  return true;
216 }
217 
218 
219 bool ReadlineColor::Process(ostream &out, const string &str)
220 {
221  // ----------- Readline -----------
222 
223  if (str.substr(0, 2)==".!")
224  return ExecuteShellCommand(out, str.substr(2));
225 
226  if (str=="lh" || str=="history")
227  {
228  out << endl << kBold << "History:" << endl;
230  }
231 
232  if (str=="v" || str=="variable")
233  {
234  out << endl << kBold << "Variables:" << endl;
236  }
237 
238  if (str=="f" || str=="function")
239  {
240  out << endl << kBold << "Functions:" << endl;
242  }
243 
244  if (str=="m" || str=="funmap")
245  {
246  out << endl << kBold << "Funmap:" << endl;
248  }
249 
250  // ------------ ReadlineWindow -------------
251 
252  if (str=="a" || str=="attrs")
253  return PrintAttributes(out);
254 
255  return false;
256 }
Set attribute Invisible.
Definition: WindowLog.h:38
Set color Cyan.
Definition: WindowLog.h:22
Set attribute Reverse.
Definition: WindowLog.h:33
bool PrintGeneralHelp(std::ostream &out, const std::string &name)
bool PrintAttributes(std::ostream &out)
Set attribute Alternative charset.
Definition: WindowLog.h:39
Set color White.
Definition: WindowLog.h:23
static bool DumpHistory()
Definition: Readline.cc:897
Set color Green.
Definition: WindowLog.h:18
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
Set color Yellow.
Definition: WindowLog.h:19
Set attribute Blink.
Definition: WindowLog.h:34
Set color Red.
Definition: WindowLog.h:17
static bool RedirectionWrapper(std::ostream &out, bool(*function)())
Definition: Readline.cc:192
Set default colors.
Definition: WindowLog.h:16
STL namespace.
Set attribute Highlight.
Definition: WindowLog.h:31
static const _time_format reset
Definition: Time.h:39
Set attribute Normal.
Definition: WindowLog.h:30
bool PrintCommands(std::ostream &out)
Set attribute Underline.
Definition: WindowLog.h:32
static bool DumpFunctions()
Definition: Readline.cc:687
Set color Magenta.
Definition: WindowLog.h:21
bool ExecuteShellCommand(std::ostream &out, const std::string &cmd)
Set color Blue.
Definition: WindowLog.h:20
Set attribute Dim.
Definition: WindowLog.h:35
static const _time_format fmt(const char *txt=0)
A stream manipulator to set the output/input format.
Definition: Time.cc:415
bool PrintBootMsg(std::ostream &out, const std::string &name, bool interactive=true)
bool Process(std::ostream &out, const std::string &str)
bool PrintKeyBindings(std::ostream &out)
Set attribute Protect.
Definition: WindowLog.h:37
static bool DumpFunmap()
Definition: Readline.cc:704
Reset all attributes.
Definition: WindowLog.h:29
static bool DumpVariables()
Definition: Readline.cc:667
Set attribute Bold.
Definition: WindowLog.h:36