Kill running preview process after 500ms when focus has changed
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #1383 Close #1384
authored by Junegunn Choi · committed by Junegunn Choi · parent 27c40dc · Browse files · 5 files·+82 added−18 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+82 added−18 removed
- other:+82 added−18 removed
Provenance
- Commit
0d748a0- Tree
791e484- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
27c40dc
5 files changed+82 added−18 removed
modified:src/util/util_unix.go+14 added-5 removed
7 unmodified lines8 "syscall"9 )10 11 // ExecCommand executes the given command with $SHELL12-func ExecCommand(command string) *exec.Cmd {12+func ExecCommand(command string, setpgid bool) *exec.Cmd {13 shell := os.Getenv("SHELL")14 if len(shell) == 0 {15 shell = "sh"16 }17- return ExecCommandWith(shell, command)17+ return ExecCommandWith(shell, command, setpgid)18 }19 20 // ExecCommandWith executes the given command with the specified shell21-func ExecCommandWith(shell string, command string) *exec.Cmd {22- return exec.Command(shell, "-c", command)21+func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd {22+ cmd := exec.Command(shell, "-c", command)23+ if setpgid {24+ cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}25+ }26+ return cmd27 }28 29+// KillCommand kills the process for the given command30+func KillCommand(cmd *exec.Cmd) error {31+ return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)32+}33+34 // IsWindows returns true on Windows35 func IsWindows() bool {36 return false37 }38 30-// SetNonBlock executes syscall.SetNonblock on file descriptor39+// SetNonblock executes syscall.SetNonblock on file descriptor40 func SetNonblock(file *os.File, nonblock bool) {41 syscall.SetNonblock(int(file.Fd()), nonblock)42 }43 4 unmodified linesmodified:src/util/util_windows.go+10 added-3 removed
8 unmodified lines9 "syscall"10 )11 12 // ExecCommand executes the given command with cmd13-func ExecCommand(command string) *exec.Cmd {13+func ExecCommand(command string, setpgid bool) *exec.Cmd {14 return ExecCommandWith("cmd", command)15 }16 17 // ExecCommandWith executes the given command with cmd. _shell parameter is18 // ignored on Windows.19+// FIXME: setpgid is unused. We set it in the Unix implementation so that we20+// can kill preview process with its child processes at once.19-func ExecCommandWith(_shell string, command string) *exec.Cmd {21+func ExecCommandWith(_shell string, command string, setpgid bool) *exec.Cmd {22 cmd := exec.Command("cmd")23 cmd.SysProcAttr = &syscall.SysProcAttr{24 HideWindow: false,25 CmdLine: fmt.Sprintf(` /v:on/s/c "%s"`, command),1 unmodified line27 }28 return cmd29 }30 31+// KillCommand kills the process for the given command32+func KillCommand(cmd *exec.Cmd) error {33+ return cmd.Process.Kill()34+}35+36 // IsWindows returns true on Windows37 func IsWindows() bool {38 return true39 }40 34-// SetNonBlock executes syscall.SetNonblock on file descriptor41+// SetNonblock executes syscall.SetNonblock on file descriptor42 func SetNonblock(file *os.File, nonblock bool) {43 syscall.SetNonblock(syscall.Handle(file.Fd()), nonblock)44 }45 4 unmodified linesmodified:src/constants.go+6 added-4 removed
20 unmodified lines21 readerPollIntervalStep = 5 * time.Millisecond22 readerPollIntervalMax = 50 * time.Millisecond23 24 // Terminal25- initialDelay = 20 * time.Millisecond26- initialDelayTac = 100 * time.Millisecond27- spinnerDuration = 200 * time.Millisecond28- maxPatternLength = 30025+ initialDelay = 20 * time.Millisecond26+ initialDelayTac = 100 * time.Millisecond27+ spinnerDuration = 200 * time.Millisecond28+ previewCancelWait = 500 * time.Millisecond29+ maxPatternLength = 30030 31 // Matcher32 numPartitionsMultiplier = 833 maxPartitions = 3242 unmodified lines76 EvtReady77 )78 79 const (80+ exitCancel = -181 exitOk = 082 exitNoMatch = 183 exitError = 284 exitInterrupt = 1301 unmodified linemodified:src/reader.go+1 added-1 removed
101 unmodified lines102 return true103 }104 105 func (r *Reader) readFromCommand(shell string, cmd string) bool {106- listCommand := util.ExecCommandWith(shell, cmd)106+ listCommand := util.ExecCommandWith(shell, cmd, false)107 out, err := listCommand.StdoutPipe()108 if err != nil {109 return false110 }7 unmodified linesmodified:src/terminal.go+51 added-5 removed
112 unmodified lines113 initFunc func()114 prevLines []itemLine115 suppress bool116 startChan chan bool117+ killChan chan int118 slab *util.Slab119 theme *tui.ColorTheme120 tui tui.Renderer121 }292 unmodified lines414 suppress: true,415 slab: util.MakeSlab(slab16Size, slab32Size),416 theme: opts.Theme,417 startChan: make(chan bool, 1),418+ killChan: make(chan int),419 tui: renderer,420 initFunc: func() { renderer.Init() }}421 t.prompt, t.promptLen = t.processTabs([]rune(opts.Prompt), 0)422 return &t876 unmodified lines1299 if !valid {1300 return1301 }1302 command := replacePlaceholder(template, t.ansi, t.delimiter, forcePlus, string(t.input), list)1301- cmd := util.ExecCommand(command)1303+ cmd := util.ExecCommand(command, false)1304 if !background {1305 cmd.Stdin = os.Stdin1306 cmd.Stdout = os.Stdout1307 cmd.Stderr = os.Stderr74 unmodified lines1382 t.deselectItem(item)1383 }1384 }1385 1386+func (t *Terminal) killPreview(code int) {1387+ select {1388+ case t.killChan <- code:1389+ default:1390+ if code != exitCancel {1391+ os.Exit(code)1392+ }1393+ }1394+}1395+1396+func (t *Terminal) cancelPreview() {1397+ t.killPreview(exitCancel)1398+}1399+1400 // Loop is called to start Terminal I/O1401 func (t *Terminal) Loop() {1402 // prof := profile.Start(profile.ProfilePath("/tmp/"))1403 <-t.startChan69 unmodified lines1473 // We don't display preview window if no match1474 if request[0] != nil {1475 command := replacePlaceholder(t.preview.command,1476 t.ansi, t.delimiter, false, string(t.input), request)1461- cmd := util.ExecCommand(command)1477+ cmd := util.ExecCommand(command, true)1478 if t.pwindow != nil {1479 env := os.Environ()1480 env = append(env, fmt.Sprintf("LINES=%d", t.pwindow.Height()))1481 env = append(env, fmt.Sprintf("COLUMNS=%d", t.pwindow.Width()))1482 cmd.Env = env1483 }1468- out, _ := cmd.CombinedOutput()1469- t.reqBox.Set(reqPreviewDisplay, string(out))1484+ var out bytes.Buffer1485+ cmd.Stdout = &out1486+ cmd.Stderr = &out1487+ cmd.Start()1488+ finishChan := make(chan bool, 1)1489+ updateChan := make(chan bool)1490+ go func() {1491+ select {1492+ case code := <-t.killChan:1493+ if code != exitCancel {1494+ util.KillCommand(cmd)1495+ os.Exit(code)1496+ } else {1497+ select {1498+ case <-time.After(previewCancelWait):1499+ util.KillCommand(cmd)1500+ updateChan <- true1501+ case <-finishChan:1502+ updateChan <- false1503+ }1504+ }1505+ case <-finishChan:1506+ updateChan <- false1507+ }1508+ }()1509+ cmd.Wait()1510+ finishChan <- true1511+ if out.Len() > 0 || !<-updateChan {1512+ t.reqBox.Set(reqPreviewDisplay, string(out.Bytes()))1513+ }1514 } else {1515 t.reqBox.Set(reqPreviewDisplay, "")1516 }1517 }9 unmodified lines1527 if code <= exitNoMatch && t.history != nil {1528 t.history.append(string(t.input))1529 }1530 // prof.Stop()1487- os.Exit(code)1531+ t.killPreview(code)1532 }1533 1534 go func() {1535 var focused *Item18 unmodified lines1554 version = t.version1555 focused = currentFocus1556 if t.isPreviewEnabled() {1557 _, list := t.buildPlusList(t.preview.command, false)1558+ t.cancelPreview()1559 t.previewBox.Set(reqPreviewEnqueue, list)1560 }1561 }1562 case reqJump:101 unmodified lines1664 t.resizeWindows()1665 if t.previewer.enabled {1666 valid, list := t.buildPlusList(t.preview.command, false)1667 if valid {1668+ t.cancelPreview()1669 t.previewBox.Set(reqPreviewEnqueue, list)1670 }1671 }1672 req(reqList, reqInfo, reqHeader)364 unmodified lines