return remain line if interrupt.

This commit is contained in:
chzyer 2016-03-25 17:08:13 +08:00
parent bb5b4af6e7
commit 1e409caaf3
2 changed files with 26 additions and 9 deletions

View File

@ -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 "):

View File

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