import "github.com/chzyer/readline"
Readline is a pure go implementation for GNU-Readline kind library.
example:
rl, err := readline.New("> ") if err != nil { panic(err) } defer rl.Close() for { line, err := rl.Readline() if err != nil { // io.EOF break } println(line) }
Package terminal provides support functions for dealing with terminals, as commonly found on UNIX systems.
Putting a terminal into raw mode is the most common requirement:
oldState, err := terminal.MakeRaw(0) if err != nil { panic(err) } defer terminal.Restore(0, oldState)
complete.go complete_helper.go complete_segment.go history.go operation.go password.go readline.go remote.go runebuf.go runes.go search.go std.go term.go term_linux.go term_unix.go terminal.go utils.go utils_unix.go vim.go
const ( T_DATA = MsgType(iota) T_WIDTH T_WIDTH_REPORT T_ISTTY_REPORT T_RAW T_ERAW // exit raw T_EOF )
const ( CharLineStart = 1 CharBackward = 2 CharInterrupt = 3 CharDelete = 4 CharLineEnd = 5 CharForward = 6 CharBell = 7 CharCtrlH = 8 CharTab = 9 CharCtrlJ = 10 CharKill = 11 CharCtrlL = 12 CharEnter = 13 CharNext = 14 CharPrev = 16 CharBckSearch = 18 CharFwdSearch = 19 CharTranspose = 20 CharCtrlU = 21 CharCtrlW = 23 CharCtrlY = 25 CharCtrlZ = 26 CharEsc = 27 CharEscapeEx = 91 CharBackspace = 127 )
var ( Stdin io.ReadCloser = os.Stdin Stdout io.WriteCloser = os.Stdout Stderr io.WriteCloser = os.Stderr )
var TabWidth = 4
add history to global instance manually raise error only if `SetHistoryPath` is set with a non-empty path
ClearScreen clears the console screen
func Debug(o ...interface{})
append log info to another file
func DefaultOnWidthChanged(f func())
GetSize returns the dimensions of the given terminal.
IsTerminal returns true if the given file descriptor is a terminal.
readline with global configs
calculate how many lines for N character
func ListenRemote(n, addr string, cfg *Config, h func(*Instance), onListen ...func(net.Listener) error) error
NewFillableStdin gives you FillableStdin
ReadPassword reads a line of input from a terminal without local echo. This is commonly used for inputting passwords and other sensitive data. The slice returned does not include the \n.
func SetAutoComplete(completer AutoCompleter)
set auto completer to global instance
let readline load history from filepath and try to persist history into disk set fp to "" to prevent readline persisting history to disk so the `AddHistory` will return nil error forever.
func SuspendMe()
SuspendMe use to send suspend signal to myself, when we in the raw mode. For OSX it need to send to parent's pid For Linux it need to send to myself
func WaitForResume() chan struct{}
WaitForResume need to call before current process got suspend. It will run a ticker until a long duration is occurs, which means this process is resumed.
type AutoCompleter interface { // Readline will pass the whole line and current offset to it // Completer need to pass all the candidates, and how long they shared the same characters in line // Example: // [go, git, git-shell, grep] // Do("g", 1) => ["o", "it", "it-shell", "rep"], 1 // Do("gi", 2) => ["t", "t-shell"], 2 // Do("git", 3) => ["", "-shell"], 3 Do(line []rune, pos int) (newLine [][]rune, length int) }
func SegmentFunc(f func([][]rune, int) [][]rune) AutoCompleter
type CancelableStdin struct {
// contains filtered or unexported fields
}
func NewCancelableStdin(r io.Reader) *CancelableStdin
func (c *CancelableStdin) Close() error
func (c *CancelableStdin) Read(b []byte) (n int, err error)
type Config struct { // prompt supports ANSI escape sequence, so we can color some characters even in windows Prompt string // readline will persist historys to file where HistoryFile specified HistoryFile string // specify the max length of historys, it's 500 by default, set it to -1 to disable history HistoryLimit int DisableAutoSaveHistory bool // enable case-insensitive history searching HistorySearchFold bool // AutoCompleter will called once user press TAB AutoComplete AutoCompleter // Any key press will pass to Listener // NOTE: Listener will be triggered by (nil, 0, 0) immediately Listener Listener Painter Painter // If VimMode is true, readline will in vim.insert mode by default VimMode bool InterruptPrompt string EOFPrompt string FuncGetWidth func() int Stdin io.ReadCloser StdinWriter io.Writer Stdout io.Writer Stderr io.Writer EnableMask bool MaskRune rune // erase the editing line after user submited it // it use in IM usually. UniqueEditLine bool // filter input runes (may be used to disable CtrlZ or for translating some keys to different actions) // -> output = new (translated) rune and true/false if continue with processing this one FuncFilterInputRune func(rune) (rune, bool) // force use interactive even stdout is not a tty FuncIsTerminal func() bool FuncMakeRaw func() error FuncExitRaw func() error FuncOnWidthChanged func(func()) ForceUseInteractive bool // contains filtered or unexported fields }
func (c *Config) SetListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool))
type DumpListener struct {
// contains filtered or unexported fields
}
func (d *DumpListener) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
Caller type for dynamic completion
type DynamicPrefixCompleterInterface interface { PrefixCompleterInterface IsDynamic() bool GetDynamicNames(line []rune) [][]rune }
FillableStdin is a stdin reader which can prepend some data before reading into the real stdin
func (s *FillableStdin) Close() error
func (s *FillableStdin) Read(p []byte) (n int, err error)
Read will read from the local buffer and if no data, read from stdin
we must make sure that call Close() before process exit.
HistoryDisable the save of the commands into the history
HistoryEnable the save of the commands into the history (default on)
we can generate a config by `i.GenPasswordConfig()`
same as readline
err is one of (nil, io.EOF, readline.ErrInterrupt)
change history persistence in runtime
switch VimMode in runtime
readline will refresh automatic when write through Stdout()
readline will refresh automatic when write through Stdout()
WriteStdin prefill the next Stdin fetch Next time you call ReadLine() this value will be writen before the user input ie :
i := readline.New() i.WriteStdin([]byte("test")) _, _= i.Readline()
gives
> test[cursor]
func (*InterruptError) Error() string
type Listener interface { OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) }
func FuncListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) Listener
type Operation struct {
// contains filtered or unexported fields
}
func (o Operation) CompleteRefresh()
func (o Operation) EnterCompleteSelectMode()
func (o Operation) EnterVimInsertMode()
func (o Operation) ExitCompleteSelectMode()
func (o Operation) ExitPasswordMode()
func (o Operation) ExitVimInsertMode()
func (o Operation) ExitVimMode()
if err is not nil, it just mean it fail to write to file other things goes fine.
func (o Operation) SearchBackspace()
type PrefixCompleter struct { Name []rune Dynamic bool Callback DynamicCompleteFunc Children []PrefixCompleterInterface }
func NewPrefixCompleter(pc ...PrefixCompleterInterface) *PrefixCompleter
func PcItem(name string, pc ...PrefixCompleterInterface) *PrefixCompleter
func PcItemDynamic(callback DynamicCompleteFunc, pc ...PrefixCompleterInterface) *PrefixCompleter
func (p *PrefixCompleter) GetChildren() []PrefixCompleterInterface
func (p *PrefixCompleter) GetDynamicNames(line []rune) [][]rune
func (p *PrefixCompleter) GetName() []rune
func (p *PrefixCompleter) IsDynamic() bool
func (p *PrefixCompleter) SetChildren(children []PrefixCompleterInterface)
func (p *PrefixCompleter) Tree(prefix string) string
type PrefixCompleterInterface interface { Print(prefix string, level int, buf *bytes.Buffer) Do(line []rune, pos int) (newLine [][]rune, length int) GetName() []rune GetChildren() []PrefixCompleterInterface SetChildren(children []PrefixCompleterInterface) }
type RawMode struct {
// contains filtered or unexported fields
}
type RemoteCli struct {
// contains filtered or unexported fields
}
type RemoteSvr struct {
// contains filtered or unexported fields
}
func (r *RuneBuffer) BackEscapeWord()
func (r *RuneBuffer) Backspace()
func (r *RuneBuffer) Backup()
func (r *RuneBuffer) Clean()
func (r *RuneBuffer) CurrentWidth(x int) int
func (r *RuneBuffer) CursorLineCount() int
func (r *RuneBuffer) Delete() (success bool)
func (r *RuneBuffer) DeleteWord()
func (r *RuneBuffer) Erase()
func (r *RuneBuffer) IdxLine(width int) int
func (r *RuneBuffer) IsCursorInEnd() bool
func (r *RuneBuffer) Kill()
func (r *RuneBuffer) KillFront()
func (r *RuneBuffer) Len() int
func (r *RuneBuffer) LineCount(width int) int
func (r *RuneBuffer) MoveBackward()
func (r *RuneBuffer) MoveForward()
func (r *RuneBuffer) MoveTo(ch rune, prevChar, reverse bool) (success bool)
func (r *RuneBuffer) MoveToEndWord()
func (r *RuneBuffer) MoveToLineEnd()
func (r *RuneBuffer) MoveToLineStart()
func (r *RuneBuffer) MoveToNextWord()
func (r *RuneBuffer) MoveToPrevWord() (success bool)
func (r *RuneBuffer) OnWidthChange(newWidth int)
func (r *RuneBuffer) Pos() int
func (r *RuneBuffer) PromptLen() int
func (r *RuneBuffer) Refresh(f func())
func (r *RuneBuffer) Replace(ch rune)
func (r *RuneBuffer) Reset() []rune
func (r *RuneBuffer) Restore()
func (r *RuneBuffer) RuneSlice(i int) []rune
func (r *RuneBuffer) Runes() []rune
func (r *RuneBuffer) Set(buf []rune)
func (r *RuneBuffer) SetConfig(cfg *Config)
func (r *RuneBuffer) SetMask(m rune)
func (r *RuneBuffer) SetOffset(offset string)
func (r *RuneBuffer) SetPrompt(prompt string)
func (r *RuneBuffer) SetStyle(start, end int, style string)
func (r *RuneBuffer) SetWithIdx(idx int, buf []rune)
func (r *RuneBuffer) Transpose()
func (r *RuneBuffer) WriteRune(s rune)
func (r *RuneBuffer) WriteRunes(s []rune)
func (r *RuneBuffer) WriteString(s string)
func (r *RuneBuffer) Yank()
type Runes struct{}
Search in runes from front to end
Search in runes from end to front
type SegmentComplete struct { SegmentCompleter }
func SegmentAutoComplete(completer SegmentCompleter) *SegmentComplete
type SegmentCompleter interface { // a // |- a1 // |--- a11 // |- a2 // b // input: // DoTree([], 0) [a, b] // DoTree([a], 1) [a] // DoTree([a, ], 0) [a1, a2] // DoTree([a, a], 1) [a1, a2] // DoTree([a, a1], 2) [a1] // DoTree([a, a1, ], 0) [a11] // DoTree([a, a1, a], 1) [a11] DoSegment([][]rune, int) [][]rune }
type State struct {
// contains filtered or unexported fields
}
State contains the state of a terminal.
GetState returns the current state of a terminal which may be useful to restore the terminal after a signal.
MakeRaw put the terminal connected to the given file descriptor into raw mode and returns the previous state of the terminal so that it can be restored.
type TabCompleter struct{}
type Terminal struct {
// contains filtered or unexported fields
}
return rune(0) if meet EOF
SleepToResume will sleep myself, and return only if I'm resumed.
WriteStdin prefill the next Stdin fetch Next time you call ReadLine() this value will be writen before the user input
Path | Synopsis |
---|---|
runes | deprecated. |
Package readline imports 19 packages (graph) and is imported by 473 packages. Updated 2020-02-08. Refresh now. Tools for package owners.