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

Last change on this file since 11656 was 11479, checked in by tbretz, 13 years ago
Replaces ato/atof by stoi/stof; replaced boost::lexical_cast by to_string
File size: 6.3 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 ReadlineColor::PrintBootMsg(fLogI, GetName());
170
171 // Flush the buffer before we start out readline loop
172 fLogI.Display(true);
173 fLogO.Display(true);
174
175 // Now run readlines main loop
176 Readline::Run();
177
178 // flush buffer to display
179 fLogI.Display(true);
180 fLogO.Display(true);
181}
182
183// **************************************************************************
184/** @class ConsoleStream
185
186@brief This is an extension to the Readline class provding a colored output
187
188This in an extension to the Readline class. It's purpose is just to have a
189colored output stream available. It's main idea is that it is used in
190environments without user interaction as a replacement the Console class.
191This is interesting to be able to do everything identical as if Console
192would be used, but Run() does not prompt but just wait until Stop()
193was called. The advantage is that some functions of Readline can be used
194like Execute and the history (just for Execute() commands of course)
195
196 */
197// **************************************************************************
198
199
200// --------------------------------------------------------------------------
201//
202//! Instantiate a console stream. It will create a single WindowLog object
203//! which is returned as input and output stream.
204//!
205//! @param name
206//! The name of the program passed to the Readline constructor
207//!
208ConsoleStream::ConsoleStream(const char *name) : Readline(name)
209{
210 ReadlineColor::PrintBootMsg(fLogO, GetName(), false);
211}
212
213// --------------------------------------------------------------------------
214//
215//! Just usleep until Stop() was called.
216//
217void ConsoleStream::Run(const char *)
218{
219 while (!IsStopped())
220 usleep(10000);
221}
Note: See TracBrowser for help on using the repository browser.