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

Last change on this file since 14062 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: 6.6 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
84// --------------------------------------------------------------------------
85//
86//! Processes the command provided by the Shell-class.
87//!
88//! @returns
89//! whether a command was successfully processed or could not be found
90//
91bool Console::Process(const string &str)
92{
93 if (ReadlineColor::Process(fLogI, str))
94 return true;
95
96 if (str.substr(0, 3)==".w ")
97 {
98 fLogO.Display(true);
99 fLogO.SetBacklog(false);
100 fLogO.SetNullOutput(false);
101 usleep(stoul(str.substr(3))*1000);
102 fLogO.SetNullOutput(true);
103 fLogO.SetBacklog(true);
104 return true;
105 }
106
107 if (Readline::Process(str))
108 return true;
109
110 return false;
111}
112
113// --------------------------------------------------------------------------
114//
115//! Before readline starts flush the buffer to display all stuff which was
116//! buffered since the last readline call returned.
117//
118void Console::Startup()
119{
120 // Call readline's startup (just in case, it is empty)
121 Readline::Startup();
122
123 // First flush the buffer of the stream which is synchronous
124 // with the prompt
125 fLogI.Display(true);
126
127 // Now flush the stream which is asychronous
128 fLogO.Display(true);
129
130 // The order has the advantage that output initiated by the prompt
131 // is not interrupter by the synchronous stream
132}
133
134// --------------------------------------------------------------------------
135//
136//! Flush the buffer if we are in continous mode, and call Readline's
137//! EventHook to update the prompt.
138//
139void Console::EventHook()
140{
141 // If the output is continous and we are going to output something
142 // first jump back to the beginning of the line (well, that
143 // doesn't work well if the input line is already two lines)
144 // and then flush the buffer.
145 if (fContinous && fLogO.GetSizeBacklog()>0)
146 {
147 // Clear the line we are going to overwrite
148 std::cout << "\r\033[0K";
149 fLogO.Display(true);
150 }
151
152 // Call Readline's EventHook to update the prompt
153 Readline::EventHook();
154}
155
156string Console::GetLinePrompt() const
157{
158 const string siz = fLogO.GetSizeStr();
159
160 ostringstream str;
161 str << '[' << GetLine();
162 return fContinous ? str.str()+']' : str.str()+':'+siz+']';
163}
164
165// --------------------------------------------------------------------------
166//
167//! Before Readline::Run() is called the buffer is flushed as well as
168//! after the Run() loop has exited.
169//! command processing. This keeps things as seperated as possible,
170//! although there is no gurantee.
171//
172void Console::Run(const char *)
173{
174 // Flush the buffer before we print the boot message
175 fLogO.Display(true);
176
177 ReadlineColor::PrintBootMsg(fLogI, GetName());
178
179 // Flush the buffer before we start out readline loop
180 fLogI.Display(true);
181 fLogO.Display(true);
182
183 // Now run readlines main loop
184 Readline::Run();
185
186 // flush buffer to display
187 fLogI.Display(true);
188 fLogO.Display(true);
189}
190
191// **************************************************************************
192/** @class ConsoleStream
193
194@brief This is an extension to the Readline class provding a colored output
195
196This in an extension to the Readline class. It's purpose is just to have a
197colored output stream available. It's main idea is that it is used in
198environments without user interaction as a replacement the Console class.
199This is interesting to be able to do everything identical as if Console
200would be used, but Run() does not prompt but just wait until Stop()
201was called. The advantage is that some functions of Readline can be used
202like Execute and the history (just for Execute() commands of course)
203
204 */
205// **************************************************************************
206
207
208// --------------------------------------------------------------------------
209//
210//! Instantiate a console stream. It will create a single WindowLog object
211//! which is returned as input and output stream.
212//!
213//! @param name
214//! The name of the program passed to the Readline constructor
215//!
216ConsoleStream::ConsoleStream(const char *name) : Readline(name)
217{
218 fLogO.SetBacklog(false);
219 fLogO.SetNullOutput(false);
220 ReadlineColor::PrintBootMsg(fLogO, GetName(), false);
221}
222
223void ConsoleStream::PrintReadlineError(const std::string &str)
224{
225 fLogO << kRed << str << endl;
226}
227
228// --------------------------------------------------------------------------
229//
230//! Just usleep until Stop() was called.
231//
232void ConsoleStream::Run(const char *)
233{
234 while (!IsStopped())
235 usleep(10000);
236}
Note: See TracBrowser for help on using the repository browser.