Kill input command on terminate

Fix #2381
Close #2382
This commit is contained in:
Junegunn Choi
2021-03-07 11:30:26 +09:00
parent 4f9a7f8c87
commit e2e8d94b14
3 changed files with 24 additions and 16 deletions

View File

@@ -73,6 +73,7 @@ const (
EvtSearchFin EvtSearchFin
EvtHeader EvtHeader
EvtReady EvtReady
EvtQuit
) )
const ( const (

View File

@@ -254,7 +254,11 @@ func Run(opts *Options, version string, revision string) {
} }
for evt, value := range *events { for evt, value := range *events {
switch evt { switch evt {
case EvtQuit:
if reading {
reader.terminate()
}
os.Exit(value.(int))
case EvtReadNew, EvtReadFin: case EvtReadNew, EvtReadFin:
if evt == EvtReadFin && nextCommand != nil { if evt == EvtReadFin && nextCommand != nil {
restart(*nextCommand) restart(*nextCommand)

View File

@@ -1826,7 +1826,7 @@ func (t *Terminal) killPreview(code int) {
case t.killChan <- code: case t.killChan <- code:
default: default:
if code != exitCancel { if code != exitCancel {
os.Exit(code) t.eventBox.Set(EvtQuit, code)
} }
} }
} }
@@ -1835,6 +1835,16 @@ func (t *Terminal) cancelPreview() {
t.killPreview(exitCancel) 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 // Loop is called to start Terminal I/O
func (t *Terminal) Loop() { func (t *Terminal) Loop() {
// prof := profile.Start(profile.ProfilePath("/tmp/")) // prof := profile.Start(profile.ProfilePath("/tmp/"))
@@ -2010,7 +2020,7 @@ func (t *Terminal) Loop() {
case code := <-t.killChan: case code := <-t.killChan:
if code != exitCancel { if code != exitCancel {
util.KillCommand(cmd) util.KillCommand(cmd)
os.Exit(code) t.eventBox.Set(EvtQuit, code)
} else { } else {
timer := time.NewTimer(previewCancelWait) timer := time.NewTimer(previewCancelWait)
select { 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) { refreshPreview := func(command string) {
if len(command) > 0 && t.isPreviewEnabled() { if len(command) > 0 && t.isPreviewEnabled() {
_, list := t.buildPlusList(command, false) _, list := t.buildPlusList(command, false)
@@ -2108,12 +2108,13 @@ func (t *Terminal) Loop() {
case reqRedraw: case reqRedraw:
t.redraw() t.redraw()
case reqClose: case reqClose:
exit(func() int { t.exit(func() int {
if t.output() { if t.output() {
return exitOk return exitOk
} }
return exitNoMatch return exitNoMatch
}) })
return
case reqPreviewDisplay: case reqPreviewDisplay:
result := value.(previewResult) result := value.(previewResult)
if t.previewer.version != result.version { if t.previewer.version != result.version {
@@ -2134,12 +2135,14 @@ func (t *Terminal) Loop() {
t.previewer.version = value.(int64) t.previewer.version = value.(int64)
t.printPreviewDelayed() t.printPreviewDelayed()
case reqPrintQuery: case reqPrintQuery:
exit(func() int { t.exit(func() int {
t.printer(string(t.input)) t.printer(string(t.input))
return exitOk return exitOk
}) })
return
case reqQuit: case reqQuit:
exit(func() int { return exitInterrupt }) t.exit(func() int { return exitInterrupt })
return
} }
} }
t.refresh() t.refresh()