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

Last change on this file since 10429 was 10429, checked in by tbretz, 9 years ago
Moved the tools function into their own namespace to get rid of problems whenlinking with root.
File size: 4.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 <iostream>
16
17#include "tools.h"
18
19#include "ReadlineColor.h"
20
21using namespace std;
22
23// --------------------------------------------------------------------------
24//
25//! Instantiate a console stream. It will create a WindowLog object
26//! and immediatel switch off its output to the console. The default more
27//! is non-continous.
28//!
29//! @param name
30//!     The name of the program passed to the Readline constructor
31//!
32Console::Console(const char *name) : Readline(name), fContinous(false)
33{
34    fLogO.SetNullOutput();
35    fLogI.SetNullOutput();
36    fLogO.SetBacklog(true);
37    fLogI.SetBacklog(true);
38}
39
40// --------------------------------------------------------------------------
41//
42//! Flush the contents of the buffer before it is destroyed.
43//
44Console::~Console()
45{
46    // flush buffer to display before it is destroyed in its destructor
47    fLogO.Display();
48    fLogI.Display();
49}
50
51// --------------------------------------------------------------------------
52//
53//! Wrapper to call the correspnding function from ReadlineColor
54//
55bool Console::PrintGeneralHelp()
56{
57    return ReadlineColor::PrintGeneralHelp(fLogI, GetName());
58}
59
60// --------------------------------------------------------------------------
61//
62//! Wrapper to call the correspnding function from ReadlineColor
63//
64bool Console::PrintCommands()
65{
66    return ReadlineColor::PrintCommands(fLogI);
67}
68
69// --------------------------------------------------------------------------
70//
71//! Wrapper to call the correspnding function from ReadlineColor
72//
73bool Console::PrintKeyBindings()
74{
75    return ReadlineColor::PrintKeyBindings(fLogI);
76}
77
78// --------------------------------------------------------------------------
79//
80//! Processes the command provided by the Shell-class.
81//!
82//! @returns
83//!    whether a command was successfully processed or could not be found
84//
85bool Console::Process(const string &str)
86{
87    if (ReadlineColor::Process(fLogI, str))
88        return true;
89
90    if (Readline::Process(str))
91        return true;
92
93    return false;
94}
95
96// --------------------------------------------------------------------------
97//
98//! Before readline starts flush the buffer to display all stuff which was
99//! buffered since the last readline call returned.
100//
101void Console::Startup()
102{
103    // Call readline's startup (just in case, it is empty)
104    Readline::Startup();
105
106    // Flush the buffer and remove the flushed contents from the buffer
107    fLogO.Display(true);
108    fLogI.Display(true);
109}
110
111// --------------------------------------------------------------------------
112//
113//! Flush the buffer if we are in continous mode, and call Readline's
114//! EventHook to update the prompt.
115//
116void Console::EventHook()
117{
118    // If the output is continous and we are going to output something
119    // first jump back to the beginning of the line (well, that
120    // doesn't work well if the input line is already two lines)
121    // and then flush the buffer.
122    if (fContinous && fLogO.GetSizeBacklog()>0)
123    {
124        std::cout << "\r";
125        fLogO.Display(true);
126    }
127
128    // Call Readline's EventHook to update the prompt
129    Readline::EventHook();
130}
131
132string Console::GetLinePrompt() const
133{
134    const string siz = fLogO.GetSizeStr();
135    return fContinous ?
136        Tools::Form("[%d]",    GetLine()) :
137        Tools::Form("[%d:%s]", GetLine(), siz.c_str());
138}
139
140// --------------------------------------------------------------------------
141//
142//! Before Readline::Run() is called the buffer is flushed as well as
143//! after the Run() loop has exited.
144//! command processing. This keeps things as seperated as possible,
145//! although there is no gurantee.
146//
147void Console::Run(const char *)
148{
149    ReadlineColor::PrintBootMsg(fLogI, GetName());
150
151    // Flush the buffer before we start out readline loop
152    fLogI.Display(true);
153    fLogO.Display(true);
154
155    // Now run readlines main loop
156    Readline::Run();
157
158    // flush buffer to display
159    fLogO.Display(true);
160    fLogI.Display(true);
161}
Note: See TracBrowser for help on using the repository browser.