Skip to content

Commit

Permalink
fix(spin): pause tea before running the sub-process (#621)
Browse files Browse the repository at this point in the history
We need to pause the main tea.Program process to restore the terminal
state before running the sub-process.

Fixes: #607
  • Loading branch information
aymanbagabas authored Jul 16, 2024
1 parent f8f8645 commit e569667
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions spin/spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -89,7 +88,7 @@ func commandStart(command []string) tea.Cmd {
output: bothbuf.String(),
status: status,
}
}
})
}

func (m model) Init() tea.Cmd {
Expand All @@ -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")
Expand Down

0 comments on commit e569667

Please sign in to comment.