Documentation
¶
Overview ¶
Library to interact with ansi/v100 style terminals.
Index ¶
- Constants
- type AutoCompleteCallback
- type Terminal
- func (t *Terminal) AddToHistory(commands ...string)
- func (t *Terminal) AutoHistory() bool
- func (t *Terminal) Close() error
- func (t *Terminal) History() []string
- func (t *Terminal) IsTerminal() bool
- func (t *Terminal) NewHistory(capacity int)
- func (t *Terminal) ReadLine() (string, error)
- func (t *Terminal) ReplaceLatest(command string) string
- func (t *Terminal) SetAutoCompleteCallback(f AutoCompleteCallback)
- func (t *Terminal) SetAutoHistory(enabled bool)
- func (t *Terminal) SetHistoryFile(f string) error
- func (t *Terminal) SetPrompt(s string)
Constants ¶
const DefaultHistoryCapacity = term.DefaultHistoryEntries
DefaultHistoryCapacity is the default number of entries in the history (99).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AutoCompleteCallback ¶
type AutoCompleteCallback func(t *Terminal, line string, pos int, key rune) (newLine string, newPos int, ok bool)
AutoCompleteCallback is called with "this" terminal as first argument so AutoCompleteCallback can use t.Out etc. (compared to the original x/term callback).
type Terminal ¶
type Terminal struct { // Use this for any output to the screen/console so the required \r are added in raw mode // the prompt and command edit is refresh as needed when input comes in. Out io.Writer // contains filtered or unexported fields }
func Open ¶
Open opens stdin as a terminal, do `defer terminal.Close()` to restore the terminal to its original state upon exit. fortio.org/log (and thus stdlib "log") will be redirected to the terminal in a manner that preserves the prompt.
func (*Terminal) AddToHistory ¶ added in v0.4.0
AddToHistory add commands to the history.
func (*Terminal) AutoHistory ¶ added in v0.5.0
AutoHistory returns the current auto history setting.
func (*Terminal) Close ¶
Close restores the terminal to its original state. Must be called at exit to avoid leaving the terminal in raw mode. Safe to call multiple times. Will save the history to the history file if one was set using [SetHistoryFile] and the capacity is > 0.
func (*Terminal) IsTerminal ¶
func (*Terminal) NewHistory ¶ added in v0.4.0
NewHistory creates/resets the history to a new one with the given capacity. Using 0 as capacity will disable history reading and writing but not change the underlying history state from it's DefaultHistoryCapacity.
func (*Terminal) ReadLine ¶
ReadLine reads a line from the terminal using the setup prompt and history and edit capabilities. Returns the line and an error if any. io.EOF is returned when the user presses ^D or ^C.
func (*Terminal) ReplaceLatest ¶ added in v0.5.0
ReplaceLatest replaces the current history with the given commands, returns the previous value.
func (*Terminal) SetAutoCompleteCallback ¶
func (t *Terminal) SetAutoCompleteCallback(f AutoCompleteCallback)
SetAutoCompleteCallback sets the callback called for each key press. Can be used to implement auto completion. See example/main.go for an example.
func (*Terminal) SetAutoHistory ¶ added in v0.5.0
SetAutoHistory enables/disables auto history (default is enabled).
func (*Terminal) SetHistoryFile ¶
Sets up a file to load and save history from/to. File is being read when this is called. If no error is returned, the file will also be automatically updated on Close().