class LineInput
Defined at line 69 of file ../../src/lib/line_input/line_input.h
Abtract base class for line input.
This class implements a push model for input of characters, allowing it to be used in
asynchronous contexts.
The model is you create a LineInput class outside of the input loop. It encapsulates the history
state and remembers the prompt. When you want to read a line:
1. Call Show().
2. Push data to it via OnInput().
3. On an accept callback:
3a. Handle the input.
3b. Add line to history if desired.
4. Repeat until done.
5. Call Hide() to put the terminal back.
If your application has data that it needs to print asynchronously, just:
1. Call Hide().
2. Print the stuff you want.
3. Call Show().
Public Methods
void SetAutocompleteCallback (AutocompleteCallback cb)
Provides the callback for tab completion.
void SetChangeCallback (ChangeCallback cb)
Provides the callback for when the current line changes.
void SetCancelCallback (CancelCallback cb)
Provides the callback for handling Control-C. If unset, "^C" will be echoed and the line
will be cleared.
void SetEofCallback (EofCallback cb)
Provides the callback for handling EOF. If unset EOF will be ignored.
void SetMaxCols (size_t max)
Sets the maximum width of a line. Beyond this the input will scroll. Setitng to 0 will disable
horizontal scrolling.
const std::string & GetLine ()
Returns the current input text.
const std::deque<std::string> & GetHistory ()
Returns the current history. The most resent input is at the begin().
void OnInput (char c)
Provides one character of input to the editor. Callbacks for autocomplete or line done will be
issued from within this function.
void AddToHistory (const std::string & line)
Adds the given line to history. If the history is longer than max_history_, the oldest thing
will be deleted.
AddToHistory should be called on startup before the initial Show() call, or from within the
accept callback (typically you would add the current line at this point).
void Hide (InterruptHandlingBehavior behavior)
The input can be hidden and re-shown. Hiding it will erase the current line and put the cursor
at the beginning of the line, but not change any internal state. Showing it again will repaint
the line at the new cursor position. This allows other output to be printed to the screen
without interfering with the input.
Hiding or showing when it's already in that state will do nothing. There is not a reference
count on the hide calls.
OnInput() should not be called while hidden.
Tip: When the application is done (the user types "quit" or whatever), call Hide() from within
the AcceptCallback or EofCallback. This will ensure the prompt isn't repainted when the
callback is complete only to be rehidden on exit (which will cause flickering).
void Show ()
void SetCurrentInput (const std::string & input)
Replaces the contents of the current line with the given contents. The cursor will be placed
at the end of the text. Any changes in editing history will be reset.
This bypasses handling of any special characters and input and any such
characters will be included as literals on the line. This will issue any changed callbacks.
void ~LineInput ()
Defined at line 71 of file ../../src/lib/line_input/line_input.h
Enumerations
enum InterruptHandlingBehavior
| Name | Value |
|---|---|
| kHandleInterrupts | 0 |
| kIgnoreInterrupts | 1 |
Whether the input should handle interruptions (e.g., handle Ctrl-C by calling the cancel
callback and handle Ctrl-D by calling EOF callbacks) while hidden.
Defined at line 131 of file ../../src/lib/line_input/line_input.h