1 | // **************************************************************************
|
---|
2 | /** @class Console
|
---|
3 |
|
---|
4 | @brief This is an extension to the Readline class provding buffered output
|
---|
5 |
|
---|
6 | This in an extension to the Readline class. It's purpose is to keep a
|
---|
7 | buffered output stream and flush the stream either between readline entries
|
---|
8 | (non continous mode) or continously, keeping the readline prompt as
|
---|
9 | intact 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 |
|
---|
22 | using 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 | //!
|
---|
33 | Console::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 | //
|
---|
45 | Console::~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 | //
|
---|
56 | bool Console::PrintGeneralHelp()
|
---|
57 | {
|
---|
58 | return ReadlineColor::PrintGeneralHelp(fLogI, GetName());
|
---|
59 | }
|
---|
60 |
|
---|
61 | // --------------------------------------------------------------------------
|
---|
62 | //
|
---|
63 | //! Wrapper to call the correspnding function from ReadlineColor
|
---|
64 | //
|
---|
65 | bool Console::PrintCommands()
|
---|
66 | {
|
---|
67 | return ReadlineColor::PrintCommands(fLogI);
|
---|
68 | }
|
---|
69 |
|
---|
70 | // --------------------------------------------------------------------------
|
---|
71 | //
|
---|
72 | //! Wrapper to call the correspnding function from ReadlineColor
|
---|
73 | //
|
---|
74 | bool 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 | //
|
---|
86 | bool 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 | //
|
---|
102 | void Console::Startup()
|
---|
103 | {
|
---|
104 | // Call readline's startup (just in case, it is empty)
|
---|
105 | Readline::Startup();
|
---|
106 |
|
---|
107 | // Flush the buffer and remove the flushed contents from the buffer
|
---|
108 | fLogO.Display(true);
|
---|
109 | fLogI.Display(true);
|
---|
110 | }
|
---|
111 |
|
---|
112 | // --------------------------------------------------------------------------
|
---|
113 | //
|
---|
114 | //! Flush the buffer if we are in continous mode, and call Readline's
|
---|
115 | //! EventHook to update the prompt.
|
---|
116 | //
|
---|
117 | void Console::EventHook()
|
---|
118 | {
|
---|
119 | // If the output is continous and we are going to output something
|
---|
120 | // first jump back to the beginning of the line (well, that
|
---|
121 | // doesn't work well if the input line is already two lines)
|
---|
122 | // and then flush the buffer.
|
---|
123 | if (fContinous && fLogO.GetSizeBacklog()>0)
|
---|
124 | {
|
---|
125 | std::cout << "\r";
|
---|
126 | fLogO.Display(true);
|
---|
127 | }
|
---|
128 |
|
---|
129 | // Call Readline's EventHook to update the prompt
|
---|
130 | Readline::EventHook();
|
---|
131 | }
|
---|
132 |
|
---|
133 | string Console::GetLinePrompt() const
|
---|
134 | {
|
---|
135 | const string siz = fLogO.GetSizeStr();
|
---|
136 |
|
---|
137 | ostringstream str;
|
---|
138 | str << '[' << GetLine();
|
---|
139 | return fContinous ? str.str()+']' : str.str()+':'+siz+']';
|
---|
140 | }
|
---|
141 |
|
---|
142 | // --------------------------------------------------------------------------
|
---|
143 | //
|
---|
144 | //! Before Readline::Run() is called the buffer is flushed as well as
|
---|
145 | //! after the Run() loop has exited.
|
---|
146 | //! command processing. This keeps things as seperated as possible,
|
---|
147 | //! although there is no gurantee.
|
---|
148 | //
|
---|
149 | void Console::Run(const char *)
|
---|
150 | {
|
---|
151 | ReadlineColor::PrintBootMsg(fLogI, GetName());
|
---|
152 |
|
---|
153 | // Flush the buffer before we start out readline loop
|
---|
154 | fLogI.Display(true);
|
---|
155 | fLogO.Display(true);
|
---|
156 |
|
---|
157 | // Now run readlines main loop
|
---|
158 | Readline::Run();
|
---|
159 |
|
---|
160 | // flush buffer to display
|
---|
161 | fLogO.Display(true);
|
---|
162 | fLogI.Display(true);
|
---|
163 | }
|
---|