Single line interactive text editor. More...
#include <senf/Utils/Termlib/Editor.hh>
Public Types | |
typedef boost::function< void(LineEditor &)> | KeyBinding |
Type of a key binding function. More... | |
typedef boost::function< void(std::string const &)> | AcceptCallback |
Callback function type. More... | |
Public Types inherited from senf::term::BaseEditor | |
typedef KeyParser::keycode_t | keycode_t |
Public Member Functions | |
LineEditor (AbstractTerminal &terminal, AcceptCallback cb) | |
Create a LineEditor. More... | |
Public Member Functions inherited from senf::term::BaseEditor | |
BaseEditor (AbstractTerminal &terminal) | |
void | newline () |
Move to beginning of a new, empty line. More... | |
void | toColumn (unsigned c) |
Move cursor to column c . More... | |
void | put (char ch) |
Write ch at current column. More... | |
void | put (std::string const &text) |
Write text starting at current column. More... | |
void | clearLine () |
Clear current line and move cursor to first column. More... | |
void | setBold () |
Set bold char display. More... | |
void | setNormal () |
Set normal char display. More... | |
void | maybeClrScr () |
Clear screen if possible. More... | |
void | toLine (unsigned l) |
Move to relative display line l. More... | |
void | reset () |
Reset display area to single line. More... | |
unsigned | currentColumn () const |
Return number of current column. More... | |
unsigned | currentLine () const |
Return number of current relative line. More... | |
unsigned | width () const |
Return current screen width. More... | |
unsigned | height () const |
Return current screen height. More... | |
Public Member Functions inherited from senf::term::AbstractTerminal::Callbacks | |
virtual | ~Callbacks () |
Static Public Attributes | |
static unsigned const | MAX_HISTORY_SIZE = 1024u |
Static Public Attributes inherited from senf::term::BaseEditor | |
static unsigned const | DEFAULT_KEY_TIMEOUT_MS = 500u |
Overall edit control | |
void | show () |
Enable editor widget. More... | |
void | hide () |
Disable editor widget. More... | |
void | accept () |
Accept current user input and call the accept callback. More... | |
void | clear () |
Clear editor buffer. More... | |
void | redisplay () |
Mark the editor buffer for redisplay. More... | |
void | forceRedisplay () |
Redisplay the editor buffer now. More... | |
void | prompt (std::string const &text) |
Set prompt string. More... | |
Cursor and display movement | |
void | gotoChar (unsigned n) |
Move cursor to position n. More... | |
void | scrollTo (unsigned n) |
Move position to beginning of display line. More... | |
Text manipulation | |
void | deleteChar (unsigned n=1) |
Delete n characters at point. More... | |
void | insert (char ch) |
Insert ch at point. More... | |
void | insert (std::string const &text) |
Insert text at point. More... | |
void | set (std::string const &text, unsigned pos=0u) |
Set edit buffer contents. More... | |
History | |
void | pushHistory (std::string const &text, bool accept=false) |
Add string text to history. More... | |
void | prevHistory () |
Switch to previous history entry. More... | |
void | nextHistory () |
Switch to next history entry. More... | |
Aux Display | |
void | auxDisplay (unsigned line, std::string const &text) |
Display text on aux display line line. More... | |
unsigned | maxAuxDisplayHeight () |
Get maximum height of the aux display area. More... | |
void | clearAuxDisplay () |
Clear the aux display area. More... | |
Get information | |
std::string const & | text () |
Get current editor buffer contents. More... | |
unsigned | point () |
Get current cursor position. More... | |
unsigned | displayPos () |
Get current display position. More... | |
keycode_t | lastKey () |
Get last key code received. More... | |
Key bindings | |
void | defineKey (keycode_t key, KeyBinding binding) |
Bind key key to binding. More... | |
void | unsetKey (keycode_t key) |
Remove all bindings for key. More... | |
Additional Inherited Members |
Single line interactive text editor.
LineEditor implements a single-line input widget on an arbitrary AbstractTerminal.
The LineEditor will query the user for an input line. When the user accepts a line, LineEditor will call a user callback function. After the callback has been called, the editor is disabled. To accept a new input line, call show().
Keys are defined in the keymap using defineKey(). The default bindings are: <table class="senf"> <tr><td>\c Return</td> <td>bindings::accept</td></tr> <tr><td>\c Right</td> <td>bindings::forwardChar</td></tr> <tr><td>\c Left</td> <td>bindings::backwardChar</td></tr> <tr><td>\c Up</td> <td>bindings::prevHistory</td></tr> <tr><td>\c Down</td> <td>bindings::nextHistory</td></tr> <tr><td>\c Backspace</td> <td>bindings::backwardDeleteChar</td></tr> <tr><td>\c Delete</td> <td>bindings::deleteChar</td></tr> <tr><td>\c Home</td> <td>bindings::beginningOfLine</td></tr> <tr><td>\c End</td> <td>bindings::endOfLine</td></tr> <tr><td>\c Ctrl-K</td> <td>bindings::deleteToEndOfLine</td></tr> <tr><td>\c Ctrl-A</td> <td>bindings::beginningOfLine</td></tr> <tr><td>\c Ctrl-E</td> <td>bindings::endOfLine</td></tr> <tr><td>\c Ctrl-D</td> <td>bindings::deleteChar</td></tr> <tr><td>\c Ctrl-C</td> <td>bindings::restartEdit</td></tr> <tr><td>\c Ctrl-L</td> <td>bindings::clearScreen</td></tr> </table> See the senf::term::bindings namespace for a list of all default provided key binding functions.
Completion support is provided by senf::term::bindings::complete(). To use the completer, you need to implement a completion function and pass it as second argument to bindings::complete():
When \c myCompleter is a class member, use senf::membind() and pass this instead of \c &myCompleter to \c boost::bind() and thus to senf::term::bindings::complete. The completion protocol is as follows: When completion is desired, the completer function is called. \a b and \a e are set to 0 and <tt>editor.point()</tt> respectively. \a prefix and \a completions are empty. \li the completer may restrict the to-be-completed string to any subrange by changing \a b and \a e accordingly. \li If there is an initial substring which applies to \e all completions but should not be listed in the list of completions, assign this value to \a prefix. \li Add all possible completions to the \a completions vector not including the \a prefix. \li The completion result is taken from the size of the \a completions vector \e only: If this vector is empty, completion failed (even if \a prefix is set), a single entry in \a completions (even if it is the empty string) signals a unique completion.
The auxiliary display area is accessed using auxDisplay() and clearAuxDisplay(). The aux display area is \e cleared \e before each new key is processed. Therefore it is only temporary. The aux display area however will survive hide() / show().
Calling hide() will temporarily disable the editor. All editor display will be removed. Calling show() will redisplay the editor in it's current state including the aux display area.
typedef boost::function<void (std::string const &)> senf::term::LineEditor::AcceptCallback |
typedef boost::function<void (LineEditor&)> senf::term::LineEditor::KeyBinding |
senf::term::LineEditor::LineEditor | ( | AbstractTerminal & | terminal, |
AcceptCallback | cb | ||
) |
Create a LineEditor.
[in] | terminal | abstract terminal interface |
[in] | cb | callback to call for complete input line |
void senf::term::LineEditor::accept | ( | ) |
void senf::term::LineEditor::auxDisplay | ( | unsigned | line, |
std::string const & | text | ||
) |
void senf::term::LineEditor::clear | ( | ) |
void senf::term::LineEditor::clearAuxDisplay | ( | ) |
void senf::term::LineEditor::defineKey | ( | keycode_t | key, |
KeyBinding | binding | ||
) |
void senf::term::LineEditor::deleteChar | ( | unsigned | n = 1 | ) |
unsigned senf::term::LineEditor::displayPos | ( | ) |
void senf::term::LineEditor::forceRedisplay | ( | ) |
void senf::term::LineEditor::gotoChar | ( | unsigned | n | ) |
void senf::term::LineEditor::hide | ( | ) |
void senf::term::LineEditor::insert | ( | char | ch | ) |
void senf::term::LineEditor::insert | ( | std::string const & | text | ) |
senf::term::LineEditor::keycode_t senf::term::LineEditor::lastKey | ( | ) |
unsigned senf::term::LineEditor::maxAuxDisplayHeight | ( | ) |
void senf::term::LineEditor::nextHistory | ( | ) |
unsigned senf::term::LineEditor::point | ( | ) |
void senf::term::LineEditor::prevHistory | ( | ) |
void senf::term::LineEditor::prompt | ( | std::string const & | text | ) |
void senf::term::LineEditor::pushHistory | ( | std::string const & | text, |
bool | accept = false |
||
) |
void senf::term::LineEditor::redisplay | ( | ) |
void senf::term::LineEditor::scrollTo | ( | unsigned | n | ) |
void senf::term::LineEditor::set | ( | std::string const & | text, |
unsigned | pos = 0u |
||
) |
void senf::term::LineEditor::show | ( | ) |
std::string const & senf::term::LineEditor::text | ( | ) |
void senf::term::LineEditor::unsetKey | ( | keycode_t | key | ) |
|
static |