| 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 <iostream>
|
|---|
| 16 |
|
|---|
| 17 | #include "tools.h"
|
|---|
| 18 |
|
|---|
| 19 | #include "ReadlineColor.h"
|
|---|
| 20 |
|
|---|
| 21 | using 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 | //!
|
|---|
| 32 | Console::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 | //
|
|---|
| 44 | Console::~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 | //
|
|---|
| 55 | bool Console::PrintGeneralHelp()
|
|---|
| 56 | {
|
|---|
| 57 | return ReadlineColor::PrintGeneralHelp(fLogI, GetName());
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | // --------------------------------------------------------------------------
|
|---|
| 61 | //
|
|---|
| 62 | //! Wrapper to call the correspnding function from ReadlineColor
|
|---|
| 63 | //
|
|---|
| 64 | bool Console::PrintCommands()
|
|---|
| 65 | {
|
|---|
| 66 | return ReadlineColor::PrintCommands(fLogI);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | // --------------------------------------------------------------------------
|
|---|
| 70 | //
|
|---|
| 71 | //! Wrapper to call the correspnding function from ReadlineColor
|
|---|
| 72 | //
|
|---|
| 73 | bool 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 | //
|
|---|
| 85 | bool 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 | //
|
|---|
| 101 | void 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 | //
|
|---|
| 116 | void 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 |
|
|---|
| 132 | string Console::GetLinePrompt() const
|
|---|
| 133 | {
|
|---|
| 134 | const string siz = fLogO.GetSizeStr();
|
|---|
| 135 | return fContinous ?
|
|---|
| 136 | Form("[%d]", GetLine()) : Form("[%d:%s]", GetLine(), siz.c_str());
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // --------------------------------------------------------------------------
|
|---|
| 140 | //
|
|---|
| 141 | //! Before Readline::Run() is called the buffer is flushed as well as
|
|---|
| 142 | //! after the Run() loop has exited.
|
|---|
| 143 | //! command processing. This keeps things as seperated as possible,
|
|---|
| 144 | //! although there is no gurantee.
|
|---|
| 145 | //
|
|---|
| 146 | void Console::Run(const char *)
|
|---|
| 147 | {
|
|---|
| 148 | ReadlineColor::PrintBootMsg(fLogI, GetName());
|
|---|
| 149 |
|
|---|
| 150 | // Flush the buffer before we start out readline loop
|
|---|
| 151 | fLogI.Display(true);
|
|---|
| 152 | fLogO.Display(true);
|
|---|
| 153 |
|
|---|
| 154 | // Now run readlines main loop
|
|---|
| 155 | Readline::Run();
|
|---|
| 156 |
|
|---|
| 157 | // flush buffer to display
|
|---|
| 158 | fLogO.Display(true);
|
|---|
| 159 | fLogI.Display(true);
|
|---|
| 160 | }
|
|---|