Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spin): interrupt child process on ctrl+c #732

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions spin/spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"os/exec"
"strings"
"syscall"
"time"

"github.com/charmbracelet/gum/internal/exit"
Expand Down Expand Up @@ -48,6 +49,8 @@ var (
bothbuf strings.Builder
outbuf strings.Builder
errbuf strings.Builder

executing *exec.Cmd
)

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

cmd := exec.Command(command[0], args...) //nolint:gosec
cmd.Stdout = io.MultiWriter(&bothbuf, &outbuf)
cmd.Stderr = io.MultiWriter(&bothbuf, &errbuf)
_ = cmd.Run()
status := cmd.ProcessState.ExitCode()
executing = exec.Command(command[0], args...) //nolint:gosec
executing.Stdout = io.MultiWriter(&bothbuf, &outbuf)
executing.Stderr = io.MultiWriter(&bothbuf, &errbuf)
_ = executing.Run()
status := executing.ProcessState.ExitCode()
if status == -1 {
status = 1
}
Expand All @@ -82,6 +85,13 @@ func commandStart(command []string) tea.Cmd {
}
}

func commandAbort() tea.Msg {
if executing != nil && executing.Process != nil {
_ = executing.Process.Signal(syscall.SIGINT)
}
return nil
}

func (m model) Init() tea.Cmd {
return tea.Batch(
m.spinner.Tick,
Expand Down Expand Up @@ -135,7 +145,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "ctrl+c":
m.aborted = true
return m, tea.Quit
return m, commandAbort
}
}

Expand Down
Loading