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

Last change on this file since 10764 was 10663, checked in by tbretz, 14 years ago
Changed the logic of dumping the two output buffers.
File size: 4.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
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 (Readline::Process(str))
92 return true;
93
94 return false;
95}
96
97// --------------------------------------------------------------------------
98//
99//! Before readline starts flush the buffer to display all stuff which was
100//! buffered since the last readline call returned.
101//
102void Console::Startup()
103{
104 // Call readline's startup (just in case, it is empty)
105 Readline::Startup();
106
107 // First flush the buffer of the stream which is synchronous
108 // with the prompt
109 fLogI.Display(true);
110
111 // Now flush the stream which is asychronous
112 fLogO.Display(true);
113
114 // The order has the advantage that output initiated by the prompt
115 // is not interrupter by the synchronous stream
116}
117
118// --------------------------------------------------------------------------
119//
120//! Flush the buffer if we are in continous mode, and call Readline's
121//! EventHook to update the prompt.
122//
123void Console::EventHook()
124{
125 // If the output is continous and we are going to output something
126 // first jump back to the beginning of the line (well, that
127 // doesn't work well if the input line is already two lines)
128 // and then flush the buffer.
129 if (fContinous && fLogO.GetSizeBacklog()>0)
130 {
131 std::cout << "\r";
132 fLogO.Display(true);
133 }
134
135 // Call Readline's EventHook to update the prompt
136 Readline::EventHook();
137}
138
139string Console::GetLinePrompt() const
140{
141 const string siz = fLogO.GetSizeStr();
142
143 ostringstream str;
144 str << '[' << GetLine();
145 return fContinous ? str.str()+']' : str.str()+':'+siz+']';
146}
147
148// --------------------------------------------------------------------------
149//
150//! Before Readline::Run() is called the buffer is flushed as well as
151//! after the Run() loop has exited.
152//! command processing. This keeps things as seperated as possible,
153//! although there is no gurantee.
154//
155void Console::Run(const char *)
156{
157 ReadlineColor::PrintBootMsg(fLogI, GetName());
158
159 // Flush the buffer before we start out readline loop
160 fLogI.Display(true);
161 fLogO.Display(true);
162
163 // Now run readlines main loop
164 Readline::Run();
165
166 // flush buffer to display
167 fLogI.Display(true);
168 fLogO.Display(true);
169}
Note: See TracBrowser for help on using the repository browser.