CLI  2.9
ui_less.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_less.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  Less::Less(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_cliLessLine(* new CmdLineEdition())
46  {
47  }
48 
49  Less::Less(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_cliLessLine(* 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_cliLessLine;
65  }
66 
68  {
69  return m_uiText;
70  }
71 
72  void Less::Reset(void)
73  {
74  // Nothing to do.
75  }
76 
78  {
79  // Very first display.
80  if (m_puiTextIt == NULL)
81  {
83  m_puiTextIt = new TextIterator(cli_ScreenInfo, cli_ScreenInfo.GetSafeHeight() - 1);
84  }
85  CLI_ASSERT(m_puiTextIt != NULL);
86  if (m_puiTextIt != NULL) { m_uiText.Begin(*m_puiTextIt); }
87  PrintScreen();
88  }
89 
90  void Less::OnKey(const KEY E_KeyCode)
91  {
92  // Ensure m_puiTextIt is valid.
93  CLI_ASSERT(m_puiTextIt != NULL);
94  if (m_puiTextIt == NULL) { Quit(); }
95  else {
96  switch (E_KeyCode)
97  {
98  case KEY_BEGIN:
99  m_uiText.Begin(*m_puiTextIt);
100  PrintScreen();
101  break;
102  case PAGE_UP:
103  if (m_uiText.PageUp(*m_puiTextIt)) PrintScreen();
104  else Beep();
105  break;
106  case KEY_UP:
107  if (m_uiText.LineUp(*m_puiTextIt)) PrintScreen();
108  else Beep();
109  break;
110  case KEY_DOWN:
111  case ENTER:
112  if (m_uiText.LineDown(*m_puiTextIt, NULL)) PrintScreen();
113  else Beep();
114  break;
115  case PAGE_DOWN:
116  case SPACE:
117  if (m_uiText.PageDown(*m_puiTextIt, NULL)) PrintScreen();
118  else Beep();
119  break;
120  case KEY_END:
121  m_uiText.End(*m_puiTextIt, NULL);
122  PrintScreen();
123  break;
124  case KEY_q:
125  case KEY_Q:
126  case ESCAPE:
127  case BREAK:
128  case LOGOUT:
129  case NULL_KEY:
130  // Stop display
131  Quit();
132  break;
133  default:
134  // Non managed character: beep.
135  Beep();
136  break;
137  }
138  }
139  }
140 
141  void Less::PrintScreen(void)
142  {
144 
145  // Then let current bottom position move one page down while printing the page.
146  const StringDevice cli_Out(cli_ScreenInfo.GetSafeHeight() * (cli_ScreenInfo.GetSafeWidth() + 1), false);
147  CLI_ASSERT(m_puiTextIt != NULL);
148  if (m_puiTextIt != NULL) { m_uiText.PrintPage(*m_puiTextIt, cli_Out, true); }
149 
150  // Eventually clean out the screen and print out the computed string.
151  m_cliLessLine.Reset();
153  GetStream(OUTPUT_STREAM) << cli_Out.GetString();
154  m_cliLessLine.Put(GetStream(OUTPUT_STREAM), COLUMN);
155  }
156 
157  void Less::Quit(void)
158  {
159  m_cliLessLine.CleanAll(GetStream(OUTPUT_STREAM));
160  EndControl(true);
161  }
162 
163  CLI_NS_END(ui)
164 
166 
Main namespace of the CLI library.
Shell class definition.
Less(const unsigned int UI_MaxLines, const unsigned int UI_MaxLineLength)
Top execution context constructor.
Definition: ui_less.cpp:42
#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
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_less.cpp:77
String device definition.
Text class definition.
End key.
Definition: io_device.h:201
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
virtual ~Less(void)
Destructor.
Definition: ui_less.cpp:56
void EndControl(const bool B_ExecResult)
Method to call by child classes in order to end the control execution.
Definition: ui.cpp:79
Page up arrow key.
Definition: io_device.h:195
Output stream.
Definition: exec_context.h:56
Assertion facilities.
Screen information.
Definition: io_device.h:461
Less class definition.
const bool PageUp(TextIterator &it) const
Moves iterator one page up.
Definition: ui_text.cpp:113
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
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.
Begin key.
Definition: io_device.h:200
Generic output device.
Definition: io_device.h:256
virtual void CleanScreen(void) const
Clean screen.
virtual const ScreenInfo GetScreenInfo(void) const
Screen info accessor.
void End(TextIterator &it, const OutputDevice *const PCLI_Out) const
Retrieves a text end iterator.
Definition: ui_text.cpp:235
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
Simple line user interface object.
Definition: ui_less.h:52
const bool LineUp(TextIterator &it) const
Moves iterator one line up.
Definition: ui_text.cpp:128
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
Down arrow key.
Definition: io_device.h:192
Command line edition objet.
Break (Ctrl+C).
Definition: io_device.h:53
virtual void Reset(void)
Handler called when data reset is required.
Definition: ui_less.cpp:72
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.
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_less.cpp:90
const OutputDevice & GetText(void)
Text member accessor.
Definition: ui_less.cpp:67
Null key.
Definition: io_device.h:51
Simple line user interface object.
Definition: ui_text.h:47
Up arrow key.
Definition: io_device.h:191
void PrintPage(TextIterator &it, const OutputDevice &CLI_Out, const bool B_FillPageWithBlankLines) const
Print out a page of text.
Definition: ui_text.cpp:241