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 | using namespace std;
|
---|
20 |
|
---|
21 | // --------------------------------------------------------------------------
|
---|
22 | //
|
---|
23 | //! Instantiate a console stream. It will create a WindowLog object
|
---|
24 | //! and immediatel switch off its output to the console. The default more
|
---|
25 | //! is non-continous.
|
---|
26 | //!
|
---|
27 | //! @param name
|
---|
28 | //! The name of the program passed to the Readline constructor
|
---|
29 | //!
|
---|
30 | Console::Console(const char *name) : Readline(name), fContinous(false)
|
---|
31 | {
|
---|
32 | fLogO.SetNullOutput();
|
---|
33 | fLogI.SetNullOutput();
|
---|
34 | fLogO.SetBacklog(true);
|
---|
35 | fLogI.SetBacklog(true);
|
---|
36 | }
|
---|
37 |
|
---|
38 | // --------------------------------------------------------------------------
|
---|
39 | //
|
---|
40 | //! Flush the contents of the buffer before it is destroyed.
|
---|
41 | //
|
---|
42 | Console::~Console()
|
---|
43 | {
|
---|
44 | // flush buffer to display before it is destroyed in its destructor
|
---|
45 | fLogO.Display();
|
---|
46 | fLogI.Display();
|
---|
47 | }
|
---|
48 |
|
---|
49 | // --------------------------------------------------------------------------
|
---|
50 | //
|
---|
51 | //! Before readline starts flush the buffer to display all stuff which was
|
---|
52 | //! buffered since the last readline call returned.
|
---|
53 | //
|
---|
54 | void Console::Startup()
|
---|
55 | {
|
---|
56 | // Call readline's startup (just in case, it is empty)
|
---|
57 | Readline::Startup();
|
---|
58 |
|
---|
59 | // Flush the buffer and remove the flushed contents from the buffer
|
---|
60 | fLogO.Display(true);
|
---|
61 | fLogI.Display(true);
|
---|
62 | }
|
---|
63 |
|
---|
64 | // --------------------------------------------------------------------------
|
---|
65 | //
|
---|
66 | //! Flush the buffer if we are in continous mode, and call Readline's
|
---|
67 | //! EventHook to update the prompt.
|
---|
68 | //
|
---|
69 | void Console::EventHook()
|
---|
70 | {
|
---|
71 | // If the output is continous and we are going to output something
|
---|
72 | // first jump back to the beginning of the line (well, that
|
---|
73 | // doesn't work well if the input line is already two lines)
|
---|
74 | // and then flush the buffer.
|
---|
75 | if (fContinous && fLogO.GetSizeBacklog()>0)
|
---|
76 | {
|
---|
77 | std::cout << "\r";
|
---|
78 | fLogO.Display(true);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // Call Readline's EventHook to update the prompt
|
---|
82 | Readline::EventHook();
|
---|
83 | }
|
---|
84 |
|
---|
85 | string Console::GetLinePrompt() const
|
---|
86 | {
|
---|
87 | const string siz = fLogO.GetSizeStr();
|
---|
88 | return fContinous ?
|
---|
89 | Form("[%d]", GetLine()) : Form("[%d:%s]", GetLine(), siz.c_str());
|
---|
90 | }
|
---|
91 |
|
---|
92 | /*
|
---|
93 | // --------------------------------------------------------------------------
|
---|
94 | //
|
---|
95 | //! Flush the buffer before it is filled with contents produced by the
|
---|
96 | //! command processing. This keeps things as seperated as possible,
|
---|
97 | //! although there is no gurantee.
|
---|
98 | //
|
---|
99 | void Console::Shutdown(const char *)
|
---|
100 | {
|
---|
101 | // Flush the buffer (after readline() returned, before processing commands)
|
---|
102 | fLog.Display(true);
|
---|
103 | // std::cout << std::endl;
|
---|
104 | }
|
---|
105 | */
|
---|
106 |
|
---|
107 | // --------------------------------------------------------------------------
|
---|
108 | //
|
---|
109 | //! Before Readline::Run() is called the buffer is flushed as well as
|
---|
110 | //! after the Run() loop has exited.
|
---|
111 | //! command processing. This keeps things as seperated as possible,
|
---|
112 | //! although there is no gurantee.
|
---|
113 | //
|
---|
114 | void Console::Run(const char *)
|
---|
115 | {
|
---|
116 | // Flush the buffer before we stat out readline loop
|
---|
117 | fLogI.Display(true);
|
---|
118 | fLogO.Display(true);
|
---|
119 |
|
---|
120 | // Now run readlines main loop
|
---|
121 | Readline::Run();
|
---|
122 |
|
---|
123 | // flush buffer to display
|
---|
124 | fLogO.Display(true);
|
---|
125 | fLogI.Display(true);
|
---|
126 | }
|
---|