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

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