CLI  2.9
ui_more.cpp
1 /*
2  Copyright (c) 2006-2018, Alexis Royer, http://alexis.royer.free.fr/CLI
3 
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of the CLI library project nor the names of its contributors may be used to endorse or promote products derived from this software
12  without specific prior written permission.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 
28 #include "cli/pch.h"
29 
30 #include "cli/assert.h"
31 #include "cli/ui_more.h"
32 #include "cli/shell.h"
33 #include "cli/string_device.h"
34 #include "ui_text.h"
35 #include "command_line_edition.h"
36 
37 
39 
40  CLI_NS_BEGIN(ui)
41 
42  More::More(const unsigned int UI_MaxLines, const unsigned int UI_MaxLineLength)
43  : UI(),
44  m_uiText(* new Text(UI_MaxLines, UI_MaxLineLength)), m_puiTextIt(NULL),
45  m_cliMoreLine(* new CmdLineEdition())
46  {
47  }
48 
49  More::More(ExecutionContext& CLI_ParentContext, const unsigned int UI_MaxLines, const unsigned int UI_MaxLineLength)
50  : UI(CLI_ParentContext),
51  m_uiText(* new Text(UI_MaxLines, UI_MaxLineLength)), m_puiTextIt(NULL),
52  m_cliMoreLine(* new CmdLineEdition())
53  {
54  }
55 
57  {
58  delete & m_uiText;
59  if (m_puiTextIt != NULL)
60  {
61  delete m_puiTextIt;
62  m_puiTextIt = NULL;
63  }
64  delete & m_cliMoreLine;
65  }
66 
68  {
69  return m_uiText;
70  }
71 
72  void More::Reset(void)
73  {
74  // Nothing to do.
75  }
76 
78  {
79  // Very first display.
81  if (m_puiTextIt == NULL)
82  {
83  m_puiTextIt = new TextIterator(cli_ScreenInfo, cli_ScreenInfo.GetSafeHeight() - 1);
84  }
85  CLI_ASSERT(m_puiTextIt != NULL);
86  if (m_puiTextIt != NULL)
87  {
88  m_uiText.Begin(*m_puiTextIt);
89  const StringDevice cli_Out(cli_ScreenInfo.GetSafeHeight() * (cli_ScreenInfo.GetSafeWidth() + 1), false);
90  m_uiText.PrintPage(*m_puiTextIt, cli_Out, false);
91  GetStream(OUTPUT_STREAM) << cli_Out.GetString();
92  }
93  ShowMoreMessage();
94  }
95 
96  void More::OnKey(const KEY E_KeyCode)
97  {
98  // Ensure m_puiTextIt is valid.
99  CLI_ASSERT(m_puiTextIt != NULL);
100  if (m_puiTextIt == NULL) { Quit(); }
101  else {
102  switch (E_KeyCode)
103  {
104  case KEY_END:
105  HideMoreMessage();
106  // We are already at the bottom of a page, no need to optimize display so far, let it progress fluently.
107  // Optimize line by line at least.
108  for (bool b_LineDown = true; b_LineDown; )
109  {
111  const StringDevice cli_Out(cli_ScreenInfo.GetSafeHeight() * (cli_ScreenInfo.GetSafeWidth() + 1), false);
112  b_LineDown = m_uiText.LineDown(*m_puiTextIt, & cli_Out);
113  GetStream(OUTPUT_STREAM) << cli_Out.GetString();
114  }
115  ShowMoreMessage();
116  break;
117  case PAGE_DOWN:
118  case SPACE:
119  // Print one more page
120  HideMoreMessage();
121  do {
122  // Output lines to a buffer output device.
124  const StringDevice cli_Out(cli_ScreenInfo.GetSafeHeight() * (cli_ScreenInfo.GetSafeWidth() + 1), false);
125  m_uiText.PageDown(*m_puiTextIt, & cli_Out);
126  // Display in one call in order to optimize display.
127  GetStream(OUTPUT_STREAM) << cli_Out.GetString();
128  } while(0);
129  ShowMoreMessage();
130  break;
131  case KEY_DOWN:
132  case ENTER:
133  // Print one more line
134  HideMoreMessage();
135  m_uiText.LineDown(*m_puiTextIt, & GetStream(OUTPUT_STREAM));
136  ShowMoreMessage();
137  break;
138  case KEY_q:
139  case KEY_Q:
140  case ESCAPE:
141  case BREAK:
142  case LOGOUT:
143  case NULL_KEY:
144  // Stop display
145  Quit();
146  break;
147  default:
148  // Non managed character. Beep.
149  Beep();
150  break;
151  }
152  }
153  }
154 
155  void More::ShowMoreMessage(void)
156  {
157  CLI_ASSERT(m_puiTextIt != NULL);
158  if (m_puiTextIt != NULL)
159  {
160  m_cliMoreLine.Reset();
161  TextIterator tmp(*m_puiTextIt);
162  if (m_uiText.LineDown(tmp, NULL))
163  {
164  // Still lines to display.
165  const ResourceString cli_MoreMessage = ResourceString()
166  .SetString(ResourceString::LANG_EN, "--- More ---")
167  .SetString(ResourceString::LANG_FR, "--- Plus ---");
168  m_cliMoreLine.Put(GetStream(OUTPUT_STREAM), cli_MoreMessage.GetString(GetLang()));
169  return;
170  }
171  }
172 
173  // If the input text is done (or something wrong occurred), terminate the UI execution.
174  Quit();
175  }
176 
177  void More::HideMoreMessage(void)
178  {
179  m_cliMoreLine.CleanAll(GetStream(OUTPUT_STREAM));
180  }
181 
182  void More::Quit(void)
183  {
184  HideMoreMessage();
185  EndControl(true);
186  }
187 
188  CLI_NS_END(ui)
189 
191 
Main namespace of the CLI library.
Shell class definition.
const tk::String GetString(const LANG E_Lang) const
Access to the string of a given language.
#define CLI_ASSERT(a)
CLI assertion macro.
Definition: assert.h:49
void Begin(TextIterator &it) const
Retrieves a text head iterator.
Definition: ui_text.cpp:105
const ResourceString::LANG GetLang(void) const
Language access.
String device definition.
const OutputDevice & GetText(void)
Text member accessor.
Definition: ui_more.cpp:67
Text class definition.
End key.
Definition: io_device.h:201
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
Simple line user interface object.
Definition: ui_more.h:52
void EndControl(const bool B_ExecResult)
Method to call by child classes in order to end the control execution.
Definition: ui.cpp:79
ResourceString class.
Output stream.
Definition: exec_context.h:56
Assertion facilities.
Screen information.
Definition: io_device.h:461
Page down arrow key.
Definition: io_device.h:196
const unsigned int GetSafeHeight(void) const
Safe screen height accessor.
Definition: io_device.h:520
void CleanAll(const OutputDevice &CLI_OutputDevice)
Line deletion.
String device.
Definition: string_device.h:46
More(const unsigned int UI_MaxLines, const unsigned int UI_MaxLineLength)
Top execution context constructor.
Definition: ui_more.cpp:42
Execution context.
Definition: exec_context.h:149
CmdLineEdition class definition.
Space.
Definition: io_device.h:57
void Beep(void)
Sends a beep signal.
CLI library default pre-compiled header.
Generic output device.
Definition: io_device.h:256
virtual const ScreenInfo GetScreenInfo(void) const
Screen info accessor.
enum _KEY KEY
Input keys.
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
const bool LineDown(TextIterator &it, const OutputDevice *const PCLI_Out) const
Moves iterator one line down.
Definition: ui_text.cpp:177
More class definition.
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_more.cpp:96
const unsigned int GetSafeWidth(void) const
Safe screen width accessor.
Definition: io_device.h:514
Escape.
Definition: io_device.h:56
void Put(const OutputDevice &CLI_OutputDevice, const KEY E_Char)
Character addition.
Enter.
Definition: io_device.h:55
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_more.cpp:77
ResourceString & SetString(const LANG E_Lang, const char *const STR_String)
Adds resource for a given language.
Down arrow key.
Definition: io_device.h:192
Command line edition objet.
virtual ~More(void)
Destructor.
Definition: ui_more.cpp:56
virtual void Reset(void)
Handler called when data reset is required.
Definition: ui_more.cpp:72
Break (Ctrl+C).
Definition: io_device.h:53
const bool PageDown(TextIterator &it, const OutputDevice *const PCLI_Out) const
Moves iterator one page down.
Definition: ui_text.cpp:220
Logout (Ctrl+D).
Definition: io_device.h:54
Text iterator class.
Definition: ui_text.h:145
Generic user interface class.
Definition: ui.h:60
void Reset(void)
Reset the object.
const OutputDevice & GetStream(const STREAM_TYPE E_StreamType) const
Output stream accessor.
Null key.
Definition: io_device.h:51
const tk::String & GetString(void) const
String accessor.
Simple line user interface object.
Definition: ui_text.h:47
void PrintPage(TextIterator &it, const OutputDevice &CLI_Out, const bool B_FillPageWithBlankLines) const
Print out a page of text.
Definition: ui_text.cpp:241