Skip to content

Commit

Permalink
feat(spin): --show-stdout --show-stderr (#774)
Browse files Browse the repository at this point in the history
closes #362
  • Loading branch information
caarlos0 authored Dec 13, 2024
1 parent d1bfd56 commit 32786f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion spin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (o Options) Run() error {
title: o.TitleStyle.ToLipgloss().Render(o.Title),
command: o.Command,
align: o.Align,
showOutput: o.ShowOutput && isTTY,
showStdout: (o.ShowOutput || o.ShowStdout) && isTTY,
showStderr: (o.ShowOutput || o.ShowStderr) && isTTY,
showError: o.ShowError,
isTTY: isTTY,
}
Expand Down
4 changes: 3 additions & 1 deletion spin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
type Options struct {
Command []string `arg:"" help:"Command to run"`

ShowOutput bool `help:"Show or pipe output of command during execution" default:"false" env:"GUM_SPIN_SHOW_OUTPUT"`
ShowOutput bool `help:"Show or pipe output of command during execution (shows both STDOUT and STDERR)" default:"false" env:"GUM_SPIN_SHOW_OUTPUT"`
ShowError bool `help:"Show output of command only if the command fails" default:"false" env:"GUM_SPIN_SHOW_ERROR"`
ShowStdout bool `help:"Show STDOUT output" default:"false" env:"GUM_SPIN_SHOW_STDOUT"`
ShowStderr bool `help:"Show STDERR errput" default:"false" env:"GUM_SPIN_SHOW_STDERR"`
Spinner string `help:"Spinner type" short:"s" type:"spinner" enum:"line,dot,minidot,jump,pulse,points,globe,moon,monkey,meter,hamburger" default:"dot" env:"GUM_SPIN_SPINNER"`
SpinnerStyle style.Styles `embed:"" prefix:"spinner." set:"defaultForeground=212" envprefix:"GUM_SPIN_SPINNER_"`
Title string `help:"Text to display to user while spinning" default:"Loading..." env:"GUM_SPIN_TITLE"`
Expand Down
20 changes: 13 additions & 7 deletions spin/spin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type model struct {
stdout string
stderr string
output string
showOutput bool
showStdout bool
showStderr bool
showError bool
}

Expand Down Expand Up @@ -106,8 +107,16 @@ func (m model) View() string {
return m.title
}

if m.quitting && m.showOutput {
return strings.TrimPrefix(errbuf.String()+"\n"+outbuf.String(), "\n")
var out string
if m.showStderr {
out += errbuf.String()
}
if m.showStdout {
out += outbuf.String()
}

if m.quitting && out != "" {
return out
}

var header string
Expand All @@ -116,10 +125,7 @@ func (m model) View() string {
} else {
header = m.title + " " + m.spinner.View()
}
if !m.showOutput {
return header
}
return header + errbuf.String() + "\n" + outbuf.String()
return header + "\n" + out
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down

0 comments on commit 32786f7

Please sign in to comment.