Don't exit fzf by SIGINT while executing command (#2375)

Fix #2374

Co-authored-by: Junegunn Choi <junegunn.c@gmail.com>
This commit is contained in:
bitterfox 2021-02-28 21:01:03 +09:00 committed by GitHub
parent bb0502ff44
commit 4f9a7f8c87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -157,6 +157,7 @@ type Terminal struct {
slab *util.Slab slab *util.Slab
theme *tui.ColorTheme theme *tui.ColorTheme
tui tui.Renderer tui tui.Renderer
executing *util.AtomicBool
} }
type selectedItem struct { type selectedItem struct {
@ -525,7 +526,8 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
startChan: make(chan bool, 1), startChan: make(chan bool, 1),
killChan: make(chan int), killChan: make(chan int),
tui: renderer, tui: renderer,
initFunc: func() { renderer.Init() }} initFunc: func() { renderer.Init() },
executing: util.NewAtomicBool(false)}
t.prompt, t.promptLen = t.parsePrompt(opts.Prompt) t.prompt, t.promptLen = t.parsePrompt(opts.Prompt)
t.pointer, t.pointerLen = t.processTabs([]rune(opts.Pointer), 0) t.pointer, t.pointerLen = t.processTabs([]rune(opts.Pointer), 0)
t.marker, t.markerLen = t.processTabs([]rune(opts.Marker), 0) t.marker, t.markerLen = t.processTabs([]rune(opts.Marker), 0)
@ -1713,13 +1715,17 @@ func (t *Terminal) executeCommand(template string, forcePlus bool, background bo
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
t.tui.Pause(true) t.tui.Pause(true)
t.executing.Set(true)
cmd.Run() cmd.Run()
t.executing.Set(false)
t.tui.Resume(true, false) t.tui.Resume(true, false)
t.redraw() t.redraw()
t.refresh() t.refresh()
} else { } else {
t.tui.Pause(false) t.tui.Pause(false)
t.executing.Set(true)
cmd.Run() cmd.Run()
t.executing.Set(false)
t.tui.Resume(false, false) t.tui.Resume(false, false)
} }
cleanTemporaryFiles() cleanTemporaryFiles()
@ -1837,8 +1843,12 @@ func (t *Terminal) Loop() {
intChan := make(chan os.Signal, 1) intChan := make(chan os.Signal, 1)
signal.Notify(intChan, os.Interrupt, syscall.SIGTERM) signal.Notify(intChan, os.Interrupt, syscall.SIGTERM)
go func() { go func() {
<-intChan for s := range intChan {
t.reqBox.Set(reqQuit, nil) // Don't quit by SIGINT while executing because it should be for the executing command and not for fzf itself
if !(s == os.Interrupt && t.executing.Get()) {
t.reqBox.Set(reqQuit, nil)
}
}
}() }()
contChan := make(chan os.Signal, 1) contChan := make(chan os.Signal, 1)

View File

@ -1911,6 +1911,17 @@ class TestGoFZF < TestBase
tmux.send_keys :Down tmux.send_keys :Down
tmux.until { |lines| assert_equal 2, lines.select_count } tmux.until { |lines| assert_equal 2, lines.select_count }
end end
def test_interrupt_execute
tmux.send_keys "seq 100 | #{FZF} --bind 'ctrl-l:execute:echo executing {}; sleep 100'", :Enter
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.send_keys 'C-l'
tmux.until { |lines| assert lines.any_include?('executing 1') }
tmux.send_keys 'C-c'
tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.send_keys 99
tmux.until { |lines| assert_equal 1, lines.match_count }
end
end end
module TestShell module TestShell