gum/spin/spin.go
Aaron Korte 01c7d96945
feat: pass stderr (#64)
* feat: pass stderr

Pass stderr from the command run by gum spin analog to how stdout is passed.

#63

* Apply suggestions from code review

* Update spin/spin.go

* fix(spin): Add stderr to output

When `--show-output` is passed we should display all the output of the
command and allow scripters to control `stderr` and `stdout` how they
want.

For example, if a user wants only `stdout` they can redirect stderr to
`/dev/null`:

```
gum spin --show-output -- <action> 2> /dev/null
```

Co-authored-by: Maas Lalani <maas@lalani.dev>
2022-08-01 13:24:01 -04:00

98 lines
2 KiB
Go

// Package spin provides a shell script interface for the spinner bubble.
// https://github.com/charmbracelet/bubbles/tree/master/spinner
//
// It is useful for displaying that some task is running in the background
// while consuming it's output so that it is not shown to the user.
//
// For example, let's do a long running task: $ sleep 5
//
// We can simply prepend a spinner to this task to show it to the user, while
// performing the task / command in the background.
//
// $ gum spin -t "Taking a nap..." -- sleep 5
//
// The spinner will automatically exit when the task is complete.
//
package spin
import (
"os/exec"
"strings"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
spinner spinner.Model
title string
command []string
aborted bool
status int
stdout string
stderr string
}
type finishCommandMsg struct {
stdout string
stderr string
status int
}
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
var outbuf, errbuf strings.Builder
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
_ = cmd.Run()
status := cmd.ProcessState.ExitCode()
if status == -1 {
status = 1
}
return finishCommandMsg{
stdout: outbuf.String(),
stderr: errbuf.String(),
status: status,
}
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(
m.spinner.Tick,
commandStart(m.command),
)
}
func (m model) View() string { return m.spinner.View() + " " + m.title }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case finishCommandMsg:
m.stdout = msg.stdout
m.stderr = msg.stderr
m.status = msg.status
return m, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
m.aborted = true
return m, tea.Quit
}
}
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}