| 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 <unistd.h>
|
|---|
| 16 |
|
|---|
| 17 | #include <sstream>
|
|---|
| 18 | #include <iostream>
|
|---|
| 19 |
|
|---|
| 20 | #include "tools.h"
|
|---|
| 21 |
|
|---|
| 22 | #include "ReadlineColor.h"
|
|---|
| 23 |
|
|---|
| 24 | using namespace std;
|
|---|
| 25 |
|
|---|
| 26 | // --------------------------------------------------------------------------
|
|---|
| 27 | //
|
|---|
| 28 | //! Instantiate a console stream. It will create a WindowLog object
|
|---|
| 29 | //! and immediatel switch off its output to the console. The default more
|
|---|
| 30 | //! is non-continous.
|
|---|
| 31 | //!
|
|---|
| 32 | //! @param name
|
|---|
| 33 | //! The name of the program passed to the Readline constructor
|
|---|
| 34 | //!
|
|---|
| 35 | Console::Console(const char *name) : Readline(name), fContinous(false)
|
|---|
| 36 | {
|
|---|
| 37 | fLogO.SetNullOutput();
|
|---|
| 38 | fLogI.SetNullOutput();
|
|---|
| 39 | fLogO.SetBacklog(true);
|
|---|
| 40 | fLogI.SetBacklog(true);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | // --------------------------------------------------------------------------
|
|---|
| 44 | //
|
|---|
| 45 | //! Flush the contents of the buffer before it is destroyed.
|
|---|
| 46 | //
|
|---|
| 47 | Console::~Console()
|
|---|
| 48 | {
|
|---|
| 49 | // flush buffer to display before it is destroyed in its destructor
|
|---|
| 50 | fLogO.Display();
|
|---|
| 51 | fLogI.Display();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | void Console::PrintReadlineError(const std::string &str)
|
|---|
| 55 | {
|
|---|
| 56 | fLogI << kRed << str << endl;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | // --------------------------------------------------------------------------
|
|---|
| 60 | //
|
|---|
| 61 | //! Wrapper to call the correspnding function from ReadlineColor
|
|---|
| 62 | //
|
|---|
| 63 | bool Console::PrintGeneralHelp()
|
|---|
| 64 | {
|
|---|
| 65 | return ReadlineColor::PrintGeneralHelp(fLogI, GetName());
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // --------------------------------------------------------------------------
|
|---|
| 69 | //
|
|---|
| 70 | //! Wrapper to call the correspnding function from ReadlineColor
|
|---|
| 71 | //
|
|---|
| 72 | bool Console::PrintCommands()
|
|---|
| 73 | {
|
|---|
| 74 | return ReadlineColor::PrintCommands(fLogI);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | // --------------------------------------------------------------------------
|
|---|
| 78 | //
|
|---|
| 79 | //! Wrapper to call the correspnding function from ReadlineColor
|
|---|
| 80 | //
|
|---|
| 81 | bool Console::PrintKeyBindings()
|
|---|
| 82 | {
|
|---|
| 83 | return ReadlineColor::PrintKeyBindings(fLogI);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void Console::Lock()
|
|---|
| 87 | {
|
|---|
| 88 | // FIXME: Check missing
|
|---|
| 89 | fLogO.Display(true);
|
|---|
| 90 | fLogO.SetBacklog(false);
|
|---|
| 91 | fLogO.SetNullOutput(false);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | void Console::Unlock()
|
|---|
| 95 | {
|
|---|
| 96 | // FIXME: Check missing
|
|---|
| 97 | fLogO.SetNullOutput(true);
|
|---|
| 98 | fLogO.SetBacklog(true);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | // --------------------------------------------------------------------------
|
|---|
| 102 | //
|
|---|
| 103 | //! Processes the command provided by the Shell-class.
|
|---|
| 104 | //!
|
|---|
| 105 | //! @returns
|
|---|
| 106 | //! whether a command was successfully processed or could not be found
|
|---|
| 107 | //
|
|---|
| 108 | bool Console::Process(const string &str)
|
|---|
| 109 | {
|
|---|
| 110 | if (ReadlineColor::Process(fLogI, str))
|
|---|
| 111 | return true;
|
|---|
| 112 |
|
|---|
| 113 | if (str.substr(0, 3)==".w ")
|
|---|
| 114 | {
|
|---|
| 115 | Lock();
|
|---|
| 116 | usleep(stoul(str.substr(3))*1000);
|
|---|
| 117 | Unlock();
|
|---|
| 118 | return true;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | if (Readline::Process(str))
|
|---|
| 122 | return true;
|
|---|
| 123 |
|
|---|
| 124 | return false;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | // --------------------------------------------------------------------------
|
|---|
| 128 | //
|
|---|
| 129 | //! Before readline starts flush the buffer to display all stuff which was
|
|---|
| 130 | //! buffered since the last readline call returned.
|
|---|
| 131 | //
|
|---|
| 132 | void Console::Startup()
|
|---|
| 133 | {
|
|---|
| 134 | // Call readline's startup (just in case, it is empty)
|
|---|
| 135 | Readline::Startup();
|
|---|
| 136 |
|
|---|
| 137 | // First flush the buffer of the stream which is synchronous
|
|---|
| 138 | // with the prompt
|
|---|
| 139 | fLogI.Display(true);
|
|---|
| 140 |
|
|---|
| 141 | // Now flush the stream which is asychronous
|
|---|
| 142 | fLogO.Display(true);
|
|---|
| 143 |
|
|---|
| 144 | // The order has the advantage that output initiated by the prompt
|
|---|
| 145 | // is not interrupter by the synchronous stream
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | // --------------------------------------------------------------------------
|
|---|
| 149 | //
|
|---|
| 150 | //! Flush the buffer if we are in continous mode, and call Readline's
|
|---|
| 151 | //! EventHook to update the prompt.
|
|---|
| 152 | //
|
|---|
| 153 | void Console::EventHook(bool)
|
|---|
| 154 | {
|
|---|
| 155 | // If the output is continous and we are going to output something
|
|---|
| 156 | // first jump back to the beginning of the line (well, that
|
|---|
| 157 | // doesn't work well if the input line is already two lines)
|
|---|
| 158 | // and then flush the buffer.
|
|---|
| 159 | const bool newline = fContinous && fLogO.GetSizeBacklog()>0;
|
|---|
| 160 | if (newline)
|
|---|
| 161 | {
|
|---|
| 162 | // Clear the line we are going to overwrite
|
|---|
| 163 | std::cout << "\r\033[0K";
|
|---|
| 164 | fLogO.Display(true);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | // Call Readline's EventHook to update the prompt
|
|---|
| 168 | // and signal readline so that a new prompt is displayed
|
|---|
| 169 | Readline::EventHook(newline);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | string Console::GetLinePrompt() const
|
|---|
| 173 | {
|
|---|
| 174 | const string siz = fLogO.GetSizeStr();
|
|---|
| 175 |
|
|---|
| 176 | ostringstream str;
|
|---|
| 177 | str << '[' << GetLine();
|
|---|
| 178 | return fContinous ? str.str()+']' : str.str()+':'+siz+']';
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | // --------------------------------------------------------------------------
|
|---|
| 182 | //
|
|---|
| 183 | //! Before Readline::Run() is called the buffer is flushed as well as
|
|---|
| 184 | //! after the Run() loop has exited.
|
|---|
| 185 | //! command processing. This keeps things as seperated as possible,
|
|---|
| 186 | //! although there is no gurantee.
|
|---|
| 187 | //
|
|---|
| 188 | void Console::Run(const char *)
|
|---|
| 189 | {
|
|---|
| 190 | // Flush the buffer before we print the boot message
|
|---|
| 191 | fLogO.Display(true);
|
|---|
| 192 |
|
|---|
| 193 | ReadlineColor::PrintBootMsg(fLogI, GetName());
|
|---|
| 194 |
|
|---|
| 195 | // Flush the buffer before we start out readline loop
|
|---|
| 196 | fLogI.Display(true);
|
|---|
| 197 | fLogO.Display(true);
|
|---|
| 198 |
|
|---|
| 199 | // Now run readlines main loop
|
|---|
| 200 | Readline::Run();
|
|---|
| 201 |
|
|---|
| 202 | // flush buffer to display
|
|---|
| 203 | fLogI.Display(true);
|
|---|
| 204 | fLogO.Display(true);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | // **************************************************************************
|
|---|
| 208 | /** @class ConsoleStream
|
|---|
| 209 |
|
|---|
| 210 | @brief This is an extension to the Readline class provding a colored output
|
|---|
| 211 |
|
|---|
| 212 | This in an extension to the Readline class. It's purpose is just to have a
|
|---|
| 213 | colored output stream available. It's main idea is that it is used in
|
|---|
| 214 | environments without user interaction as a replacement the Console class.
|
|---|
| 215 | This is interesting to be able to do everything identical as if Console
|
|---|
| 216 | would be used, but Run() does not prompt but just wait until Stop()
|
|---|
| 217 | was called. The advantage is that some functions of Readline can be used
|
|---|
| 218 | like Execute and the history (just for Execute() commands of course)
|
|---|
| 219 |
|
|---|
| 220 | */
|
|---|
| 221 | // **************************************************************************
|
|---|
| 222 |
|
|---|
| 223 | ConsoleStream::~ConsoleStream()
|
|---|
| 224 | {
|
|---|
| 225 | fLogO.Display();
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | // --------------------------------------------------------------------------
|
|---|
| 229 | //
|
|---|
| 230 | //! Instantiate a console stream. It will create a single WindowLog object
|
|---|
| 231 | //! which is returned as input and output stream.
|
|---|
| 232 | //!
|
|---|
| 233 | //! @param name
|
|---|
| 234 | //! The name of the program passed to the Readline constructor
|
|---|
| 235 | //!
|
|---|
| 236 | ConsoleStream::ConsoleStream(const char *name) : Readline(name)
|
|---|
| 237 | {
|
|---|
| 238 | fLogO.SetBacklog(false);
|
|---|
| 239 | fLogO.SetNullOutput(false);
|
|---|
| 240 | ReadlineColor::PrintBootMsg(fLogO, GetName(), false);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | void ConsoleStream::PrintReadlineError(const std::string &str)
|
|---|
| 244 | {
|
|---|
| 245 | fLogO << kRed << str << endl;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | // --------------------------------------------------------------------------
|
|---|
| 249 | //
|
|---|
| 250 | //! Just usleep until Stop() was called.
|
|---|
| 251 | //
|
|---|
| 252 | void ConsoleStream::Run(const char *)
|
|---|
| 253 | {
|
|---|
| 254 | while (!IsStopped())
|
|---|
| 255 | {
|
|---|
| 256 | const string buf = GetExternalInput();
|
|---|
| 257 | SetExternalInput("");
|
|---|
| 258 | if (!buf.empty())
|
|---|
| 259 | ProcessLine(buf);
|
|---|
| 260 |
|
|---|
| 261 | usleep(100000);
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|