forked from mirror/readline
return remain line if interrupt.
This commit is contained in:
parent
bb5b4af6e7
commit
1e409caaf3
|
@ -47,7 +47,7 @@ func main() {
|
|||
Prompt: "\033[31m»\033[0m ",
|
||||
HistoryFile: "/tmp/readline.tmp",
|
||||
AutoComplete: completer,
|
||||
InterruptPrompt: "\nInterrupt, Press Ctrl+D to exit",
|
||||
InterruptPrompt: "^C",
|
||||
EOFPrompt: "exit",
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -65,9 +65,16 @@ func main() {
|
|||
log.SetOutput(l.Stderr())
|
||||
for {
|
||||
line, err := l.Readline()
|
||||
if err == io.EOF {
|
||||
if err == readline.ErrInterrupt {
|
||||
if len(line) == 0 {
|
||||
break
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
} else if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
line = strings.TrimSpace(line)
|
||||
switch {
|
||||
case strings.HasPrefix(line, "mode "):
|
||||
|
|
24
operation.go
24
operation.go
|
@ -9,6 +9,14 @@ var (
|
|||
ErrInterrupt = errors.New("Interrupt")
|
||||
)
|
||||
|
||||
type InterruptError struct {
|
||||
Line []rune
|
||||
}
|
||||
|
||||
func (*InterruptError) Error() string {
|
||||
return "Interrupted"
|
||||
}
|
||||
|
||||
type Operation struct {
|
||||
cfg *Config
|
||||
t *Terminal
|
||||
|
@ -248,11 +256,13 @@ func (o *Operation) ioloop() {
|
|||
}
|
||||
o.buf.MoveToLineEnd()
|
||||
o.buf.Refresh(nil)
|
||||
o.buf.WriteString(o.cfg.InterruptPrompt + "\n")
|
||||
o.buf.Reset()
|
||||
hint := o.cfg.InterruptPrompt + "\n"
|
||||
o.buf.WriteString(hint)
|
||||
remain := o.buf.Reset()
|
||||
remain = remain[:len(remain)-len([]rune(hint))]
|
||||
isUpdateHistory = false
|
||||
o.history.Revert()
|
||||
o.errchan <- ErrInterrupt
|
||||
o.errchan <- &InterruptError{remain}
|
||||
default:
|
||||
if o.IsSearchMode() {
|
||||
o.SearchChar(r)
|
||||
|
@ -302,10 +312,7 @@ func (o *Operation) Stdout() io.Writer {
|
|||
|
||||
func (o *Operation) String() (string, error) {
|
||||
r, err := o.Runes()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(r), nil
|
||||
return string(r), err
|
||||
}
|
||||
|
||||
func (o *Operation) Runes() ([]rune, error) {
|
||||
|
@ -321,6 +328,9 @@ func (o *Operation) Runes() ([]rune, error) {
|
|||
case r := <-o.outchan:
|
||||
return r, nil
|
||||
case err := <-o.errchan:
|
||||
if e, ok := err.(*InterruptError); ok {
|
||||
return e.Line, ErrInterrupt
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue