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 {