FACT++  1.0
Console.cc
Go to the documentation of this file.
1 // **************************************************************************
12 // **************************************************************************
13 #include "Console.h"
14 
15 #include <unistd.h>
16 
17 #include <sstream>
18 #include <iostream>
19 
20 #include "tools.h"
21 
22 #include "ReadlineColor.h"
23 
24 using namespace std;
25 
26 // --------------------------------------------------------------------------
27 //
35 Console::Console(const char *name) : Readline(name), fContinous(false)
36 {
39  fLogO.SetBacklog(true);
40  fLogI.SetBacklog(true);
41 }
42 
43 // --------------------------------------------------------------------------
44 //
46 //
48 {
49  // flush buffer to display before it is destroyed in its destructor
50  fLogO.Display();
51  fLogI.Display();
52 }
53 
54 void Console::PrintReadlineError(const std::string &str)
55 {
56  fLogI << kRed << str << endl;
57 }
58 
59 // --------------------------------------------------------------------------
60 //
62 //
64 {
66 }
67 
68 // --------------------------------------------------------------------------
69 //
71 //
73 {
75 }
76 
77 // --------------------------------------------------------------------------
78 //
80 //
82 {
84 }
85 
87 {
88  // FIXME: Check missing
89  fLogO.Display(true);
90  fLogO.SetBacklog(false);
91  fLogO.SetNullOutput(false);
92 }
93 
95 {
96  // FIXME: Check missing
97  fLogO.SetNullOutput(true);
98  fLogO.SetBacklog(true);
99 }
100 
101 // --------------------------------------------------------------------------
102 //
107 //
108 bool Console::Process(const string &str)
109 {
110  if (ReadlineColor::Process(fLogI, str))
111  return true;
112 
113  if (str.substr(0, 3)==".w ")
114  {
115  Lock();
116  usleep(stoul(str.substr(3))*1000);
117  Unlock();
118  return true;
119  }
120 
121  if (Readline::Process(str))
122  return true;
123 
124  return false;
125 }
126 
127 // --------------------------------------------------------------------------
128 //
131 //
133 {
134  // Call readline's startup (just in case, it is empty)
136 
137  // First flush the buffer of the stream which is synchronous
138  // with the prompt
139  fLogI.Display(true);
140 
141  // Now flush the stream which is asychronous
142  fLogO.Display(true);
143 
144  // The order has the advantage that output initiated by the prompt
145  // is not interrupter by the synchronous stream
146 }
147 
148 // --------------------------------------------------------------------------
149 //
152 //
154 {
155  // If the output is continous and we are going to output something
156  // first jump back to the beginning of the line (well, that
157  // doesn't work well if the input line is already two lines)
158  // and then flush the buffer.
159  const bool newline = fContinous && fLogO.GetSizeBacklog()>0;
160  if (newline)
161  {
162  // Clear the line we are going to overwrite
163  std::cout << "\r\033[0K";
164  fLogO.Display(true);
165  }
166 
167  // Call Readline's EventHook to update the prompt
168  // and signal readline so that a new prompt is displayed
169  Readline::EventHook(newline);
170 }
171 
173 {
174  const string siz = fLogO.GetSizeStr();
175 
176  ostringstream str;
177  str << '[' << GetLine();
178  return fContinous ? str.str()+']' : str.str()+':'+siz+']';
179 }
180 
181 // --------------------------------------------------------------------------
182 //
187 //
188 void Console::Run(const char *)
189 {
190  // Flush the buffer before we print the boot message
191  fLogO.Display(true);
192 
194 
195  // Flush the buffer before we start out readline loop
196  fLogI.Display(true);
197  fLogO.Display(true);
198 
199  // Now run readlines main loop
200  Readline::Run();
201 
202  // flush buffer to display
203  fLogI.Display(true);
204  fLogO.Display(true);
205 }
206 
207 // **************************************************************************
221 // **************************************************************************
222 
224 {
225  fLogO.Display();
226 }
227 
228 // --------------------------------------------------------------------------
229 //
236 ConsoleStream::ConsoleStream(const char *name) : Readline(name)
237 {
238  fLogO.SetBacklog(false);
239  fLogO.SetNullOutput(false);
241 }
242 
243 void ConsoleStream::PrintReadlineError(const std::string &str)
244 {
245  fLogO << kRed << str << endl;
246 }
247 
248 // --------------------------------------------------------------------------
249 //
251 //
252 void ConsoleStream::Run(const char *)
253 {
254  while (!IsStopped())
255  {
256  const string buf = GetExternalInput();
257  SetExternalInput("");
258  if (!buf.empty())
259  ProcessLine(buf);
260 
261  usleep(100000);
262  }
263 }
int GetLine() const
Definition: Readline.h:121
void Unlock()
Definition: Console.cc:94
void Startup()
Definition: Console.cc:132
void Run(const char *=0)
Just usleep until Stop() was called.
Definition: Console.cc:252
void PrintReadlineError(const std::string &str)
Definition: Console.cc:54
bool PrintGeneralHelp(std::ostream &out, const std::string &name)
bool fContinous
Definition: Console.h:40
void EventHook(bool)
Definition: Console.cc:153
void SetNullOutput(bool n=true)
Switch on or off any physical output to the screen (cout or fWindow)
Definition: WindowLog.h:137
Console(const char *name)
Definition: Console.cc:35
char str[80]
Definition: test_client.c:7
WindowLog fLogO
Definition: Console.h:10
Set color Red.
Definition: WindowLog.h:17
WindowLog fLogI
Definition: Console.h:38
STL namespace.
virtual bool Process(const std::string &str)
Definition: Readline.cc:1094
~Console()
Flush the contents of the buffer before it is destroyed.
Definition: Console.cc:47
size_t GetSizeBacklog() const
Get the current size of the backlog in bytes.
Definition: WindowLog.h:133
bool PrintCommands(std::ostream &out)
bool Process(const std::string &str)
Definition: Console.cc:108
bool PrintKeyBindings()
Wrapper to call the correspnding function from ReadlineColor.
Definition: Console.cc:81
std::string GetSizeStr() const
Definition: WindowLog.cc:278
virtual void Run(const char *prompt=0)
Definition: Readline.cc:1406
WindowLog fLogO
Definition: Console.h:37
virtual void Startup()
Default: Do nothing.
Definition: Readline.cc:304
bool PrintBootMsg(std::ostream &out, const std::string &name, bool interactive=true)
static void SetExternalInput(const std::string &inp)
Definition: Readline.h:116
void PrintReadlineError(const std::string &str)
Definition: Console.cc:243
bool PrintCommands()
Wrapper to call the correspnding function from ReadlineColor.
Definition: Console.cc:72
bool Process(std::ostream &out, const std::string &str)
static std::string GetExternalInput()
Definition: Readline.h:119
std::string GetLinePrompt() const
Definition: Console.cc:172
bool PrintKeyBindings(std::ostream &out)
void ProcessLine(const std::string &str)
Definition: Readline.cc:1370
ConsoleStream(const char *name)
Definition: Console.cc:236
void Display(bool empty=false)
Display backlog.
Definition: WindowLog.cc:46
bool PrintGeneralHelp()
Wrapper to call the correspnding function from ReadlineColor.
Definition: Console.cc:63
std::string GetName() const
Definition: Readline.h:88
void Lock()
Definition: Console.cc:86
bool IsStopped() const
Definition: Readline.cc:1548
virtual void EventHook(bool newline=false)
Definition: Readline.cc:315
C++ wrapper for GNU&#39;s readline library.
Definition: Readline.h:10
void SetBacklog(bool n=true)
Switch on or off any storage in the backlog.
Definition: WindowLog.h:141
void Run(const char *=0)
Definition: Console.cc:188