Utils/Termlib: Implement LineEditor auxiliary display support
[senf.git] / Utils / Termlib / Editor.hh
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Editor public header */
25
26 #ifndef HH_SENF_Utils_Termlib_Editor_
27 #define HH_SENF_Utils_Termlib_Editor_ 1
28
29 // Custom includes
30 #include <map>
31 #include <vector>
32 #include <string>
33 #include <senf/Scheduler/ClockService.hh>
34 #include <senf/Scheduler/TimerEvent.hh>
35 #include "AbstractTerminal.hh"
36 #include "Terminfo.hh"
37
38 //#include "Editor.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
40
41 namespace senf {
42 namespace term {
43
44     class BaseEditor
45         : public AbstractTerminal::Callbacks
46     {
47     public:
48         typedef KeyParser::keycode_t keycode_t;
49
50         static unsigned const DEFAULT_KEY_TIMEOUT_MS = 500u;
51
52         explicit BaseEditor(AbstractTerminal & terminal);
53
54         void newline();                 ///< Move to beginning of a new, empty line
55         void toColumn(unsigned c);      ///< Move cursor to column \p c
56         void put(char ch);              ///< Write \p ch at current column
57         void put(std::string const & text); ///< Write \a text starting at current column
58         void clearLine();               ///< Clear current line and move cursor to first column
59         void setBold();                 ///< Set bold char display
60         void setNormal();               ///< Set normal char display
61         void maybeClrScr();             ///< Clear screen if possible
62
63         void toLine(unsigned l);        ///< Move to relative display line \a l
64         void reset();                   ///< Reset display area to single line
65
66         unsigned currentColumn() const; ///< Return number of current column
67         unsigned currentLine() const;   ///< Return number of current relative line
68         unsigned width();               ///< Return current screen width
69         unsigned height();              ///< Return current screen height
70
71     protected:
72         virtual bool cb_init();
73         virtual void cb_windowSizeChanged();
74
75     private:
76         virtual void v_keyReceived(keycode_t key) = 0;
77
78         virtual void cb_charReceived(char c);
79
80         void keySequenceTimeout();
81         void processKeys();
82
83         void write(char ch);
84         void write(std::string const & s);
85         
86         AbstractTerminal * terminal_;
87         Terminfo tifo_;
88         KeyParser keyParser_;
89         std::string inputBuffer_;
90         ClockService::clock_type keyTimeout_;
91         scheduler::TimerEvent timer_;
92         unsigned column_;
93         unsigned displayHeight_;
94         unsigned line_;
95     };
96
97     class LineEditor
98         : public BaseEditor
99     {
100     public:
101         ///////////////////////////////////////////////////////////////////////////
102         // Types
103
104         typedef boost::function<void (LineEditor&)> KeyBinding;
105         typedef boost::function<void (std::string const &)> AcceptCallback;
106
107         static unsigned const MAX_HISTORY_SIZE = 1024u;
108
109         ///////////////////////////////////////////////////////////////////////////
110
111         LineEditor(AbstractTerminal & terminal, AcceptCallback cb);
112         
113         ///////////////////////////////////////////////////////////////////////////
114
115         void prompt(std::string const & text);
116         void set(std::string const & text, unsigned pos = 0u);
117
118         // Overall edit control
119         void show();
120         void hide();
121         void accept();
122         void clear();
123         void redisplay();
124         void forceRedisplay();
125         
126         // Cursor and display movement
127         void gotoChar(unsigned n);
128         void scrollTo(unsigned n);
129
130         // Text manipulation
131         void deleteChar(unsigned n=1);
132         void insert(char ch);
133         void insert(std::string const & text);
134
135         // History
136         void pushHistory(std::string const & text);
137         void prevHistory();
138         void nextHistory();
139
140         // Aux Display
141         void auxDisplay(int line, std::string const & text);
142         unsigned maxAuxDisplayHeight();
143         void clearAuxDisplay();
144
145         // Get information
146         std::string const & text();
147         unsigned point();
148         unsigned displayPos();
149         keycode_t lastKey();
150
151         // Key bindings
152         void defineKey(keycode_t key, KeyBinding binding);
153         void unsetKey(keycode_t key);
154         
155     private:
156         virtual bool cb_init();
157         virtual void cb_windowSizeChanged();
158         virtual void v_keyReceived(keycode_t key);
159
160         typedef std::map<keycode_t, KeyBinding> KeyMap;
161         typedef std::vector<std::string> History;
162
163         bool enabled_;
164         bool redisplayNeeded_;
165         std::string prompt_;
166         unsigned promptWidth_;
167         unsigned editWidth_;
168         std::string text_;
169         unsigned point_;
170         unsigned displayPos_;
171         keycode_t lastKey_;
172         AcceptCallback callback_;
173         KeyMap bindings_;
174         History history_;
175         unsigned historyPoint_;
176     };
177
178 namespace bindings {
179
180     void selfInsertCommand   (LineEditor & editor);
181     void forwardChar         (LineEditor & editor);
182     void backwardChar        (LineEditor & editor);
183     void accept              (LineEditor & editor);
184     void backwardDeleteChar  (LineEditor & editor);
185     void deleteChar          (LineEditor & editor);
186     void beginningOfLine     (LineEditor & editor);
187     void endOfLine           (LineEditor & editor);
188     void deleteToEndOfLine   (LineEditor & editor);
189     void restartEdit         (LineEditor & editor);
190     void prevHistory         (LineEditor & editor);
191     void nextHistory         (LineEditor & editor);
192     void clearScreen         (LineEditor & editor);
193
194     typedef boost::function<void (LineEditor &, unsigned b, unsigned e, std::vector<std::string> &)> Completer;
195     void complete            (LineEditor & editor, Completer completer);
196
197 }
198
199 }}
200
201 ///////////////////////////////hh.e////////////////////////////////////////
202 //#include "Editor.cci"
203 //#include "Editor.ct"
204 //#include "Editor.cti"
205 #endif
206
207 \f
208 // Local Variables:
209 // mode: c++
210 // fill-column: 100
211 // comment-column: 40
212 // c-file-style: "senf"
213 // indent-tabs-mode: nil
214 // ispell-local-dictionary: "american"
215 // compile-command: "scons -u test"
216 // End: