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

Last change on this file since 10870 was 10860, checked in by tbretz, 9 years ago
Delete the line after CR.
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        // Clear the line we are going to overwrite
132        std::cout << "\r\033[0K";
133        fLogO.Display(true);
134    }
135
136    // Call Readline's EventHook to update the prompt
137    Readline::EventHook();
138}
139
140string Console::GetLinePrompt() const
141{
142    const string siz = fLogO.GetSizeStr();
143
144    ostringstream str;
145    str << '[' << GetLine();
146    return fContinous ? str.str()+']' : str.str()+':'+siz+']';
147}
148
149// --------------------------------------------------------------------------
150//
151//! Before Readline::Run() is called the buffer is flushed as well as
152//! after the Run() loop has exited.
153//! command processing. This keeps things as seperated as possible,
154//! although there is no gurantee.
155//
156void Console::Run(const char *)
157{
158    ReadlineColor::PrintBootMsg(fLogI, GetName());
159
160    // Flush the buffer before we start out readline loop
161    fLogI.Display(true);
162    fLogO.Display(true);
163
164    // Now run readlines main loop
165    Readline::Run();
166
167    // flush buffer to display
168    fLogI.Display(true);
169    fLogO.Display(true);
170}
Note: See TracBrowser for help on using the repository browser.