source: trunk/FACT++/src/Console.cc@ 14814

Last change on this file since 14814 was 14083, checked in by tbretz, 12 years ago
Moved switching off and on of the logging before and after a sleep to Lock() and Unlock() respectively.
File size: 6.9 KB
Line 
1// **************************************************************************
2/** @class Console
3
4@brief This is an extension to the Readline class provding buffered output
5
6This in an extension to the Readline class. It's purpose is to keep a
7buffered output stream and flush the stream either between readline entries
8(non continous mode) or continously, keeping the readline prompt as
9intact as possible.
10
11 */
12// **************************************************************************
13#include "Console.h"
14
15#include <sstream>
16#include <iostream>
17
18#include "tools.h"
19
20#include "ReadlineColor.h"
21
22using namespace std;
23
24// --------------------------------------------------------------------------
25//
26//! Instantiate a console stream. It will create a WindowLog object
27//! and immediatel switch off its output to the console. The default more
28//! is non-continous.
29//!
30//! @param name
31//! The name of the program passed to the Readline constructor
32//!
33Console::Console(const char *name) : Readline(name), fContinous(false)
34{
35 fLogO.SetNullOutput();
36 fLogI.SetNullOutput();
37 fLogO.SetBacklog(true);
38 fLogI.SetBacklog(true);
39}
40
41// --------------------------------------------------------------------------
42//
43//! Flush the contents of the buffer before it is destroyed.
44//
45Console::~Console()
46{
47 // flush buffer to display before it is destroyed in its destructor
48 fLogO.Display();
49 fLogI.Display();
50}
51
52void Console::PrintReadlineError(const std::string &str)
53{
54 fLogI << kRed << str << endl;
55}
56
57// --------------------------------------------------------------------------
58//
59//! Wrapper to call the correspnding function from ReadlineColor
60//
61bool Console::PrintGeneralHelp()
62{
63 return ReadlineColor::PrintGeneralHelp(fLogI, GetName());
64}
65
66// --------------------------------------------------------------------------
67//
68//! Wrapper to call the correspnding function from ReadlineColor
69//
70bool Console::PrintCommands()
71{
72 return ReadlineColor::PrintCommands(fLogI);
73}
74
75// --------------------------------------------------------------------------
76//
77//! Wrapper to call the correspnding function from ReadlineColor
78//
79bool Console::PrintKeyBindings()
80{
81 return ReadlineColor::PrintKeyBindings(fLogI);
82}
83
84void Console::Lock()
85{
86 // FIXME: Check missing
87 fLogO.Display(true);
88 fLogO.SetBacklog(false);
89 fLogO.SetNullOutput(false);
90}
91
92void Console::Unlock()
93{
94 // FIXME: Check missing
95 fLogO.SetNullOutput(true);
96 fLogO.SetBacklog(true);
97}
98
99// --------------------------------------------------------------------------
100//
101//! Processes the command provided by the Shell-class.
102//!
103//! @returns
104//! whether a command was successfully processed or could not be found
105//
106bool Console::Process(const string &str)
107{
108 if (ReadlineColor::Process(fLogI, str))
109 return true;
110
111 if (str.substr(0, 3)==".w ")
112 {
113 Lock();
114 usleep(stoul(str.substr(3))*1000);
115 Unlock();
116 return true;
117 }
118
119 if (Readline::Process(str))
120 return true;
121
122 return false;
123}
124
125// --------------------------------------------------------------------------
126//
127//! Before readline starts flush the buffer to display all stuff which was
128//! buffered since the last readline call returned.
129//
130void Console::Startup()
131{
132 // Call readline's startup (just in case, it is empty)
133 Readline::Startup();
134
135 // First flush the buffer of the stream which is synchronous
136 // with the prompt
137 fLogI.Display(true);
138
139 // Now flush the stream which is asychronous
140 fLogO.Display(true);
141
142 // The order has the advantage that output initiated by the prompt
143 // is not interrupter by the synchronous stream
144}
145
146// --------------------------------------------------------------------------
147//
148//! Flush the buffer if we are in continous mode, and call Readline's
149//! EventHook to update the prompt.
150//
151void Console::EventHook()
152{
153 // If the output is continous and we are going to output something
154 // first jump back to the beginning of the line (well, that
155 // doesn't work well if the input line is already two lines)
156 // and then flush the buffer.
157 if (fContinous && fLogO.GetSizeBacklog()>0)
158 {
159 // Clear the line we are going to overwrite
160 std::cout << "\r\033[0K";
161 fLogO.Display(true);
162 }
163
164 // Call Readline's EventHook to update the prompt
165 Readline::EventHook();
166}
167
168string Console::GetLinePrompt() const
169{
170 const string siz = fLogO.GetSizeStr();
171
172 ostringstream str;
173 str << '[' << GetLine();
174 return fContinous ? str.str()+']' : str.str()+':'+siz+']';
175}
176
177// --------------------------------------------------------------------------
178//
179//! Before Readline::Run() is called the buffer is flushed as well as
180//! after the Run() loop has exited.
181//! command processing. This keeps things as seperated as possible,
182//! although there is no gurantee.
183//
184void Console::Run(const char *)
185{
186 // Flush the buffer before we print the boot message
187 fLogO.Display(true);
188
189 ReadlineColor::PrintBootMsg(fLogI, GetName());
190
191 // Flush the buffer before we start out readline loop
192 fLogI.Display(true);
193 fLogO.Display(true);
194
195 // Now run readlines main loop
196 Readline::Run();
197
198 // flush buffer to display
199 fLogI.Display(true);
200 fLogO.Display(true);
201}
202
203// **************************************************************************
204/** @class ConsoleStream
205
206@brief This is an extension to the Readline class provding a colored output
207
208This in an extension to the Readline class. It's purpose is just to have a
209colored output stream available. It's main idea is that it is used in
210environments without user interaction as a replacement the Console class.
211This is interesting to be able to do everything identical as if Console
212would be used, but Run() does not prompt but just wait until Stop()
213was called. The advantage is that some functions of Readline can be used
214like Execute and the history (just for Execute() commands of course)
215
216 */
217// **************************************************************************
218
219ConsoleStream::~ConsoleStream()
220{
221 fLogO.Display();
222}
223
224// --------------------------------------------------------------------------
225//
226//! Instantiate a console stream. It will create a single WindowLog object
227//! which is returned as input and output stream.
228//!
229//! @param name
230//! The name of the program passed to the Readline constructor
231//!
232ConsoleStream::ConsoleStream(const char *name) : Readline(name)
233{
234 fLogO.SetBacklog(false);
235 fLogO.SetNullOutput(false);
236 ReadlineColor::PrintBootMsg(fLogO, GetName(), false);
237}
238
239void ConsoleStream::PrintReadlineError(const std::string &str)
240{
241 fLogO << kRed << str << endl;
242}
243
244// --------------------------------------------------------------------------
245//
246//! Just usleep until Stop() was called.
247//
248void ConsoleStream::Run(const char *)
249{
250 while (!IsStopped())
251 {
252 const string buf = GetExternalInput();
253 SetExternalInput("");
254 if (!buf.empty())
255 ProcessLine(buf);
256
257 usleep(100000);
258 }
259}
Note: See TracBrowser for help on using the repository browser.