diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2018-09-27 15:27:08 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2018-09-28 10:33:52 +0900 |
commit | 0d748a0699760003444efca219eb4bb245996008 (patch) | |
tree | 791e4849d9bdfb06d013dac96c2197fa2739c0e5 /src/util | |
parent | 27c40dc6b0cc0402b1602b76202be80841329a1d (diff) |
Kill running preview process after 500ms when focus has changed
Close #1383
Close #1384
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/util_unix.go | 19 | ||||
-rw-r--r-- | src/util/util_windows.go | 13 |
2 files changed, 24 insertions, 8 deletions
diff --git a/src/util/util_unix.go b/src/util/util_unix.go index fc63c027..6331275c 100644 --- a/src/util/util_unix.go +++ b/src/util/util_unix.go @@ -9,17 +9,26 @@ import ( ) // ExecCommand executes the given command with $SHELL -func ExecCommand(command string) *exec.Cmd { +func ExecCommand(command string, setpgid bool) *exec.Cmd { shell := os.Getenv("SHELL") if len(shell) == 0 { shell = "sh" } - return ExecCommandWith(shell, command) + return ExecCommandWith(shell, command, setpgid) } // ExecCommandWith executes the given command with the specified shell -func ExecCommandWith(shell string, command string) *exec.Cmd { - return exec.Command(shell, "-c", command) +func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd { + cmd := exec.Command(shell, "-c", command) + if setpgid { + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + } + return cmd +} + +// KillCommand kills the process for the given command +func KillCommand(cmd *exec.Cmd) error { + return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) } // IsWindows returns true on Windows @@ -27,7 +36,7 @@ func IsWindows() bool { return false } -// SetNonBlock executes syscall.SetNonblock on file descriptor +// SetNonblock executes syscall.SetNonblock on file descriptor func SetNonblock(file *os.File, nonblock bool) { syscall.SetNonblock(int(file.Fd()), nonblock) } diff --git a/src/util/util_windows.go b/src/util/util_windows.go index 41a9a5c7..51715cd8 100644 --- a/src/util/util_windows.go +++ b/src/util/util_windows.go @@ -10,13 +10,15 @@ import ( ) // ExecCommand executes the given command with cmd -func ExecCommand(command string) *exec.Cmd { +func ExecCommand(command string, setpgid bool) *exec.Cmd { return ExecCommandWith("cmd", command) } // ExecCommandWith executes the given command with cmd. _shell parameter is // ignored on Windows. -func ExecCommandWith(_shell string, command string) *exec.Cmd { +// FIXME: setpgid is unused. We set it in the Unix implementation so that we +// can kill preview process with its child processes at once. +func ExecCommandWith(_shell string, command string, setpgid bool) *exec.Cmd { cmd := exec.Command("cmd") cmd.SysProcAttr = &syscall.SysProcAttr{ HideWindow: false, @@ -26,12 +28,17 @@ func ExecCommandWith(_shell string, command string) *exec.Cmd { return cmd } +// KillCommand kills the process for the given command +func KillCommand(cmd *exec.Cmd) error { + return cmd.Process.Kill() +} + // IsWindows returns true on Windows func IsWindows() bool { return true } -// SetNonBlock executes syscall.SetNonblock on file descriptor +// SetNonblock executes syscall.SetNonblock on file descriptor func SetNonblock(file *os.File, nonblock bool) { syscall.SetNonblock(syscall.Handle(file.Fd()), nonblock) } |