From af66dc48f7e93ae876cd963fdfe77655651c3e60 Mon Sep 17 00:00:00 2001 From: Cheney Date: Fri, 25 Sep 2015 00:49:55 +0800 Subject: [PATCH] fix Ctrl+C in search mode do not send EOF --- operation.go | 4 +++- readline.go | 5 ++++- terminal.go | 20 ++++++++------------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/operation.go b/operation.go index 7970b4d..2080402 100644 --- a/operation.go +++ b/operation.go @@ -114,7 +114,9 @@ func (o *Operation) ioloop() { } case CharInterrupt: if o.IsSearchMode() { - o.ExitSearchMode(false) + o.t.KickRead() + o.ExitSearchMode(true) + break } o.buf.MoveToLineEnd() o.buf.Refresh() diff --git a/readline.go b/readline.go index c23b6c5..e799b62 100644 --- a/readline.go +++ b/readline.go @@ -45,6 +45,9 @@ func (i *Instance) ReadSlice() ([]byte, error) { } func (i *Instance) Close() error { + if err := i.t.Close(); err != nil { + return err + } i.o.Close() - return i.t.Close() + return nil } diff --git a/terminal.go b/terminal.go index bdc137a..9165221 100644 --- a/terminal.go +++ b/terminal.go @@ -19,7 +19,7 @@ type Terminal struct { stopChan chan struct{} kickChan chan struct{} wg sync.WaitGroup - isReading bool + isReading int64 } func NewTerminal(cfg *Config) (*Terminal, error) { @@ -60,7 +60,7 @@ func (t *Terminal) ReadRune() rune { } func (t *Terminal) IsReading() bool { - return t.isReading + return atomic.LoadInt64(&t.isReading) == 1 } func (t *Terminal) KickRead() { @@ -82,10 +82,10 @@ func (t *Terminal) ioloop() { buf := bufio.NewReader(os.Stdin) for { if !expectNextChar { - t.isReading = false + atomic.StoreInt64(&t.isReading, 0) select { case <-t.kickChan: - t.isReading = true + atomic.StoreInt64(&t.isReading, 1) case <-t.stopChan: return } @@ -109,21 +109,17 @@ func (t *Terminal) ioloop() { r = escapeExKey(r) } + expectNextChar = true switch r { - case CharInterrupt: - t.outchan <- r - goto exit case CharEsc: isEscape = true - expectNextChar = true - case CharEnter, CharCtrlJ: - t.outchan <- r + case CharInterrupt, CharEnter, CharCtrlJ: + expectNextChar = false + fallthrough default: - expectNextChar = true t.outchan <- r } } -exit: } func (t *Terminal) Close() error {