package readline

Import Path
	github.com/chzyer/readline (on go.dev)

Dependency Relation
	imports 19 packages, and imported by one package

Involved Source Files complete.go complete_helper.go complete_segment.go history.go operation.go password.go 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) } remote.go runebuf.go runes.go search.go std.go 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) term_linux.go term_unix.go terminal.go utils.go utils_unix.go vim.go
Package-Level Type Names (total 43, in which 28 are exported)
/* sort exporteds by: | */
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 DynamicPrefixCompleterInterface (interface) *PrefixCompleter PrefixCompleterInterface (interface) *SegmentComplete *TabCompleter func SegmentFunc(f func([][]rune, int) [][]rune) AutoCompleter func SetAutoComplete(completer AutoCompleter)
(*T) Close() error (*T) Read(b []byte) (n int, err error) *T : io.Closer *T : io.ReadCloser *T : io.Reader func NewCancelableStdin(r io.Reader) *CancelableStdin
AutoCompleter will called once user press TAB DisableAutoSaveHistory bool EOFPrompt string EnableMask bool ForceUseInteractive bool FuncExitRaw func() error 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 FuncGetWidth func() int force use interactive even stdout is not a tty FuncMakeRaw func() error FuncOnWidthChanged func(func()) readline will persist historys to file where HistoryFile specified specify the max length of historys, it's 500 by default, set it to -1 to disable history enable case-insensitive history searching InterruptPrompt string Any key press will pass to Listener NOTE: Listener will be triggered by (nil, 0, 0) immediately MaskRune rune Painter Painter prompt supports ANSI escape sequence, so we can color some characters even in windows Stderr io.Writer Stdin io.ReadCloser StdinWriter io.Writer Stdout io.Writer erase the editing line after user submited it it use in IM usually. If VimMode is true, readline will in vim.insert mode by default ( T) Clone() *Config (*T) Init() error (*T) SetListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) (*T) SetPainter(p Painter) func Config.Clone() *Config func (*Instance).GenPasswordConfig() *Config func (*Instance).SetConfig(cfg *Config) *Config func (*Operation).GenPasswordConfig() *Config func (*Operation).GetConfig() *Config func (*Operation).SetConfig(cfg *Config) (*Config, error) func (*Terminal).GetConfig() *Config func HandleConn(cfg Config, conn net.Conn) (*Instance, error) func ListenRemote(n, addr string, cfg *Config, h func(*Instance), onListen ...func(net.Listener) error) error func NewEx(cfg *Config) (*Instance, error) func NewOperation(t *Terminal, cfg *Config) *Operation func NewRuneBuffer(w io.Writer, prompt string, cfg *Config, width int) *RuneBuffer func NewTerminal(cfg *Config) (*Terminal, error) func (*Instance).ReadPasswordWithConfig(cfg *Config) ([]byte, error) func (*Instance).SetConfig(cfg *Config) *Config func (*Operation).PasswordWithConfig(cfg *Config) ([]byte, error) func (*Operation).SetConfig(cfg *Config) (*Config, error) func (*RemoteSvr).HandleConfig(cfg *Config) func (*RuneBuffer).SetConfig(cfg *Config) func (*Terminal).SetConfig(c *Config) error
(*T) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) *T : Listener
Caller type for dynamic completion func PcItemDynamic(callback DynamicCompleteFunc, pc ...PrefixCompleterInterface) *PrefixCompleter
( T) Do(line []rune, pos int) (newLine [][]rune, length int) ( T) GetChildren() []PrefixCompleterInterface ( T) GetDynamicNames(line []rune) [][]rune ( T) GetName() []rune ( T) IsDynamic() bool ( T) Print(prefix string, level int, buf *bytes.Buffer) ( T) SetChildren(children []PrefixCompleterInterface) *PrefixCompleter T : AutoCompleter T : PrefixCompleterInterface
FillableStdin is a stdin reader which can prepend some data before reading into the real stdin Mutex sync.Mutex (*T) Close() error Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available. Read will read from the local buffer and if no data, read from stdin Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock. A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it. *T : io.Closer *T : io.ReadCloser *T : io.Reader *T : sync.Locker
Config *Config Operation *Operation Terminal *Terminal (*T) Clean() we must make sure that call Close() before process exit. (*T) GenPasswordConfig() *Config HistoryDisable the save of the commands into the history HistoryEnable the save of the commands into the history (default on) (*T) IsVimMode() bool (*T) Line() *Result (*T) ReadPassword(prompt string) ([]byte, error) (*T) ReadPasswordEx(prompt string, l Listener) ([]byte, error) we can generate a config by `i.GenPasswordConfig()` same as readline err is one of (nil, io.EOF, readline.ErrInterrupt) (*T) ReadlineWithDefault(what string) (string, error) (*T) Refresh() (*T) ResetHistory() (*T) SaveHistory(content string) error (*T) SetConfig(cfg *Config) *Config change history persistence in runtime (*T) SetMaskRune(r rune) (*T) SetPrompt(s string) switch VimMode in runtime readline will refresh automatic when write through Stdout() readline will refresh automatic when write through Stdout() (*T) Write(b []byte) (int, error) 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] *T : io.Closer *T : io.WriteCloser *T : io.Writer func HandleConn(cfg Config, conn net.Conn) (*Instance, error) func New(prompt string) (*Instance, error) func NewEx(cfg *Config) (*Instance, error)
Line []rune (*T) Error() string *T : error
( T) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) *DumpListener func FuncListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) Listener func (*Instance).ReadPasswordEx(prompt string, l Listener) ([]byte, error) func (*Operation).PasswordEx(prompt string, l Listener) ([]byte, error)
Data []byte Type MsgType (*T) WriteTo(w io.Writer) (int, error) func NewMessage(t MsgType, data []byte) *Message func ReadMessage(r io.Reader) (*Message, error)
func NewMessage(t MsgType, data []byte) *Message const T_DATA const T_EOF const T_ERAW const T_ISTTY_REPORT const T_RAW const T_WIDTH const T_WIDTH_REPORT
(*T) Clean() (*T) Close() ( T) CompleteRefresh() ( T) EnterCompleteMode(offset int, candidate [][]rune) ( T) EnterCompleteSelectMode() ( T) EnterPasswordMode(cfg *Config) (err error) ( T) EnterVimInsertMode() ( T) ExitCompleteMode(revent bool) ( T) ExitCompleteSelectMode() ( T) ExitPasswordMode() ( T) ExitSearchMode(revert bool) ( T) ExitVimInsertMode() ( T) ExitVimMode() (*T) GenPasswordConfig() *Config (*T) GetConfig() *Config ( T) HandleCompleteSelect(r rune) bool ( T) HandleVim(r rune, readNext func() rune) rune ( T) HandleVimNormal(r rune, readNext func() rune) (t rune) ( T) IsEnableVimMode() bool ( T) IsInCompleteMode() bool ( T) IsInCompleteSelectMode() bool (*T) IsNormalMode() bool ( T) IsSearchMode() bool ( T) OnComplete() bool (*T) Password(prompt string) ([]byte, error) ( T) PasswordConfig() *Config (*T) PasswordEx(prompt string, l Listener) ([]byte, error) (*T) PasswordWithConfig(cfg *Config) ([]byte, error) (*T) Refresh() (*T) ResetHistory() (*T) Runes() ([]rune, error) if err is not nil, it just mean it fail to write to file other things goes fine. ( T) SearchBackspace() ( T) SearchChar(r rune) ( T) SearchMode(dir int) bool ( T) SearchRefresh(x int) (*T) SetBuffer(what string) (*T) SetConfig(cfg *Config) (*Config, error) (*T) SetHistoryPath(path string) (*T) SetMaskRune(r rune) (*T) SetPrompt(s string) (*T) SetTitle(t string) ( T) SetVimMode(on bool) (*T) Slice() ([]byte, error) (*T) Stderr() io.Writer (*T) Stdout() io.Writer (*T) String() (string, error) func NewOperation(t *Terminal, cfg *Config) *Operation func (*Terminal).Readline() *Operation
( T) Paint(line []rune, pos int) []rune func (*Config).SetPainter(p Painter)
Callback DynamicCompleteFunc Children []PrefixCompleterInterface Dynamic bool Name []rune (*T) Do(line []rune, pos int) (newLine [][]rune, offset int) (*T) GetChildren() []PrefixCompleterInterface (*T) GetDynamicNames(line []rune) [][]rune (*T) GetName() []rune (*T) IsDynamic() bool (*T) Print(prefix string, level int, buf *bytes.Buffer) (*T) SetChildren(children []PrefixCompleterInterface) (*T) Tree(prefix string) string *T : AutoCompleter *T : DynamicPrefixCompleterInterface *T : PrefixCompleterInterface func NewPrefixCompleter(pc ...PrefixCompleterInterface) *PrefixCompleter func PcItem(name string, pc ...PrefixCompleterInterface) *PrefixCompleter func PcItemDynamic(callback DynamicCompleteFunc, pc ...PrefixCompleterInterface) *PrefixCompleter
( T) Do(line []rune, pos int) (newLine [][]rune, length int) ( T) GetChildren() []PrefixCompleterInterface ( T) GetName() []rune ( T) Print(prefix string, level int, buf *bytes.Buffer) ( T) SetChildren(children []PrefixCompleterInterface) DynamicPrefixCompleterInterface (interface) *PrefixCompleter T : AutoCompleter func DynamicPrefixCompleterInterface.GetChildren() []PrefixCompleterInterface func (*PrefixCompleter).GetChildren() []PrefixCompleterInterface func PrefixCompleterInterface.GetChildren() []PrefixCompleterInterface func Do(p PrefixCompleterInterface, line []rune, pos int) (newLine [][]rune, offset int) func NewPrefixCompleter(pc ...PrefixCompleterInterface) *PrefixCompleter func PcItem(name string, pc ...PrefixCompleterInterface) *PrefixCompleter func PcItemDynamic(callback DynamicCompleteFunc, pc ...PrefixCompleterInterface) *PrefixCompleter func Print(p PrefixCompleterInterface, prefix string, level int, buf *bytes.Buffer) func DynamicPrefixCompleterInterface.SetChildren(children []PrefixCompleterInterface) func (*PrefixCompleter).SetChildren(children []PrefixCompleterInterface) func PrefixCompleterInterface.SetChildren(children []PrefixCompleterInterface)
(*T) Enter() (err error) (*T) Exit() error
(*T) Close() (*T) MarkIsTerminal(is bool) (*T) Serve() error (*T) ServeBy(source io.Reader) error (*T) Write(b []byte) (int, error) *T : io.Writer func NewRemoteCli(conn net.Conn) (*RemoteCli, error)
(*T) Close() error (*T) EnterRawMode() error (*T) ExitRawMode() error (*T) GetWidth() int (*T) GotIsTerminal(data []byte) (*T) GotReportWidth(data []byte) (*T) HandleConfig(cfg *Config) (*T) IsTerminal() bool (*T) Read(b []byte) (int, error) (*T) Write(b []byte) (int, error) *T : io.Closer *T : io.ReadCloser *T : io.Reader *T : io.ReadWriteCloser *T : io.ReadWriter *T : io.WriteCloser *T : io.Writer func NewRemoteSvr(conn net.Conn) (*RemoteSvr, error)
Error error Line string (*T) CanBreak() bool (*T) CanContinue() bool func (*Instance).Line() *Result
Mutex sync.Mutex (*T) BackEscapeWord() (*T) Backspace() (*T) Backup() (*T) Clean() (*T) CurrentWidth(x int) int (*T) CursorLineCount() int (*T) Delete() (success bool) (*T) DeleteWord() (*T) Erase() (*T) IdxLine(width int) int (*T) IsCursorInEnd() bool (*T) Kill() (*T) KillFront() (*T) Len() int (*T) LineCount(width int) int Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available. (*T) MoveBackward() (*T) MoveForward() (*T) MoveTo(ch rune, prevChar, reverse bool) (success bool) (*T) MoveToEndWord() (*T) MoveToLineEnd() (*T) MoveToLineStart() (*T) MoveToNextWord() (*T) MoveToPrevWord() (success bool) (*T) OnWidthChange(newWidth int) (*T) Pos() int (*T) PromptLen() int (*T) Refresh(f func()) (*T) Replace(ch rune) (*T) Reset() []rune (*T) Restore() (*T) RuneSlice(i int) []rune (*T) Runes() []rune (*T) Set(buf []rune) (*T) SetConfig(cfg *Config) (*T) SetMask(m rune) (*T) SetOffset(offset string) (*T) SetPrompt(prompt string) (*T) SetStyle(start, end int, style string) (*T) SetWithIdx(idx int, buf []rune) (*T) Transpose() Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock. A locked Mutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it. (*T) WriteRune(s rune) (*T) WriteRunes(s []rune) (*T) WriteString(s string) (*T) Yank() *T : sync.Locker func NewRuneBuffer(w io.Writer, prompt string, cfg *Config, width int) *RuneBuffer
( T) Aggregate(candicate [][]rune) (same []rune, size int) ( T) Backspace(r []rune) []byte ( T) ColorFilter(r []rune) []rune ( T) Copy(r []rune) []rune ( T) Equal(a, b []rune) bool ( T) EqualFold(a, b []rune) bool ( T) EqualRune(a, b rune, fold bool) bool ( T) EqualRuneFold(a, b rune) bool ( T) HasPrefix(r, prefix []rune) bool ( T) HasPrefixFold(r, prefix []rune) bool ( T) Index(r rune, rs []rune) int Search in runes from front to end Search in runes from end to front ( T) IndexAllBckEx(r, sub []rune, fold bool) int ( T) IndexAllEx(r, sub []rune, fold bool) int ( T) TrimSpaceLeft(in []rune) []rune ( T) Width(r rune) int ( T) WidthAll(r []rune) (length int)
SegmentCompleter SegmentCompleter (*T) Do(line []rune, pos int) (newLine [][]rune, offset int) 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] *T : AutoCompleter T : SegmentCompleter func SegmentAutoComplete(completer SegmentCompleter) *SegmentComplete
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] SegmentComplete func SegmentAutoComplete(completer SegmentCompleter) *SegmentComplete
State contains the state of a terminal. func GetState(fd int) (*State, error) func MakeRaw(fd int) (*State, error) func Restore(fd int, state *State) error
(*T) Do([]rune, int) ([][]rune, int) *T : AutoCompleter
(*T) Bell() (*T) Close() error (*T) EnterRawMode() (err error) (*T) ExitRawMode() (err error) (*T) GetConfig() *Config (*T) GetOffset(f func(offset string)) (*T) IsReading() bool (*T) KickRead() (*T) Print(s string) (*T) PrintRune(r rune) return rune(0) if meet EOF (*T) Readline() *Operation (*T) SetConfig(c *Config) error SleepToResume will sleep myself, and return only if I'm resumed. (*T) Write(b []byte) (int, error) WriteStdin prefill the next Stdin fetch Next time you call ReadLine() this value will be writen before the user input *T : io.Closer *T : io.WriteCloser *T : io.Writer func NewTerminal(cfg *Config) (*Terminal, error) func NewOperation(t *Terminal, cfg *Config) *Operation
Cc [32]uint8 Cflag uint32 Iflag uint32 Ispeed uint32 Lflag uint32 Line uint8 Oflag uint32 Ospeed uint32 Pad_cgo_0 [3]byte
Package-Level Functions (total 65, in which 48 are exported)
add history to global instance manually raise error only if `SetHistoryPath` is set with a non-empty path
ClearScreen clears the console screen
append log info to another file
func DefaultOnWidthChanged(f func())
func DialRemote(n, addr string) error
func Do(p PrefixCompleterInterface, line []rune, pos int) (newLine [][]rune, offset int)
func FuncListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) Listener
func GetInt(s []string, def int) int
GetSize returns the dimensions of the given terminal.
GetState returns the current state of a terminal which may be useful to restore the terminal after a signal.
func GetStdin() int
func HandleConn(cfg Config, conn net.Conn) (*Instance, error)
func IsPrintable(key rune) bool
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
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.
func New(prompt string) (*Instance, error)
func NewEx(cfg *Config) (*Instance, error)
NewFillableStdin gives you FillableStdin
func NewMessage(t MsgType, data []byte) *Message
func NewRuneBuffer(w io.Writer, prompt string, cfg *Config, width int) *RuneBuffer
func Password(prompt string) ([]byte, error)
func Print(p PrefixCompleterInterface, prefix string, level int, buf *bytes.Buffer)
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 Restore(fd int, state *State) error
func RetSegment(segments [][]rune, cands [][]rune, idx int) ([][]rune, int)
func SegmentFunc(f func([][]rune, int) [][]rune) 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 SplitByLine(start, screenWidth int, rs []rune) []string
func SplitSegment(line []rune, pos int) ([][]rune, int)
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
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.
Package-Level Variables (total 13, in which 5 are exported)
Package-Level Constants (total 46, in which 44 are exported)
const CharBackspace = 127
const CharBackward = 2
const CharBckSearch = 18
const CharBell = 7
const CharCtrlH = 8
const CharCtrlJ = 10
const CharCtrlL = 12
const CharCtrlU = 21
const CharCtrlW = 23
const CharCtrlY = 25
const CharCtrlZ = 26
const CharDelete = 4
const CharEnter = 13
const CharEsc = 27
const CharEscapeEx = 91
const CharForward = 6
const CharFwdSearch = 19
const CharInterrupt = 3
const CharKill = 11
const CharLineEnd = 5
const CharLineStart = 1
const CharNext = 14
const CharPrev = 16
const CharTab = 9
const CharTranspose = 20
const MetaBackspace rune = -4
const MetaBackward rune = -1
const MetaDelete rune = -3
const MetaForward rune = -2
const MetaTranspose rune = -5
const S_DIR_BCK = 0
const S_DIR_FWD = 1
const S_STATE_FAILING = 1
const S_STATE_FOUND = 0
const T_DATA MsgType = 0
const T_EOF MsgType = 6
const T_ERAW MsgType = 5 // exit raw
const T_RAW MsgType = 4
const T_WIDTH MsgType = 1
const VIM_INSERT = 1
const VIM_NORMAL = 0
const VIM_VISUAL = 2