Skip to content

Commit c3e836f

Browse files
authored
fix(spin): interrupt child process on ctrl+c (#732)
* fix(spin): interrupt child process on ctrl+c This will send a SIGINT to the child process when ctrl+c is pressed. closes #730 * fix: lint
1 parent 01f36b5 commit c3e836f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

spin/spin.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"io"
1919
"os/exec"
2020
"strings"
21+
"syscall"
2122
"time"
2223

2324
"github.com/charmbracelet/gum/internal/exit"
@@ -48,6 +49,8 @@ var (
4849
bothbuf strings.Builder
4950
outbuf strings.Builder
5051
errbuf strings.Builder
52+
53+
executing *exec.Cmd
5154
)
5255

5356
type finishCommandMsg struct {
@@ -64,11 +67,11 @@ func commandStart(command []string) tea.Cmd {
6467
args = command[1:]
6568
}
6669

67-
cmd := exec.Command(command[0], args...) //nolint:gosec
68-
cmd.Stdout = io.MultiWriter(&bothbuf, &outbuf)
69-
cmd.Stderr = io.MultiWriter(&bothbuf, &errbuf)
70-
_ = cmd.Run()
71-
status := cmd.ProcessState.ExitCode()
70+
executing = exec.Command(command[0], args...) //nolint:gosec
71+
executing.Stdout = io.MultiWriter(&bothbuf, &outbuf)
72+
executing.Stderr = io.MultiWriter(&bothbuf, &errbuf)
73+
_ = executing.Run()
74+
status := executing.ProcessState.ExitCode()
7275
if status == -1 {
7376
status = 1
7477
}
@@ -82,6 +85,13 @@ func commandStart(command []string) tea.Cmd {
8285
}
8386
}
8487

88+
func commandAbort() tea.Msg {
89+
if executing != nil && executing.Process != nil {
90+
_ = executing.Process.Signal(syscall.SIGINT)
91+
}
92+
return nil
93+
}
94+
8595
func (m model) Init() tea.Cmd {
8696
return tea.Batch(
8797
m.spinner.Tick,
@@ -135,7 +145,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
135145
switch msg.String() {
136146
case "ctrl+c":
137147
m.aborted = true
138-
return m, tea.Quit
148+
return m, commandAbort
139149
}
140150
}
141151

0 commit comments

Comments
 (0)