From e2e8d94b147c164c9c0bf8c2b70e84eb3395657c Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 7 Mar 2021 11:30:26 +0900 Subject: [PATCH] Kill input command on terminate Fix #2381 Close #2382 --- src/constants.go | 1 + src/core.go | 6 +++++- src/terminal.go | 33 ++++++++++++++++++--------------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/constants.go b/src/constants.go index 9842e0b..96d9821 100644 --- a/src/constants.go +++ b/src/constants.go @@ -73,6 +73,7 @@ const ( EvtSearchFin EvtHeader EvtReady + EvtQuit ) const ( diff --git a/src/core.go b/src/core.go index a18c3a1..d9a83bd 100644 --- a/src/core.go +++ b/src/core.go @@ -254,7 +254,11 @@ func Run(opts *Options, version string, revision string) { } for evt, value := range *events { switch evt { - + case EvtQuit: + if reading { + reader.terminate() + } + os.Exit(value.(int)) case EvtReadNew, EvtReadFin: if evt == EvtReadFin && nextCommand != nil { restart(*nextCommand) diff --git a/src/terminal.go b/src/terminal.go index 93ba21b..aac43ce 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -1826,7 +1826,7 @@ func (t *Terminal) killPreview(code int) { case t.killChan <- code: default: if code != exitCancel { - os.Exit(code) + t.eventBox.Set(EvtQuit, code) } } } @@ -1835,6 +1835,16 @@ func (t *Terminal) cancelPreview() { t.killPreview(exitCancel) } +func (t *Terminal) exit(getCode func() int) { + t.tui.Close() + code := getCode() + if code <= exitNoMatch && t.history != nil { + t.history.append(string(t.input)) + } + // prof.Stop() + t.killPreview(code) +} + // Loop is called to start Terminal I/O func (t *Terminal) Loop() { // prof := profile.Start(profile.ProfilePath("/tmp/")) @@ -2010,7 +2020,7 @@ func (t *Terminal) Loop() { case code := <-t.killChan: if code != exitCancel { util.KillCommand(cmd) - os.Exit(code) + t.eventBox.Set(EvtQuit, code) } else { timer := time.NewTimer(previewCancelWait) select { @@ -2047,16 +2057,6 @@ func (t *Terminal) Loop() { }() } - exit := func(getCode func() int) { - t.tui.Close() - code := getCode() - if code <= exitNoMatch && t.history != nil { - t.history.append(string(t.input)) - } - // prof.Stop() - t.killPreview(code) - } - refreshPreview := func(command string) { if len(command) > 0 && t.isPreviewEnabled() { _, list := t.buildPlusList(command, false) @@ -2108,12 +2108,13 @@ func (t *Terminal) Loop() { case reqRedraw: t.redraw() case reqClose: - exit(func() int { + t.exit(func() int { if t.output() { return exitOk } return exitNoMatch }) + return case reqPreviewDisplay: result := value.(previewResult) if t.previewer.version != result.version { @@ -2134,12 +2135,14 @@ func (t *Terminal) Loop() { t.previewer.version = value.(int64) t.printPreviewDelayed() case reqPrintQuery: - exit(func() int { + t.exit(func() int { t.printer(string(t.input)) return exitOk }) + return case reqQuit: - exit(func() int { return exitInterrupt }) + t.exit(func() int { return exitInterrupt }) + return } } t.refresh()