vendor: Update chzyer/readline

Still using special/readline's refresh-race branch, but it was rebased
on the latest upstream master.
This commit is contained in:
John Brooks 2017-09-21 06:56:07 -06:00
parent 23e22f0260
commit 9bd509c3d8
12 changed files with 178 additions and 51 deletions

View File

@ -61,6 +61,15 @@ var completer = readline.NewPrefixCompleter(
readline.PcItem("sleep"), readline.PcItem("sleep"),
) )
func filterInput(r rune) (rune, bool) {
switch r {
// block CtrlZ feature
case readline.CharCtrlZ:
return r, false
}
return r, true
}
func main() { func main() {
l, err := readline.NewEx(&readline.Config{ l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ", Prompt: "\033[31m»\033[0m ",
@ -69,7 +78,8 @@ func main() {
InterruptPrompt: "^C", InterruptPrompt: "^C",
EOFPrompt: "exit", EOFPrompt: "exit",
HistorySearchFold: true, HistorySearchFold: true,
FuncFilterInputRune: filterInput,
}) })
if err != nil { if err != nil {
panic(err) panic(err)
@ -127,12 +137,11 @@ func main() {
println("you set:", strconv.Quote(string(pswd))) println("you set:", strconv.Quote(string(pswd)))
} }
case strings.HasPrefix(line, "setprompt"): case strings.HasPrefix(line, "setprompt"):
prompt := line[10:] if len(line) <= 10 {
if prompt == "" {
log.Println("setprompt <prompt>") log.Println("setprompt <prompt>")
break break
} }
l.SetPrompt(prompt) l.SetPrompt(line[10:])
case strings.HasPrefix(line, "say"): case strings.HasPrefix(line, "say"):
line := strings.TrimSpace(line[3:]) line := strings.TrimSpace(line[3:])
if len(line) == 0 { if len(line) == 0 {

View File

@ -32,6 +32,10 @@ type Operation struct {
*opVim *opVim
} }
func (o *Operation) SetBuffer(what string) {
o.buf.Set([]rune(what))
}
type wrapWriter struct { type wrapWriter struct {
r *Operation r *Operation
t *Terminal t *Terminal
@ -92,6 +96,15 @@ func (o *Operation) ioloop() {
keepInSearchMode := false keepInSearchMode := false
keepInCompleteMode := false keepInCompleteMode := false
r := o.t.ReadRune() r := o.t.ReadRune()
if o.cfg.FuncFilterInputRune != nil {
var process bool
r, process = o.cfg.FuncFilterInputRune(r)
if !process {
o.buf.Refresh(nil) // to refresh the line
continue // ignore this rune
}
}
if r == 0 { // io.EOF if r == 0 { // io.EOF
if o.buf.Len() == 0 { if o.buf.Len() == 0 {
o.buf.Clean() o.buf.Clean()

View File

@ -63,6 +63,10 @@ type Config struct {
// it use in IM usually. // it use in IM usually.
UniqueEditLine bool 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 // force use interactive even stdout is not a tty
FuncIsTerminal func() bool FuncIsTerminal func() bool
FuncMakeRaw func() error FuncMakeRaw func() error
@ -238,6 +242,11 @@ func (i *Instance) Readline() (string, error) {
return i.Operation.String() return i.Operation.String()
} }
func (i *Instance) ReadlineWithDefault(what string) (string, error) {
i.Operation.SetBuffer(what)
return i.Operation.String()
}
func (i *Instance) SaveHistory(content string) error { func (i *Instance) SaveHistory(content string) error {
return i.Operation.SaveHistory(content) return i.Operation.SaveHistory(content)
} }

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux,!appengine netbsd openbsd // +build darwin dragonfly freebsd linux,!appengine netbsd openbsd solaris
// Package terminal provides support functions for dealing with terminals, as // Package terminal provides support functions for dealing with terminals, as
// commonly found on UNIX systems. // commonly found on UNIX systems.
@ -19,19 +19,17 @@ package readline
import ( import (
"io" "io"
"syscall" "syscall"
"unsafe"
) )
// State contains the state of a terminal. // State contains the state of a terminal.
type State struct { type State struct {
termios syscall.Termios termios Termios
} }
// IsTerminal returns true if the given file descriptor is a terminal. // IsTerminal returns true if the given file descriptor is a terminal.
func IsTerminal(fd int) bool { func IsTerminal(fd int) bool {
var termios syscall.Termios _, err := getTermios(fd)
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&termios)), 0, 0, 0) return err == nil
return err == 0
} }
// MakeRaw put the terminal connected to the given file descriptor into raw // MakeRaw put the terminal connected to the given file descriptor into raw
@ -39,8 +37,11 @@ func IsTerminal(fd int) bool {
// restored. // restored.
func MakeRaw(fd int) (*State, error) { func MakeRaw(fd int) (*State, error) {
var oldState State var oldState State
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
if termios, err := getTermios(fd); err != nil {
return nil, err return nil, err
} else {
oldState.termios = *termios
} }
newState := oldState.termios newState := oldState.termios
@ -52,47 +53,35 @@ func MakeRaw(fd int) (*State, error) {
newState.Cflag &^= syscall.CSIZE | syscall.PARENB newState.Cflag &^= syscall.CSIZE | syscall.PARENB
newState.Cflag |= syscall.CS8 newState.Cflag |= syscall.CS8
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 { newState.Cc[syscall.VMIN] = 1
return nil, err newState.Cc[syscall.VTIME] = 0
}
return &oldState, nil return &oldState, setTermios(fd, &newState)
} }
// GetState returns the current state of a terminal which may be useful to // GetState returns the current state of a terminal which may be useful to
// restore the terminal after a signal. // restore the terminal after a signal.
func GetState(fd int) (*State, error) { func GetState(fd int) (*State, error) {
var oldState State termios, err := getTermios(fd)
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 { if err != nil {
return nil, err return nil, err
} }
return &oldState, nil return &State{termios: *termios}, nil
} }
// Restore restores the terminal connected to the given file descriptor to a // Restore restores the terminal connected to the given file descriptor to a
// previous state. // previous state.
func restoreTerm(fd int, state *State) error { func restoreTerm(fd int, state *State) error {
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&state.termios)), 0, 0, 0) return setTermios(fd, &state.termios)
return err
}
// GetSize returns the dimensions of the given terminal.
func GetSize(fd int) (width, height int, err error) {
var dimensions [4]uint16
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0); err != 0 {
return -1, -1, err
}
return int(dimensions[1]), int(dimensions[0]), nil
} }
// ReadPassword reads a line of input from a terminal without local echo. This // 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 // is commonly used for inputting passwords and other sensitive data. The slice
// returned does not include the \n. // returned does not include the \n.
func ReadPassword(fd int) ([]byte, error) { func ReadPassword(fd int) ([]byte, error) {
var oldState syscall.Termios oldState, err := getTermios(fd)
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0); err != 0 { if err != nil {
return nil, err return nil, err
} }
@ -100,12 +89,12 @@ func ReadPassword(fd int) ([]byte, error) {
newState.Lflag &^= syscall.ECHO newState.Lflag &^= syscall.ECHO
newState.Lflag |= syscall.ICANON | syscall.ISIG newState.Lflag |= syscall.ICANON | syscall.ISIG
newState.Iflag |= syscall.ICRNL newState.Iflag |= syscall.ICRNL
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 { if err := setTermios(fd, newState); err != nil {
return nil, err return nil, err
} }
defer func() { defer func() {
syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(&oldState)), 0, 0, 0) setTermios(fd, oldState)
}() }()
var buf [16]byte var buf [16]byte

View File

@ -6,7 +6,24 @@
package readline package readline
import "syscall" import (
"syscall"
"unsafe"
)
const ioctlReadTermios = syscall.TIOCGETA func getTermios(fd int) (*Termios, error) {
const ioctlWriteTermios = syscall.TIOCSETA termios := new(Termios)
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TIOCGETA, uintptr(unsafe.Pointer(termios)), 0, 0, 0)
if err != 0 {
return nil, err
}
return termios, nil
}
func setTermios(fd int, termios *Termios) error {
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TIOCSETA, uintptr(unsafe.Pointer(termios)), 0, 0, 0)
if err != 0 {
return err
}
return nil
}

View File

@ -4,8 +4,30 @@
package readline package readline
import (
"syscall"
"unsafe"
)
// These constants are declared here, rather than importing // These constants are declared here, rather than importing
// them from the syscall package as some syscall packages, even // them from the syscall package as some syscall packages, even
// on linux, for example gccgo, do not declare them. // on linux, for example gccgo, do not declare them.
const ioctlReadTermios = 0x5401 // syscall.TCGETS const ioctlReadTermios = 0x5401 // syscall.TCGETS
const ioctlWriteTermios = 0x5402 // syscall.TCSETS const ioctlWriteTermios = 0x5402 // syscall.TCSETS
func getTermios(fd int) (*Termios, error) {
termios := new(Termios)
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlReadTermios, uintptr(unsafe.Pointer(termios)), 0, 0, 0)
if err != 0 {
return nil, err
}
return termios, nil
}
func setTermios(fd int, termios *Termios) error {
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), ioctlWriteTermios, uintptr(unsafe.Pointer(termios)), 0, 0, 0)
if err != 0 {
return err
}
return nil
}

32
vendor/github.com/chzyer/readline/term_solaris.go generated vendored Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build solaris
package readline
import "golang.org/x/sys/unix"
// GetSize returns the dimensions of the given terminal.
func GetSize(fd int) (int, int, error) {
ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
if err != nil {
return 0, 0, err
}
return int(ws.Col), int(ws.Row), nil
}
type Termios unix.Termios
func getTermios(fd int) (*Termios, error) {
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil {
return nil, err
}
return (*Termios)(termios), nil
}
func setTermios(fd int, termios *Termios) error {
return unix.IoctlSetTermios(fd, unix.TCSETSF, (*unix.Termios)(termios))
}

24
vendor/github.com/chzyer/readline/term_unix.go generated vendored Normal file
View File

@ -0,0 +1,24 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin dragonfly freebsd linux,!appengine netbsd openbsd
package readline
import (
"syscall"
"unsafe"
)
type Termios syscall.Termios
// GetSize returns the dimensions of the given terminal.
func GetSize(fd int) (int, int, error) {
var dimensions [4]uint16
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dimensions)), 0, 0, 0)
if err != 0 {
return 0, 0, err
}
return int(dimensions[1]), int(dimensions[0]), nil
}

View File

@ -10,6 +10,7 @@ import (
) )
type Terminal struct { type Terminal struct {
m sync.Mutex
cfg *Config cfg *Config
outchan chan rune outchan chan rune
closed int32 closed int32
@ -121,7 +122,7 @@ func (t *Terminal) ioloop() {
expectNextChar bool expectNextChar bool
) )
buf := bufio.NewReader(t.cfg.Stdin) buf := bufio.NewReader(t.getStdin())
for { for {
if !expectNextChar { if !expectNextChar {
atomic.StoreInt32(&t.isReading, 0) atomic.StoreInt32(&t.isReading, 0)
@ -206,10 +207,26 @@ func (t *Terminal) Close() error {
return t.ExitRawMode() return t.ExitRawMode()
} }
func (t *Terminal) GetConfig() *Config {
t.m.Lock()
cfg := *t.cfg
t.m.Unlock()
return &cfg
}
func (t *Terminal) getStdin() io.Reader {
t.m.Lock()
r := t.cfg.Stdin
t.m.Unlock()
return r
}
func (t *Terminal) SetConfig(c *Config) error { func (t *Terminal) SetConfig(c *Config) error {
if err := c.Init(); err != nil { if err := c.Init(); err != nil {
return err return err
} }
t.m.Lock()
t.cfg = c t.cfg = c
t.m.Unlock()
return nil return nil
} }

View File

@ -82,7 +82,9 @@ func Restore(fd int, state *State) error {
if err != nil { if err != nil {
// errno 0 means everything is ok :) // errno 0 means everything is ok :)
if err.Error() == "errno 0" { if err.Error() == "errno 0" {
err = nil return nil
} else {
return err
} }
} }
return nil return nil

View File

@ -1,4 +1,4 @@
// +build darwin dragonfly freebsd linux,!appengine netbsd openbsd // +build darwin dragonfly freebsd linux,!appengine netbsd openbsd solaris
package readline package readline
@ -8,7 +8,6 @@ import (
"os/signal" "os/signal"
"sync" "sync"
"syscall" "syscall"
"unsafe"
) )
type winsize struct { type winsize struct {
@ -30,17 +29,11 @@ func SuspendMe() {
// get width of the terminal // get width of the terminal
func getWidth(stdoutFd int) int { func getWidth(stdoutFd int) int {
ws := &winsize{} cols, _, err := GetSize(stdoutFd)
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL, if err != nil {
uintptr(stdoutFd),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
_ = errno
return -1 return -1
} }
return int(ws.Col) return cols
} }
func GetScreenWidth() int { func GetScreenWidth() int {

2
vendor/manifest vendored
View File

@ -23,7 +23,7 @@
"importpath": "github.com/chzyer/readline", "importpath": "github.com/chzyer/readline",
"repository": "https://github.com/special/readline", "repository": "https://github.com/special/readline",
"vcs": "git", "vcs": "git",
"revision": "435ac8270991572ea8df6de2add266b6d818d9f3", "revision": "e06700a8ff17afd011891f52e148da8dcf84ee2f",
"branch": "refresh-race", "branch": "refresh-race",
"notests": true "notests": true
}, },