Documentation
¶
Overview ¶
Package termutil contains common function for terminal operations.
Index ¶
- Variables
- type ConsoleLineTerminal
- func AddAutoCompleteMixin(term ConsoleLineTerminal, dict Dict) (ConsoleLineTerminal, error)
- func AddFileReadingWrapper(term ConsoleLineTerminal, r io.Reader, termOnEOF bool) (ConsoleLineTerminal, error)
- func AddHistoryMixin(term ConsoleLineTerminal, histFile string, ignoreLine func(string) bool) (ConsoleLineTerminal, error)
- func NewConsoleLineTerminal(console io.Writer) (ConsoleLineTerminal, error)
- type Dict
- type DictChooser
- type KeyHandler
- type MultiWordDict
- type WordListDict
Constants ¶
This section is empty.
Variables ¶
var DefaultHistoryBufferSize = 100
DefaultHistoryBufferSize is the default history buffer size in lines
Functions ¶
This section is empty.
Types ¶
type ConsoleLineTerminal ¶
type ConsoleLineTerminal interface {
/*
StartTerm prepares a new terminal session. This call initialises the tty
on Linux or retrieves an event object on Windows.
*/
StartTerm() error
/*
AddKeyHandler adds a new KeyHandler to this ConsoleLineTerminal.
*/
AddKeyHandler(handler KeyHandler)
/*
NextLine lets the user produce the next line in the terminal. All entered
characters are echoed. The line is finished if the user presses return or
pastes in a newline character. The final newline is echoed. If single
character input via getch is not available then the code falls back to a
simple line input from stdin.
*/
NextLine() (string, error)
/*
NextLinePrompt lets the user produce the next line in the terminal with a
special prompt. All entered characters are echoed if echo is 0x0 otherwise
the echo character is written. The line is finished if the user presses
return or pastes in a newline character. The final newline is echoed. If
single character input via getch is not available then the code falls back
to a simple line input from stdin.
*/
NextLinePrompt(prompt string, echo rune) (string, error)
/*
WriteString write a string on this terminal.
*/
WriteString(s string)
/*
Write writes len(p) bytes from p to the terminal.
*/
Write(p []byte) (n int, err error)
/*
StopTerm finishes the current terminal session. This call returns the tty
on Linux to its original state and closes all open handles on all platforms.
*/
StopTerm()
}
ConsoleLineTerminal is the most common console terminal implementation. The user types input and a chosen backend records the input by key. It has a graceful fallback to a standard line reader for all other platforms. The functionality can be extended by adding key handlers.
Example code:
clt, err := termutil.NewConsoleLineTerminal(os.Stdout)
if err == nil {
// Add history functionality
clt, err = termutil.AddHistoryMixin(clt, "", func(s string) bool {
return s == "q"
})
if err == nil {
rootDict := termutil.NewWordListDict([]string{"ll", "dir", "test",
"test1", "test2"})
chooser := func(lineWords []string,
dictCache map[string]termutil.Dict) (termutil.Dict, error) {
if len(lineWords) == 1 {
return rootDict, nil
}
return termutil.NewWordListDict([]string{
fmt.Sprintf("file4-%v", len(lineWords)), "file2",
"file1", "directory"}), nil
}
dict := termutil.NewMultiWordDict(chooser, nil)
clt, err = termutil.AddAutoCompleteMixin(clt, dict)
if err == nil {
if err = clt.StartTerm(); err == nil {
var line string
defer clt.StopTerm()
line, err = clt.NextLine()
for err == nil && line != "q" {
fmt.Println("###", line)
line, err = clt.NextLine()
}
}
}
}
}
if err != nil {
fmt.Println(err)
}
func AddAutoCompleteMixin ¶
func AddAutoCompleteMixin(term ConsoleLineTerminal, dict Dict) (ConsoleLineTerminal, error)
AddAutoCompleteMixin adds auto-complete support for a given ConsoleLineTerminal. The auto-complete function operates on a given Dict object which suggests either a direct match or a list of matches. A single tab auto-completes if there is a direct match. Two tabs and the console outputs all suggestions.
func AddFileReadingWrapper ¶
func AddFileReadingWrapper(term ConsoleLineTerminal, r io.Reader, termOnEOF bool) (ConsoleLineTerminal, error)
AddFileReadingWrapper wraps a given terminal and provides the fist lines of a given input reader as first lines before delegating to the wrapped terminal. Terminates after the file has been red if termOnEOF is set.
func AddHistoryMixin ¶
func AddHistoryMixin(term ConsoleLineTerminal, histFile string, ignoreLine func(string) bool) (ConsoleLineTerminal, error)
AddHistoryMixin adds history support for a given ConsoleLineTerminal. History is collected with every line and persisted in a file. The user can scroll through the history using the cursor keys up and down. The client can optionally define a ignoreLine function which causes a line to be ignored if it returns true.
func NewConsoleLineTerminal ¶
func NewConsoleLineTerminal(console io.Writer) (ConsoleLineTerminal, error)
NewConsoleLineTerminal creates a new basic ConsoleLineTerminal.
type Dict ¶
type Dict interface {
/*
Suggest returns dictionary suggestions based on a given prefix. Returns if there
is a direct match and a list of suggestions.
*/
Suggest(prefix string) ([]string, error)
}
Dict is a dictionary object used by the AutoCompleteMixin
type DictChooser ¶
DictChooser chooses a WordListDict based on given prefix words. The function also gets a presisted map of WordListDicts which can be used as a cache.
type KeyHandler ¶
KeyHandler handles specific key events. KeyHandlers are used to extend the functionality of the normal ConsoleLineTerminal. Returns if the event was consumed (no further handling possible), a new input buffer and any errors that might have occurred. The new input buffer is ignored if it is nil.
type MultiWordDict ¶
type MultiWordDict struct {
// contains filtered or unexported fields
}
MultiWordDict models a dictionary which can present suggestions based on multiple words. Only suggestions for the last word are returned. However, these suggestions may depend on the preceding words.
func NewMultiWordDict ¶
func NewMultiWordDict(chooser DictChooser, dicts map[string]Dict) *MultiWordDict
NewMultiWordDict returns a new MultiWordDict. The client code needs to specify a function to retrieve WordListDicts for given prefix words and can optionally supply an initial map of WordListDicts.
type WordListDict ¶
type WordListDict struct {
// contains filtered or unexported fields
}
WordListDict is a simple dictionary which looks up suggstions based on an internal word list
func NewWordListDict ¶
func NewWordListDict(words []string) *WordListDict
NewWordListDict returns a new WordListDict from a given list of words. The list of words will be sorted.