Documentation
¶
Overview ¶
Package readline is a pure-Go re-imagining of the UNIX readline API
Index ¶
- Constants
- Variables
- func GetSize(fd int) (width, height int, err error)
- func GetTermWidth() (termWidth int)
- func IsTerminal(fd int) bool
- func Restore(fd int, state *State) error
- type DelayedTabContext
- type EventReturn
- type ExampleHistory
- type History
- type Instance
- type NullHistory
- type State
- type TabDisplayType
Constants ¶
const ( // ErrCtrlC is returned when ctrl+c is pressed. // WARNING: this is being deprecated! Please use CtrlC (type error) instead ErrCtrlC = "Ctrl+C" // ErrEOF is returned when ctrl+d is pressed. // WARNING: this is being deprecated! Please use EOF (type error) instead ErrEOF = "EOF" )
const ( // TabDisplayGrid is the default. It's where the screen below the prompt is // divided into a grid with each suggestion occupying an individual cell. TabDisplayGrid = iota // TabDisplayList is where suggestions are displayed as a list with a // description. The suggestion gets highlighted but both are searchable (ctrl+f) TabDisplayList // TabDisplayMap is where suggestions are displayed as a list with a // description however the description is what gets highlighted and only // that is searchable (ctrl+f). The benefit of TabDisplayMap is when your // autocomplete suggestions are IDs rather than human terms. TabDisplayMap )
Variables ¶
var ( // CtrlC is returned when ctrl+c is pressed CtrlC = errors.New("Ctrl+C") // EOF is returned when ctrl+d is pressed. // (this is actually the same value as io.EOF) EOF = errors.New("EOF") )
Functions ¶
func GetTermWidth ¶
func GetTermWidth() (termWidth int)
GetTermWidth returns the width of Stdout or 80 if the width cannot be established
func IsTerminal ¶
IsTerminal returns true if the given file descriptor is a terminal.
Types ¶
type DelayedTabContext ¶
DelayedTabContext is a custom context interface for async updates to the tab completions
func (DelayedTabContext) AppendDescriptions ¶
func (dtc DelayedTabContext) AppendDescriptions(suggestions map[string]string)
AppendDescriptions updates the tab completions with additional suggestions + descriptions asynchronously
func (DelayedTabContext) AppendSuggestions ¶
func (dtc DelayedTabContext) AppendSuggestions(suggestions []string)
AppendSuggestions updates the tab completions with additional suggestions asynchronously
type EventReturn ¶
type EventReturn struct { ForwardKey bool ClearHelpers bool CloseReadline bool HintText []rune NewLine []rune NewPos int }
EventReturn is a structure returned by the callback event function. This is used by readline to determine what state the API should return to after the readline event.
type ExampleHistory ¶
type ExampleHistory struct {
// contains filtered or unexported fields
}
ExampleHistory is an example of a LineHistory interface:
func (*ExampleHistory) Dump ¶
func (h *ExampleHistory) Dump() interface{}
Dump returns the entire history
func (*ExampleHistory) GetLine ¶
func (h *ExampleHistory) GetLine(i int) (string, error)
GetLine returns a line from history
func (*ExampleHistory) Len ¶
func (h *ExampleHistory) Len() int
Len returns the number of lines in history
type History ¶
type History interface { // Append takes the line and returns an updated number of lines or an error Write(string) (int, error) // GetLine takes the historic line number and returns the line or an error GetLine(int) (string, error) // Len returns the number of history lines Len() int // Dump returns everything in readline. The return is an interface{} because // not all LineHistory implementations will want to structure the history in // the same way. And since Dump() is not actually used by the readline API // internally, this methods return can be structured in whichever way is most // convenient for your own applications (or even just create an empty //function which returns `nil` if you don't require Dump() either) Dump() interface{} }
History is an interface to allow you to write your own history logging tools. eg sqlite backend instead of a file system. By default readline will just use the dummyLineHistory interface which only logs the history to memory ([]string to be precise).
type Instance ¶
type Instance struct { // PasswordMask is what character to hide password entry behind. // Once enabled, set to 0 (zero) to disable the mask again. PasswordMask rune // SyntaxHighlight is a helper function to provide syntax highlighting. // Once enabled, set to nil to disable again. SyntaxHighlighter func([]rune) string // History is an interface for querying the readline history. // This is exposed as an interface to allow you the flexibility to define how // you want your history managed (eg file on disk, database, cloud, or even // no history at all). By default it uses a dummy interface that only stores // historic items in memory. History History // HistoryAutoWrite defines whether items automatically get written to // history. // Enabled by default. Set to false to disable. HistoryAutoWrite bool // = true // TabCompleter is a simple function that offers completion suggestions. // It takes the readline line ([]rune) and cursor pos. Returns a prefix // string, an array of suggestions and a map of definitions (optional). TabCompleter func([]rune, int, DelayedTabContext) (string, []string, map[string]string, TabDisplayType) MinTabItemLength int MaxTabItemLength int // MaxTabCompletionRows is the maximum number of rows to display in the tab // completion grid. MaxTabCompleterRows int // = 4 // SyntaxCompletion is used to autocomplete code syntax (like braces and // quotation marks). If you want to complete words or phrases then you might // be better off using the TabCompletion function. // SyntaxCompletion takes the line ([]rune) and cursor position, and returns // the new line and cursor position. SyntaxCompleter func([]rune, int) ([]rune, int) // DelayedSyntaxWorker allows for syntax highlighting happen to the line // after the line has been drawn. DelayedSyntaxWorker func([]rune) []rune // HintText is a helper function which displays hint text the prompt. // HintText takes the line input from the prompt and the cursor position. // It returns the hint text to display. HintText func([]rune, int) []rune // HintColor any ANSI escape codes you wish to use for hint formatting. By // default this will just be blue. HintFormatting string // TempDirectory is the path to write temporary files when editing a line in // $EDITOR. This will default to os.TempDir() TempDirectory string // GetMultiLine is a callback to your host program. Since multiline support // is handled by the application rather than readline itself, this callback // is required when calling $EDITOR. However if this function is not set // then readline will just use the current line. GetMultiLine func([]rune) []rune EnableGetCursorPos bool // contains filtered or unexported fields }
Instance is used to encapsulate the parameter group and run time of any given readline instance so that you can reuse the readline API for multiple entry captures without having to repeatedly unload configuration.
func NewInstance ¶
func NewInstance() *Instance
NewInstance is used to create a readline instance and initialise it with sane defaults.
func (*Instance) Readline ¶
Readline displays the readline prompt. It will return a string (user entered data) or an error.
func (*Instance) SetHintText ¶
SetHintText is a nasty function for force writing a new hint text. Use sparingly!
type NullHistory ¶
type NullHistory struct{}
NullHistory is a null History interface for when you don't want line entries remembered eg password input.
func (*NullHistory) GetLine ¶
func (h *NullHistory) GetLine(i int) (string, error)
GetLine returns a line from history
func (*NullHistory) Len ¶
func (h *NullHistory) Len() int
Len returns the number of lines in history
type State ¶
type State struct {
// contains filtered or unexported fields
}
State contains the state of a terminal.
type TabDisplayType ¶
type TabDisplayType int
TabDisplayType defines how the autocomplete suggestions display