From e569667282a59d181b394f3901a2a7e02b38677d Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 16 Jul 2024 13:07:34 -0400 Subject: [PATCH] fix(spin): pause tea before running the sub-process (#621) We need to pause the main tea.Program process to restore the terminal state before running the sub-process. Fixes: https://github.com/charmbracelet/gum/issues/607 --- spin/spin.go | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/spin/spin.go b/spin/spin.go index ee30326cd..d6ca928d7 100644 --- a/spin/spin.go +++ b/spin/spin.go @@ -46,9 +46,11 @@ type model struct { hasTimeout bool } -var bothbuf strings.Builder -var outbuf strings.Builder -var errbuf strings.Builder +var ( + bothbuf strings.Builder + outbuf strings.Builder + errbuf strings.Builder +) type finishCommandMsg struct { stdout string @@ -58,27 +60,24 @@ type finishCommandMsg struct { } func commandStart(command []string) tea.Cmd { - return func() tea.Msg { - var args []string - if len(command) > 1 { - args = command[1:] - } - cmd := exec.Command(command[0], args...) //nolint:gosec - - if isatty.IsTerminal(os.Stdout.Fd()) { - stdout := io.MultiWriter(&bothbuf, &errbuf) - stderr := io.MultiWriter(&bothbuf, &outbuf) + var args []string + if len(command) > 1 { + args = command[1:] + } - cmd.Stdout = stdout - cmd.Stderr = stderr - } else { - cmd.Stdout = os.Stdout - } + cmd := exec.Command(command[0], args...) //nolint:gosec + if isatty.IsTerminal(os.Stdout.Fd()) { + stdout := io.MultiWriter(&bothbuf, &errbuf) + stderr := io.MultiWriter(&bothbuf, &outbuf) - _ = cmd.Run() + cmd.Stdout = stdout + cmd.Stderr = stderr + } else { + cmd.Stdout = os.Stdout + } + return tea.ExecProcess(cmd, func(error) tea.Msg { status := cmd.ProcessState.ExitCode() - if status == -1 { status = 1 } @@ -89,7 +88,7 @@ func commandStart(command []string) tea.Cmd { output: bothbuf.String(), status: status, } - } + }) } func (m model) Init() tea.Cmd { @@ -99,6 +98,7 @@ func (m model) Init() tea.Cmd { timeout.Init(m.timeout, nil), ) } + func (m model) View() string { if m.quitting && m.showOutput { return strings.TrimPrefix(errbuf.String()+"\n"+outbuf.String(), "\n")