CLI  2.9
ui_line.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/ui_line.h"
31 #include "cli/shell.h"
32 #include "command_line_edition.h"
33 
34 
36 
37  CLI_NS_BEGIN(ui)
38 
39  Line::Line(const tk::String& TK_DefaultLine, const int I_MinLineLength, const int I_MaxLineLength)
40  : UI(),
41  m_tkDefaultLine(TK_DefaultLine), m_iMinLineLength(I_MinLineLength), m_iMaxLineLength(I_MaxLineLength),
42  m_cliLine(* new CmdLineEdition()), m_bResetOnTyping(false)
43  {
44  }
45 
46  Line::Line(ExecutionContext& CLI_ParentContext, const tk::String& TK_DefaultLine, const int I_MinLineLength, const int I_MaxLineLength)
47  : UI(CLI_ParentContext),
48  m_tkDefaultLine(TK_DefaultLine), m_iMinLineLength(I_MinLineLength), m_iMaxLineLength(I_MaxLineLength),
49  m_cliLine(* new CmdLineEdition()), m_bResetOnTyping(false)
50  {
51  }
52 
54  {
55  delete & m_cliLine;
56  }
57 
58  const tk::String Line::GetLine(void) const
59  {
60  return m_cliLine.GetLine();
61  }
62 
63  void Line::SetLine(const tk::String& TK_Line, const bool B_NewLine, const bool B_ResetOnTyping)
64  {
65  const OutputDevice& ECHO = GetStream(ECHO_STREAM);
66 
67  m_cliLine.CleanAll(ECHO);
68  m_cliLine.Put(ECHO, TK_Line);
69  m_bResetOnTyping = B_ResetOnTyping;
70  if (B_NewLine)
71  {
72  m_cliLine.NextLine(ECHO);
73  }
74  }
75 
76  void Line::Reset(void)
77  {
78  m_cliLine.Reset();
79  }
80 
82  {
83  SetLine(m_tkDefaultLine, false, true);
84  }
85 
86  void Line::OnKey(const KEY E_KeyCode)
87  {
88  const OutputDevice& ECHO = GetStream(ECHO_STREAM);
89 
90  switch (E_KeyCode)
91  {
92  case NULL_KEY:
93  EndControl(false);
94  break;
95  //case KEY_UP:
96  //case KEY_DOWN:
97  //case PAGE_UP:
98  //case PAGE_DOWN:
99  case KEY_BEGIN:
100  m_cliLine.Home(ECHO);
101  m_bResetOnTyping = false;
102  break;
103  case KEY_END:
104  m_cliLine.End(ECHO);
105  m_bResetOnTyping = false;
106  break;
107  case KEY_LEFT:
108  if (! m_cliLine.GetLeft().IsEmpty())
109  {
110  m_cliLine.MoveCursor(ECHO, -1);
111  m_bResetOnTyping = false;
112  }
113  else
114  {
115  Beep();
116  }
117  break;
118  case KEY_RIGHT:
119  if (! m_cliLine.GetRight().IsEmpty())
120  {
121  m_cliLine.MoveCursor(ECHO, 1);
122  m_bResetOnTyping = false;
123  }
124  else
125  {
126  Beep();
127  }
128  break;
129  case BACKSPACE:
130  if (! m_cliLine.GetLeft().IsEmpty())
131  {
132  m_cliLine.Delete(ECHO, -1);
133  m_bResetOnTyping = false;
134  }
135  else
136  {
137  Beep();
138  }
139  break;
140  case DELETE:
141  if (! m_cliLine.GetRight().IsEmpty())
142  {
143  m_cliLine.Delete(ECHO, 1);
144  m_bResetOnTyping = false;
145  }
146  else
147  {
148  Beep();
149  }
150  break;
151  case ENTER:
152  if (((m_iMinLineLength < 0) || (m_cliLine.GetLine().GetLength() >= (unsigned int) m_iMinLineLength))
153  && ((m_iMaxLineLength < 0) || (m_cliLine.GetLine().GetLength() <= (unsigned int) m_iMaxLineLength)))
154  {
155  m_cliLine.NextLine(ECHO);
156  EndControl(true);
157  }
158  else
159  {
160  Beep();
161  }
162  break;
163  case BREAK:
164  case ESCAPE:
165  case LOGOUT:
166  EndControl(false);
167  break;
168  case TAB: Beep(); break;
169  case CLS: m_cliLine.CleanAll(ECHO); m_bResetOnTyping = false; break;
170 
171  case QUESTION:
172  case KEY_a: case KEY_aacute: case KEY_agrave: case KEY_auml: case KEY_acirc:
173  case KEY_b: case KEY_c: case KEY_ccedil: case KEY_d:
174  case KEY_e: case KEY_eacute: case KEY_egrave: case KEY_euml: case KEY_ecirc:
175  case KEY_f: case KEY_g: case KEY_h:
176  case KEY_i: case KEY_iacute: case KEY_igrave: case KEY_iuml: case KEY_icirc:
177  case KEY_j: case KEY_k: case KEY_l: case KEY_m: case KEY_n:
178  case KEY_o: case KEY_oacute: case KEY_ograve: case KEY_ouml: case KEY_ocirc:
179  case KEY_p: case KEY_q: case KEY_r: case KEY_s: case KEY_t:
180  case KEY_u: case KEY_uacute: case KEY_ugrave: case KEY_uuml: case KEY_ucirc:
181  case KEY_v: case KEY_w: case KEY_x: case KEY_y: case KEY_z:
182 
183  case KEY_A: case KEY_B: case KEY_C: case KEY_D: case KEY_E: case KEY_F:
184  case KEY_G: case KEY_H: case KEY_I: case KEY_J: case KEY_K: case KEY_L:
185  case KEY_M: case KEY_N: case KEY_O: case KEY_P: case KEY_Q: case KEY_R:
186  case KEY_S: case KEY_T: case KEY_U: case KEY_V: case KEY_W: case KEY_X:
187  case KEY_Y: case KEY_Z:
188 
189  case KEY_0: case KEY_1: case KEY_2: case KEY_3: case KEY_4: case KEY_5:
190  case KEY_6: case KEY_7: case KEY_8: case KEY_9:
191 
192  case PLUS:
193  case MINUS:
194  case STAR:
195  case SLASH:
196  case LOWER_THAN:
197  case GREATER_THAN:
198  case EQUAL:
199  case PERCENT:
200 
201  case SPACE:
202  case UNDERSCORE:
203  case AROBASE:
204  case SHARP:
205  case AMPERCENT:
206  case DOLLAR:
207  case BACKSLASH:
208  case PIPE:
209  case TILDE:
210  case SQUARE:
211  case EURO:
212  case POUND:
213  case MICRO:
214  case PARAGRAPH:
215 
216  case EXCLAMATION:
217  case COLUMN:
218  case DOT:
219  case COMA:
220  case SEMI_COLUMN:
221  case QUOTE:
222  case DOUBLE_QUOTE:
223 
224  case OPENING_BRACE:
225  case CLOSING_BRACE:
226  case OPENING_CURLY_BRACE:
227  case CLOSING_CURLY_BRACE:
228  case OPENING_BRACKET:
229  case CLOSING_BRACKET:
230  if (m_bResetOnTyping)
231  {
232  m_cliLine.CleanAll(ECHO);
233  m_bResetOnTyping = false;
234  }
235  if ((m_iMaxLineLength < 0) || (! m_cliLine.GetInsertMode())
236  || (m_cliLine.GetLine().GetLength() < (unsigned int) m_iMaxLineLength))
237  {
238  m_cliLine.Put(ECHO, E_KeyCode);
239  }
240  else
241  {
242  Beep();
243  }
244  break;
245 
246  case INSERT:
247  m_cliLine.SetInsertMode(! m_cliLine.GetInsertMode());
248  break;
249 
250  default:
251  // Non managed character. Just ignore.
252  break;
253  }
254  }
255 
256  CLI_NS_END(ui)
257 
259 
void NextLine(const OutputDevice &CLI_OutputDevice)
Moves the cursor to the next line.
Right arrow key.
Definition: io_device.h:194
Main namespace of the CLI library.
const tk::String GetLine(void) const
Line retrieval.
Definition: ui_line.cpp:58
Shell class definition.
Echo stream.
Definition: exec_context.h:55
Based on utf-8 encoding for &#39;§&#39; (changed in version 2.9)
Definition: io_device.h:170
const tk::String GetLeft(void) const
Left part of the command line accessor.
Based on utf-8 encoding for &#39;à&#39; (changed in version 2.9)
Definition: io_device.h:76
Based on utf-8 encoding for &#39;€&#39; (changed in version 2.9)
Definition: io_device.h:167
Based on utf-8 encoding for &#39;í&#39; (changed in version 2.9)
Definition: io_device.h:92
Simple line user interface object.
Definition: ui_line.h:47
Based on utf-8 encoding for &#39;ì&#39; (changed in version 2.9)
Definition: io_device.h:93
const bool GetInsertMode(void) const
Insert mode retrieval.
void SetLine(const tk::String &TK_Line, const bool B_NewLine, const bool B_CleanOnTyping)
Protected line setter for child classes.
Definition: ui_line.cpp:63
Based on utf-8 encoding for &#39;ç&#39; (changed in version 2.9)
Definition: io_device.h:81
Based on utf-8 encoding for &#39;ü&#39; (changed in version 2.9)
Definition: io_device.h:114
End key.
Definition: io_device.h:201
#define CLI_NS_END(__ns)
End a namespace definition.
Definition: namespace.h:45
void EndControl(const bool B_ExecResult)
Method to call by child classes in order to end the control execution.
Definition: ui.cpp:79
CLI classes toolkit.
Definition: tk_stl.h:51
Based on utf-8 encoding for &#39;£&#39; (changed in version 2.9)
Definition: io_device.h:168
Based on utf-8 encoding for &#39;á&#39; (changed in version 2.9)
Definition: io_device.h:75
Based on utf-8 encoding for &#39;ô&#39; (changed in version 2.9)
Definition: io_device.h:105
void MoveCursor(const OutputDevice &CLI_OutputDevice, const int I_Count)
Moves the cursor.
const tk::String GetLine(void) const
Current command line.
Based on utf-8 encoding for &#39;ï&#39; (changed in version 2.9)
Definition: io_device.h:94
void CleanAll(const OutputDevice &CLI_OutputDevice)
Line deletion.
virtual ~Line(void)
Destructor.
Definition: ui_line.cpp:53
const tk::String GetRight(void) const
Right part of the command line accessor.
Execution context.
Definition: exec_context.h:149
CmdLineEdition class definition.
Space.
Definition: io_device.h:57
void Beep(void)
Sends a beep signal.
Based on utf-8 encoding for &#39;ü&#39; (changed in version 2.9)
Definition: io_device.h:86
CLI library default pre-compiled header.
Begin key.
Definition: io_device.h:200
Generic output device.
Definition: io_device.h:256
Line(const tk::String &TK_DefaultLine, const int I_MinLineLength, const int I_MaxLineLength)
Top execution context constructor.
Definition: ui_line.cpp:39
Based on utf-8 encoding for &#39;ä&#39; (changed in version 2.9)
Definition: io_device.h:77
void SetInsertMode(const bool B_InsertMode)
Insert mode setting.
Clean screen key (changed from 129 to 501 in order to avoid overlap with printable ASCII characters)...
Definition: io_device.h:60
Based on utf-8 encoding for &#39;ò&#39; (changed in version 2.9)
Definition: io_device.h:103
Based on utf-8 encoding for &#39;µ&#39; (changed in version 2.9)
Definition: io_device.h:169
virtual void Reset(void)
Handler called when data reset is required.
Definition: ui_line.cpp:76
enum _KEY KEY
Input keys.
#define CLI_NS_BEGIN(__ns)
Begin a namespace definition.
Definition: namespace.h:38
Based on utf-8 encoding for &#39;û&#39; (changed in version 2.9)
Definition: io_device.h:115
Based on utf-8 encoding for &#39;â&#39; (changed in version 2.9)
Definition: io_device.h:78
Based on utf-8 encoding for &#39;é&#39; (changed in version 2.9)
Definition: io_device.h:84
Escape.
Definition: io_device.h:56
Based on utf-8 encoding for &#39;ù&#39; (changed in version 2.9)
Definition: io_device.h:113
void Put(const OutputDevice &CLI_OutputDevice, const KEY E_Char)
Character addition.
Based on utf-8 encoding for &#39;²&#39; (changed in version 2.9)
Definition: io_device.h:166
Backspace (changed from &#39;\b&#39; to 8 in version 2.7 for ASCII compliance).
Definition: io_device.h:58
Based on utf-8 encoding for &#39;è&#39; (changed in version 2.9)
Definition: io_device.h:85
Left arrow key.
Definition: io_device.h:193
Based on utf-8 encoding for &#39;ú&#39; (changed in version 2.9)
Definition: io_device.h:112
Enter.
Definition: io_device.h:55
void Home(const OutputDevice &CLI_OutputDevice)
Moves the cursor at the beginning of the line.
void Delete(const OutputDevice &CLI_OutputDevice, const int I_Count)
Character deletion.
Command line edition objet.
Break (Ctrl+C).
Definition: io_device.h:53
Tab key.
Definition: io_device.h:62
Logout (Ctrl+D).
Definition: io_device.h:54
Based on utf-8 encoding for &#39;î&#39; (changed in version 2.9)
Definition: io_device.h:95
virtual void OnKey(const KEY E_KeyCode)
Handler called on character input.
Definition: ui_line.cpp:86
Line class definition.
Based on utf-8 encoding for &#39;û&#39; (changed in version 2.9)
Definition: io_device.h:87
Generic user interface class.
Definition: ui.h:60
void End(const OutputDevice &CLI_OutputDevice)
Moves the cursor at the end of the line.
void Reset(void)
Reset the object.
const OutputDevice & GetStream(const STREAM_TYPE E_StreamType) const
Output stream accessor.
Insert key (changed from 500 to 502 in order to avoid overlap with printable ASCII characters)...
Definition: io_device.h:61
Based on utf-8 encoding for &#39;ö&#39; (changed in version 2.9)
Definition: io_device.h:104
Based on utf-8 encoding for &#39;ó&#39; (changed in version 2.9)
Definition: io_device.h:102
Null key.
Definition: io_device.h:51
virtual void ResetToDefault(void)
Handler called when default value is required to be restored.
Definition: ui_line.cpp:81
Delete key (changed from 128 to 127 in version 2.7 for ASCII compliance).
Definition: io_device.h:59