Skip to content
Myr

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

5 files+82 added−18 removed

modified:src/util/util_unix.go+14 added-5 removed
7 unmodified lines
8 "syscall"9)1011// ExecCommand executes the given command with $SHELL12func ExecCommand(command string) *exec.Cmd {12func 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}1920// ExecCommandWith executes the given command with the specified shell21func ExecCommandWith(shell string, command string) *exec.Cmd {22 return exec.Command(shell, "-c", command)21func 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}2829// KillCommand kills the process for the given command30func KillCommand(cmd *exec.Cmd) error {31 return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)32}3334// IsWindows returns true on Windows35func IsWindows() bool {36 return false37}3830// SetNonBlock executes syscall.SetNonblock on file descriptor39// SetNonblock executes syscall.SetNonblock on file descriptor40func SetNonblock(file *os.File, nonblock bool) {41 syscall.SetNonblock(int(file.Fd()), nonblock)42}43
4 unmodified lines
modified:src/util/util_windows.go+10 added-3 removed
8 unmodified lines
9 "syscall"10)1112// ExecCommand executes the given command with cmd13func ExecCommand(command string) *exec.Cmd {13func ExecCommand(command string, setpgid bool) *exec.Cmd {14 return ExecCommandWith("cmd", command)15}1617// 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.19func ExecCommandWith(_shell string, command string) *exec.Cmd {21func 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 line
27 }28 return cmd29}3031// KillCommand kills the process for the given command32func KillCommand(cmd *exec.Cmd) error {33 return cmd.Process.Kill()34}3536// IsWindows returns true on Windows37func IsWindows() bool {38 return true39}4034// SetNonBlock executes syscall.SetNonblock on file descriptor41// SetNonblock executes syscall.SetNonblock on file descriptor42func SetNonblock(file *os.File, nonblock bool) {43 syscall.SetNonblock(syscall.Handle(file.Fd()), nonblock)44}45
4 unmodified lines
modified:src/constants.go+6 added-4 removed
20 unmodified lines
21 readerPollIntervalStep = 5 * time.Millisecond22 readerPollIntervalMax = 50 * time.Millisecond2324 // 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 = 3003031 // Matcher32 numPartitionsMultiplier = 833 maxPartitions = 32
42 unmodified lines
76 EvtReady77)7879const (80 exitCancel = -181 exitOk = 082 exitNoMatch = 183 exitError = 284 exitInterrupt = 130
1 unmodified line
modified:src/reader.go+1 added-1 removed
101 unmodified lines
102 return true103}104105func (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 lines
modified:src/terminal.go+51 added-5 removed
112 unmodified lines
113 initFunc func()114 prevLines []itemLine115 suppress bool116 startChan chan bool117 killChan chan int118 slab *util.Slab119 theme *tui.ColorTheme120 tui tui.Renderer121}
292 unmodified lines
414 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 &t
876 unmodified lines
1299 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.Stderr
74 unmodified lines
1382 t.deselectItem(item)1383 }1384}13851386func (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}13951396func (t *Terminal) cancelPreview() {1397 t.killPreview(exitCancel)1398}13991400// Loop is called to start Terminal I/O1401func (t *Terminal) Loop() {1402 // prof := profile.Start(profile.ProfilePath("/tmp/"))1403 <-t.startChan
69 unmodified lines
1473 // 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 lines
1527 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 }15331534 go func() {1535 var focused *Item
18 unmodified lines
1554 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 lines
1664 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