readline: github.com/DanCripe/readline Index | Files | Directories

package readline

import "github.com/DanCripe/readline"

Readline is a pure go implementation for GNU-Readline kind library.

WHY: Readline will support most of features which GNU Readline is supported, and provide a pure go environment and a MIT license.

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)
}

Index

Package Files

char.go complete.go complete_helper.go debug.go doc.go history.go operation.go password.go readline.go runebuf.go search.go std.go terminal.go utils.go utils_unix.go vim.go

Constants

const (
    CharLineStart = 1
    CharBackward  = 2
    CharInterrupt = 3
    CharDelete    = 4
    CharLineEnd   = 5
    CharForward   = 6
    CharBell      = 7
    CharCtrlH     = 8
    CharTab       = 9
    CharCtrlJ     = 10
    CharKill      = 11
    CharEnter     = 13
    CharNext      = 14
    CharPrev      = 16
    CharBckSearch = 18
    CharFwdSearch = 19
    CharTranspose = 20
    CharCtrlU     = 21
    CharCtrlW     = 23
    CharEsc       = 27
    CharEscapeEx  = 91
    CharBackspace = 127
)
const (
    MetaBackward rune = -iota - 1
    MetaForward
    MetaDelete
    MetaBackspace
    MetaTranspose
)
const (
    S_STATE_FOUND = iota
    S_STATE_FAILING
)
const (
    S_DIR_BCK = iota
    S_DIR_FWD
)
const (
    VIM_NORMAL = iota
    VIM_INSERT
    VIM_VISUAL
)

Variables

var (
    Stdin  io.ReadCloser  = os.Stdin
    Stdout io.WriteCloser = os.Stdout
    Stderr io.WriteCloser = os.Stderr
)
var (
    ErrInterrupt = errors.New("Interrupt")
)
var (
    StdinFd = int(uintptr(syscall.Stdin))
)

func Debug Uses

func Debug(o ...interface{})

append log info to another file

func Do Uses

func Do(p PrefixCompleterInterface, line []rune, pos int) (newLine [][]rune, offset int)

func GetInt Uses

func GetInt(s []string, def int) int

func IsPrintable Uses

func IsPrintable(key rune) bool

func IsTerminal Uses

func IsTerminal(fd int) bool

IsTerminal returns true if the given file descriptor is a terminal.

func IsWordBreak Uses

func IsWordBreak(i rune) bool

func LineCount Uses

func LineCount(w int) int

calculate how many lines for N character

func MakeRaw Uses

func MakeRaw(fd int) (*terminal.State, error)

func Print Uses

func Print(p PrefixCompleterInterface, prefix string, level int, buf *bytes.Buffer)

func Restore Uses

func Restore(fd int, state *terminal.State) error

type AutoCompleter Uses

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:
    //   Do("g", 1) => ["o", "it", "it-shell", "rep"], 1
    //   Do("gi", 2) => ["t", "t-shell"], 1
    //   Do("git", 3) => ["", "-shell"], 0
    Do(line []rune, pos int) (newLine [][]rune, length int)
}

type Config Uses

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

    // 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

    // If VimMode is true, readline will in vim.insert mode by default
    VimMode bool

    InterruptPrompt string
    EOFPrompt       string

    Stdin  FdReader
    Stdout io.Writer
    Stderr io.Writer

    MaskRune rune
    // contains filtered or unexported fields
}

func (*Config) Init Uses

func (c *Config) Init() error

func (*Config) SetListener Uses

func (c *Config) SetListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool))

type DumpListener Uses

type DumpListener struct {
    // contains filtered or unexported fields
}

func (*DumpListener) OnChange Uses

func (d *DumpListener) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)

type FdReader Uses

type FdReader interface {
    io.Reader
    Fd() uintptr
}

type Instance Uses

type Instance struct {
    Config    *Config
    Terminal  *Terminal
    Operation *Operation
}

func New Uses

func New(prompt string) (*Instance, error)

func NewEx Uses

func NewEx(cfg *Config) (*Instance, error)

func (*Instance) Close Uses

func (i *Instance) Close() error

we must make sure that call Close() before process exit.

func (*Instance) GenPasswordConfig Uses

func (i *Instance) GenPasswordConfig() *Config

func (*Instance) IsVimMode Uses

func (i *Instance) IsVimMode() bool

func (*Instance) ReadPassword Uses

func (i *Instance) ReadPassword(prompt string) ([]byte, error)

func (*Instance) ReadPasswordEx Uses

func (i *Instance) ReadPasswordEx(prompt string, l Listener) ([]byte, error)

func (*Instance) ReadPasswordWithConfig Uses

func (i *Instance) ReadPasswordWithConfig(cfg *Config) ([]byte, error)

we can generate a config by `i.GenPasswordConfig()`

func (*Instance) ReadSlice Uses

func (i *Instance) ReadSlice() ([]byte, error)

same as readline

func (*Instance) Readline Uses

func (i *Instance) Readline() (string, error)

func (*Instance) Refresh Uses

func (i *Instance) Refresh()

func (*Instance) SetConfig Uses

func (i *Instance) SetConfig(cfg *Config) *Config

func (*Instance) SetHistoryPath Uses

func (i *Instance) SetHistoryPath(p string)

change hisotry persistence in runtime

func (*Instance) SetMaskRune Uses

func (i *Instance) SetMaskRune(r rune)

func (*Instance) SetPrompt Uses

func (i *Instance) SetPrompt(s string)

func (*Instance) SetVimMode Uses

func (i *Instance) SetVimMode(on bool)

switch VimMode in runtime

func (*Instance) Stderr Uses

func (i *Instance) Stderr() io.Writer

readline will refresh automatic when write through Stdout()

func (*Instance) Stdout Uses

func (i *Instance) Stdout() io.Writer

readline will refresh automatic when write through Stdout()

type Listener Uses

type Listener interface {
    OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
}

func FuncListener Uses

func FuncListener(f func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)) Listener

type Operation Uses

type Operation struct {
    // contains filtered or unexported fields
}

func NewOperation Uses

func NewOperation(t *Terminal, cfg *Config) *Operation

func (*Operation) Close Uses

func (o *Operation) Close()

func (Operation) CloseHistory Uses

func (o Operation) CloseHistory()

func (Operation) CompactHistory Uses

func (o Operation) CompactHistory()

func (Operation) CompleteRefresh Uses

func (o Operation) CompleteRefresh()

func (Operation) EnterCompleteMode Uses

func (o Operation) EnterCompleteMode(offset int, candidate [][]rune)

func (Operation) EnterCompleteSelectMode Uses

func (o Operation) EnterCompleteSelectMode()

func (Operation) EnterPasswordMode Uses

func (o Operation) EnterPasswordMode(cfg *Config) (err error)

func (Operation) EnterVimInsertMode Uses

func (o Operation) EnterVimInsertMode()

func (Operation) ExitCompleteMode Uses

func (o Operation) ExitCompleteMode(revent bool)

func (Operation) ExitCompleteSelectMode Uses

func (o Operation) ExitCompleteSelectMode()

func (Operation) ExitPasswordMode Uses

func (o Operation) ExitPasswordMode()

func (Operation) ExitSearchMode Uses

func (o Operation) ExitSearchMode(revert bool)

func (Operation) ExitVimInsertMode Uses

func (o Operation) ExitVimInsertMode()

func (Operation) ExitVimMode Uses

func (o Operation) ExitVimMode()

func (Operation) FindHistoryBck Uses

func (o Operation) FindHistoryBck(isNewSearch bool, rs []rune, start int) (int, *list.Element)

func (Operation) FindHistoryFwd Uses

func (o Operation) FindHistoryFwd(isNewSearch bool, rs []rune, start int) (int, *list.Element)

func (*Operation) GenPasswordConfig Uses

func (o *Operation) GenPasswordConfig() *Config

func (Operation) HandleCompleteSelect Uses

func (o Operation) HandleCompleteSelect(r rune) bool

func (Operation) HandleVim Uses

func (o Operation) HandleVim(r rune, readNext func() rune) rune

func (Operation) HandleVimNormal Uses

func (o Operation) HandleVimNormal(r rune, readNext func() rune) (t rune)

func (Operation) HistoryRewrite Uses

func (o Operation) HistoryRewrite()

func (Operation) InitHistory Uses

func (o Operation) InitHistory()

func (Operation) IsEnableVimMode Uses

func (o Operation) IsEnableVimMode() bool

func (Operation) IsHistoryClosed Uses

func (o Operation) IsHistoryClosed() bool

func (Operation) IsInCompleteMode Uses

func (o Operation) IsInCompleteMode() bool

func (Operation) IsInCompleteSelectMode Uses

func (o Operation) IsInCompleteSelectMode() bool

func (*Operation) IsNormalMode Uses

func (o *Operation) IsNormalMode() bool

func (Operation) IsSearchMode Uses

func (o Operation) IsSearchMode() bool

func (Operation) NewHistory Uses

func (o Operation) NewHistory(current []rune)

func (Operation) NextHistory Uses

func (o Operation) NextHistory() ([]rune, bool)

func (Operation) OnComplete Uses

func (o Operation) OnComplete()

func (*Operation) Password Uses

func (o *Operation) Password(prompt string) ([]byte, error)

func (Operation) PasswordConfig Uses

func (o Operation) PasswordConfig() *Config

func (*Operation) PasswordEx Uses

func (o *Operation) PasswordEx(prompt string, l Listener) ([]byte, error)

func (*Operation) PasswordWithConfig Uses

func (o *Operation) PasswordWithConfig(cfg *Config) ([]byte, error)

func (Operation) PrevHistory Uses

func (o Operation) PrevHistory() []rune

func (Operation) PushHistory Uses

func (o Operation) PushHistory(s []rune)

func (*Operation) Refresh Uses

func (o *Operation) Refresh()

func (Operation) RevertHistory Uses

func (o Operation) RevertHistory()

func (*Operation) Runes Uses

func (o *Operation) Runes() ([]rune, error)

func (Operation) SearchBackspace Uses

func (o Operation) SearchBackspace()

func (Operation) SearchChar Uses

func (o Operation) SearchChar(r rune)

func (Operation) SearchMode Uses

func (o Operation) SearchMode(dir int)

func (Operation) SearchRefresh Uses

func (o Operation) SearchRefresh(x int)

func (*Operation) SetConfig Uses

func (op *Operation) SetConfig(cfg *Config) (*Config, error)

func (*Operation) SetHistoryPath Uses

func (o *Operation) SetHistoryPath(path string)

func (*Operation) SetMaskRune Uses

func (o *Operation) SetMaskRune(r rune)

func (*Operation) SetPrompt Uses

func (o *Operation) SetPrompt(s string)

func (*Operation) SetTitle Uses

func (o *Operation) SetTitle(t string)

func (Operation) SetVimMode Uses

func (o Operation) SetVimMode(on bool)

func (*Operation) Slice Uses

func (o *Operation) Slice() ([]byte, error)

func (*Operation) Stderr Uses

func (o *Operation) Stderr() io.Writer

func (*Operation) Stdout Uses

func (o *Operation) Stdout() io.Writer

func (*Operation) String Uses

func (o *Operation) String() (string, error)

func (Operation) UpdateHistory Uses

func (o Operation) UpdateHistory(s []rune, commit bool)

type PrefixCompleter Uses

type PrefixCompleter struct {
    Name     []rune
    Children []PrefixCompleterInterface
}

func NewPrefixCompleter Uses

func NewPrefixCompleter(pc ...PrefixCompleterInterface) *PrefixCompleter

func PcItem Uses

func PcItem(name string, pc ...PrefixCompleterInterface) *PrefixCompleter

func (*PrefixCompleter) Do Uses

func (p *PrefixCompleter) Do(line []rune, pos int) (newLine [][]rune, offset int)

func (*PrefixCompleter) GetChildren Uses

func (p *PrefixCompleter) GetChildren() []PrefixCompleterInterface

func (*PrefixCompleter) GetName Uses

func (p *PrefixCompleter) GetName() []rune

func (*PrefixCompleter) Print Uses

func (p *PrefixCompleter) Print(prefix string, level int, buf *bytes.Buffer)

func (*PrefixCompleter) Tree Uses

func (p *PrefixCompleter) Tree(prefix string) string

type PrefixCompleterInterface Uses

type PrefixCompleterInterface interface {
    Print(prefix string, level int, buf *bytes.Buffer)
    Do(line []rune, pos int) (newLine [][]rune, length int)
    GetName() []rune
    GetChildren() []PrefixCompleterInterface
}

type RuneBuffer Uses

type RuneBuffer struct {
    // contains filtered or unexported fields
}

func NewRuneBuffer Uses

func NewRuneBuffer(w io.Writer, prompt string, mask rune) *RuneBuffer

func (*RuneBuffer) BackEscapeWord Uses

func (r *RuneBuffer) BackEscapeWord()

func (*RuneBuffer) Backspace Uses

func (r *RuneBuffer) Backspace()

func (*RuneBuffer) Backup Uses

func (r *RuneBuffer) Backup()

func (*RuneBuffer) Clean Uses

func (r *RuneBuffer) Clean()

func (*RuneBuffer) CurrentWidth Uses

func (r *RuneBuffer) CurrentWidth(x int) int

func (*RuneBuffer) CursorLineCount Uses

func (r *RuneBuffer) CursorLineCount() int

func (*RuneBuffer) Delete Uses

func (r *RuneBuffer) Delete() (success bool)

func (*RuneBuffer) DeleteWord Uses

func (r *RuneBuffer) DeleteWord()

func (*RuneBuffer) Erase Uses

func (r *RuneBuffer) Erase()

func (*RuneBuffer) IdxLine Uses

func (r *RuneBuffer) IdxLine() int

func (*RuneBuffer) Kill Uses

func (r *RuneBuffer) Kill()

func (*RuneBuffer) KillFront Uses

func (r *RuneBuffer) KillFront()

func (*RuneBuffer) Len Uses

func (r *RuneBuffer) Len() int

func (*RuneBuffer) LineCount Uses

func (r *RuneBuffer) LineCount() int

func (*RuneBuffer) MoveBackward Uses

func (r *RuneBuffer) MoveBackward()

func (*RuneBuffer) MoveForward Uses

func (r *RuneBuffer) MoveForward()

func (*RuneBuffer) MoveTo Uses

func (r *RuneBuffer) MoveTo(ch rune, prevChar, reverse bool) (success bool)

func (*RuneBuffer) MoveToLineEnd Uses

func (r *RuneBuffer) MoveToLineEnd()

func (*RuneBuffer) MoveToLineStart Uses

func (r *RuneBuffer) MoveToLineStart()

func (*RuneBuffer) MoveToNextWord Uses

func (r *RuneBuffer) MoveToNextWord()

func (*RuneBuffer) MoveToPrevWord Uses

func (r *RuneBuffer) MoveToPrevWord() (success bool)

func (*RuneBuffer) Pos Uses

func (r *RuneBuffer) Pos() int

func (*RuneBuffer) PromptLen Uses

func (r *RuneBuffer) PromptLen() int

func (*RuneBuffer) Refresh Uses

func (r *RuneBuffer) Refresh(f func())

func (*RuneBuffer) Reset Uses

func (r *RuneBuffer) Reset() []rune

func (*RuneBuffer) Restore Uses

func (r *RuneBuffer) Restore()

func (*RuneBuffer) RuneSlice Uses

func (r *RuneBuffer) RuneSlice(i int) []rune

func (*RuneBuffer) Runes Uses

func (r *RuneBuffer) Runes() []rune

func (*RuneBuffer) Set Uses

func (r *RuneBuffer) Set(buf []rune)

func (*RuneBuffer) SetMask Uses

func (r *RuneBuffer) SetMask(m rune)

func (*RuneBuffer) SetPrompt Uses

func (r *RuneBuffer) SetPrompt(prompt string)

func (*RuneBuffer) SetStyle Uses

func (r *RuneBuffer) SetStyle(start, end int, style string)

func (*RuneBuffer) SetWithIdx Uses

func (r *RuneBuffer) SetWithIdx(idx int, buf []rune)

func (*RuneBuffer) Transpose Uses

func (r *RuneBuffer) Transpose()

func (*RuneBuffer) WriteRune Uses

func (r *RuneBuffer) WriteRune(s rune)

func (*RuneBuffer) WriteRunes Uses

func (r *RuneBuffer) WriteRunes(s []rune)

func (*RuneBuffer) WriteString Uses

func (r *RuneBuffer) WriteString(s string)

type Terminal Uses

type Terminal struct {
    // contains filtered or unexported fields
}

func NewTerminal Uses

func NewTerminal(cfg *Config) (*Terminal, error)

func (*Terminal) Bell Uses

func (t *Terminal) Bell()

func (*Terminal) Close Uses

func (t *Terminal) Close() error

func (*Terminal) EnterRawMode Uses

func (t *Terminal) EnterRawMode() (err error)

func (*Terminal) ExitRawMode Uses

func (t *Terminal) ExitRawMode() (err error)

func (*Terminal) IsReading Uses

func (t *Terminal) IsReading() bool

func (*Terminal) KickRead Uses

func (t *Terminal) KickRead()

func (*Terminal) Print Uses

func (t *Terminal) Print(s string)

func (*Terminal) PrintRune Uses

func (t *Terminal) PrintRune(r rune)

func (*Terminal) ReadRune Uses

func (t *Terminal) ReadRune() rune

func (*Terminal) Readline Uses

func (t *Terminal) Readline() *Operation

func (*Terminal) SetConfig Uses

func (t *Terminal) SetConfig(c *Config) error

func (*Terminal) Write Uses

func (t *Terminal) Write(b []byte) (int, error)

Directories

PathSynopsis
runes

Package readline imports 16 packages (graph). Updated 2019-03-03. Refresh now. Tools for package owners.