Editor.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Termlib_Editor_
18 #define HH_SENF_Utils_Termlib_Editor_ 1
19 
20 // Custom includes
21 #include <map>
22 #include <vector>
23 #include <string>
26 #include "AbstractTerminal.hh"
27 #include "Terminfo.hh"
28 
29 //#include "Editor.mpp"
30 //-/////////////////////////////////////////////////////////////////////////////////////////////////
31 
32 namespace senf {
33 namespace term {
34 
51  class BaseEditor
53  {
54  public:
56 
57  static unsigned const DEFAULT_KEY_TIMEOUT_MS = 500u;
58 
59  explicit BaseEditor(AbstractTerminal & terminal);
60 
61  void newline();
62  void toColumn(unsigned c);
63  void put(char ch);
64  void put(std::string const & text);
65  void clearLine();
66  void setBold();
67  void setNormal();
68  void maybeClrScr();
69 
70  void toLine(unsigned l);
71  void reset();
72 
73  unsigned currentColumn() const;
74  unsigned currentLine() const;
75  unsigned width() const;
76  unsigned height() const;
77 
78  protected:
79  virtual bool cb_init();
80  virtual void cb_windowSizeChanged();
82 
83 #ifndef DOXYGEN
84  private:
85 #endif
86  virtual void v_keyReceived(keycode_t key) = 0;
87 
88  private:
89  virtual void cb_charReceived(char c);
90 
91  void keySequenceTimeout();
92  void processKeys();
93 
94  void write(char ch);
95  void write(std::string const & s);
96 
97  AbstractTerminal * terminal_;
98  Terminfo tifo_;
99  KeyParser keyParser_;
100  std::string inputBuffer_;
101  ClockService::clock_type keyTimeout_;
102  scheduler::TimerEvent timer_;
103  unsigned column_;
104  unsigned displayHeight_;
105  unsigned line_;
106  };
107 
203  : public BaseEditor
204  {
205  public:
206  //-////////////////////////////////////////////////////////////////////////
207  // Types
208 
209  typedef boost::function<void (LineEditor&)> KeyBinding;
210  typedef boost::function<void (std::string const &)> AcceptCallback;
212 
213  static unsigned const MAX_HISTORY_SIZE = 1024u;
214 
215  //-////////////////////////////////////////////////////////////////////////
216 
217  LineEditor(AbstractTerminal & terminal, AcceptCallback cb);
219 
223  //-////////////////////////////////////////////////////////////////////////
224 
226  //\{
227 
228  void show();
229  void hide();
230  void accept();
231  void clear();
232  void redisplay();
233  void forceRedisplay();
234  void prompt(std::string const & text);
235 
236  //\}
237 
239  //\{
240 
241  void gotoChar(unsigned n);
242  void scrollTo(unsigned n);
243 
244  //\}
245 
247  //\{
248 
249  void deleteChar(unsigned n=1);
250  void insert(char ch);
251  void insert(std::string const & text);
252  void set(std::string const & text, unsigned pos = 0u);
254 
258  //\}
259 
261  //\{
262 
263  void pushHistory(std::string const & text, bool accept = false);
265  void prevHistory();
266  void nextHistory();
267 
268  //\}
269 
271  //\{
272 
273  void auxDisplay(unsigned line, std::string const & text);
275  unsigned maxAuxDisplayHeight();
276  void clearAuxDisplay();
277 
278  //\}
279 
281  //\{
282 
283  std::string const & text();
284  unsigned point();
285  unsigned displayPos();
286  keycode_t lastKey();
287 
288  //\}
289 
291  //\{
292 
293  void defineKey(keycode_t key, KeyBinding binding);
295  void unsetKey(keycode_t key);
296 
297  //\}
298 
299  private:
300  virtual bool cb_init();
301  virtual void cb_windowSizeChanged();
302  virtual void v_keyReceived(keycode_t key);
303 
304  typedef std::map<keycode_t, KeyBinding> KeyMap;
305  typedef std::vector<std::string> History;
306  typedef std::vector<std::string> AuxDisplay;
307 
308  bool enabled_;
309  bool redisplayNeeded_;
310  std::string prompt_;
311  unsigned promptWidth_;
312  unsigned editWidth_;
313  std::string text_;
314  unsigned point_;
315  unsigned displayPos_;
316  keycode_t lastKey_;
317  AcceptCallback callback_;
318  KeyMap bindings_;
319  History history_;
320  unsigned historyPoint_;
321  AuxDisplay auxDisplay_;
322  };
323 
326 namespace bindings {
327 
328  void selfInsertCommand (LineEditor & editor);
329  void forwardChar (LineEditor & editor);
330  void backwardChar (LineEditor & editor);
331  void accept (LineEditor & editor);
332  void acceptWithRepeat (LineEditor & editor);
333  void backwardDeleteChar (LineEditor & editor);
334  void deleteChar (LineEditor & editor);
335  void beginningOfLine (LineEditor & editor);
336  void endOfLine (LineEditor & editor);
337  void deleteToEndOfLine (LineEditor & editor);
338  void restartEdit (LineEditor & editor);
339  void prevHistory (LineEditor & editor);
340  void nextHistory (LineEditor & editor);
341  void clearScreen (LineEditor & editor);
342 
343  typedef boost::function<void (LineEditor &, unsigned & b, unsigned & e,
344  std::string & prefix, std::vector<std::string> &)> Completer;
345  void complete (LineEditor & editor, Completer completer);
347 
354 }
355 
356 }}
357 
358 //-/////////////////////////////////////////////////////////////////////////////////////////////////
359 //#include "Editor.cci"
360 //#include "Editor.ct"
361 //#include "Editor.cti"
362 #endif
363 
364 
365 // Local Variables:
366 // mode: c++
367 // fill-column: 100
368 // comment-column: 40
369 // c-file-style: "senf"
370 // indent-tabs-mode: nil
371 // ispell-local-dictionary: "american"
372 // compile-command: "scons -u test"
373 // End:
unsigned currentLine() const
Return number of current relative line.
Definition: Editor.cc:184
config::time_type clock_type
Provide editor support terminal functionality.
Definition: Editor.hh:51
void acceptWithRepeat(LineEditor &editor)
Accept, possibly repeat last history entry.
Definition: Editor.cc:577
wchar_t keycode_t
Key code data type.
Definition: Terminfo.hh:263
boost::function< void(LineEditor &, unsigned &b, unsigned &e, std::string &prefix, std::vector< std::string > &)> Completer
Definition: Editor.hh:344
unsigned height() const
Return current screen height.
Definition: Editor.cc:252
void selfInsertCommand(LineEditor &editor)
Insert key as literal character.
Definition: Editor.cc:553
virtual void cb_windowSizeChanged()
Called whenever the terminal window size changes.
Definition: Editor.cc:218
void nextHistory(LineEditor &editor)
Move to next history entry.
Definition: Editor.cc:627
void clearLine()
Clear current line and move cursor to first column.
Definition: Editor.cc:98
Parse key escape sequences.
Definition: Terminfo.hh:236
void restartEdit(LineEditor &editor)
Clear edit buffer and restart edit.
Definition: Editor.cc:615
void toLine(unsigned l)
Move to relative display line l.
Definition: Editor.cc:125
void backwardChar(LineEditor &editor)
Move one char backwards.
Definition: Editor.cc:565
static unsigned const DEFAULT_KEY_TIMEOUT_MS
Definition: Editor.hh:57
BaseEditor(AbstractTerminal &terminal)
Definition: Editor.cc:28
virtual void v_keyReceived(keycode_t key)=0
Called whenever a key is received.
void accept(LineEditor &editor)
Accept input line.
Definition: Editor.cc:572
void complete(LineEditor &editor, Completer completer)
Complete text at cursor.
Definition: Editor.cc:639
void deleteToEndOfLine(LineEditor &editor)
Delete from cursor to end of line.
Definition: Editor.cc:610
void backwardDeleteChar(LineEditor &editor)
Delete char before cursor.
Definition: Editor.cc:586
virtual bool cb_init()
Called when terminal is initialized.
Definition: Editor.cc:190
void forwardChar(LineEditor &editor)
Move one char forward.
Definition: Editor.cc:560
void put(char ch)
Write ch at current column.
Definition: Editor.cc:78
Terminfo public header.
boost::function< void(std::string const &)> AcceptCallback
Callback function type.
Definition: Editor.hh:210
void prevHistory(LineEditor &editor)
Move to previous history entry.
Definition: Editor.cc:622
Terminfo database entry.
Definition: Terminfo.hh:55
void setNormal()
Set normal char display.
Definition: Editor.cc:112
unsigned currentColumn() const
Return number of current column.
Definition: Editor.cc:178
boost::function< void(LineEditor &)> KeyBinding
Type of a key binding function.
Definition: Editor.hh:209
Terminal public header.
void beginningOfLine(LineEditor &editor)
Move to beginning of line.
Definition: Editor.cc:600
Single line interactive text editor.
Definition: Editor.hh:202
void newline()
Move to beginning of a new, empty line.
Definition: Editor.cc:38
void clearScreen(LineEditor &editor)
Clear screen and redisplay editor.
Definition: Editor.cc:632
void maybeClrScr()
Clear screen if possible.
Definition: Editor.cc:119
void reset()
Reset display area to single line.
Definition: Editor.cc:168
KeyParser::keycode_t keycode_t
Definition: Editor.hh:55
void deleteChar(LineEditor &editor)
Delete char at cursor.
Definition: Editor.cc:595
::phoenix::function< detail::clear > const clear
void setBold()
Set bold char display.
Definition: Editor.cc:105
void toColumn(unsigned c)
Move cursor to column c.
Definition: Editor.cc:46
unsigned width() const
Return current screen width.
Definition: Editor.cc:246
Abstract terminal interface.
void endOfLine(LineEditor &editor)
Move to end of line.
Definition: Editor.cc:605