command.go
This file
Path
spin/command.go
82 lines · 2.4 KiB · Go
Last changed by
feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
History of this fileTake it
The walk stopped after 200 commits: lines older than 19a93b0 point at it.
refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 1–9why, one step back1package spin23import (4 "fmt"5 "os"67 "github.com/charmbracelet/bubbles/spinner"8 tea "github.com/charmbracelet/bubbletea"9 "github.com/charmbracelet/gum/internal/exit"feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 10why, one step back10 "github.com/charmbracelet/gum/internal/timeout"feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 11why, one step back11 "github.com/charmbracelet/gum/style"feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 12why, one step back12 "github.com/charmbracelet/x/term"refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 13–17why, one step back13)1415// Run provides a shell script interface for the spinner bubble.16// https://github.com/charmbracelet/bubbles/spinner17func (o Options) Run() error {fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9
lines 18–19why, one step back18 isOutTTY := term.IsTerminal(os.Stdout.Fd())19 isErrTTY := term.IsTerminal(os.Stderr.Fd())refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 20–23why, one step back2021 s := spinner.New()22 s.Style = o.SpinnerStyle.ToLipgloss()23 s.Spinner = spinnerMap[o.Spinner]feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 24why, one step back24 top, right, bottom, left := style.ParsePadding(o.Padding)feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 25why, one step back25 m := model{refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 26–29why, one step back26 spinner: s,27 title: o.TitleStyle.ToLipgloss().Render(o.Title),28 command: o.Command,29 align: o.Align,fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9
lines 30–31why, one step back30 showStdout: (o.ShowOutput || o.ShowStdout) && isOutTTY,31 showStderr: (o.ShowOutput || o.ShowStderr) && isErrTTY,refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 32why, one step back32 showError: o.ShowError,fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9
line 33why, one step back33 isTTY: isErrTTY,feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 34why, one step back34 padding: []int{top, right, bottom, left},refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 35–36why, one step back35 }36feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 37–39why, one step back37 ctx, cancel := timeout.Context(o.Timeout)38 defer cancel()39fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9
lines 40–41why, one step back40 tm, err := tea.NewProgram(41 m,feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 42–43why, one step back42 tea.WithOutput(os.Stderr),43 tea.WithContext(ctx),fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9
lines 44–45why, one step back44 tea.WithInput(nil),45 ).Run()feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
lines 46–47why, one step back46 if err != nil {47 return fmt.Errorf("unable to run action: %w", err)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 48–49why, one step back48 }49feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 50why, one step back50 m = tm.(model)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 51–53why, one step back51 // If the command succeeds, and we are printing output and we are in a TTY then push the STDOUT we got to the actual52 // STDOUT for piping or other things.53 //nolint:nestiffix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
lines 54–70why, one step back54 if m.err != nil {55 if _, err := fmt.Fprintf(os.Stderr, "%s\n", m.err.Error()); err != nil {56 return fmt.Errorf("failed to write to stdout: %w", err)57 }58 return exit.ErrExit(1)59 } else if m.status == 0 {60 var output string61 if o.ShowOutput || (o.ShowStdout && o.ShowStderr) {62 output = m.output63 } else if o.ShowStdout {64 output = m.stdout65 } else if o.ShowStderr {66 output = m.stderr67 }68 if output != "" {69 if _, err := os.Stdout.WriteString(output); err != nil {70 return fmt.Errorf("failed to write to stdout: %w", err)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 71–75why, one step back71 }72 }73 } else if o.ShowError {74 // Otherwise if we are showing errors and the command did not exit with a 0 status code then push all of the command75 // output to the terminal. This way failed commands can be debugged.fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 76why, one step back76 if _, err := os.Stdout.WriteString(m.output); err != nil {refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
lines 77–80why, one step back77 return fmt.Errorf("failed to write to stdout: %w", err)78 }79 }80refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e
line 81why, one step back81 return exit.ErrExit(m.status)refactor(input): simplify echoModeCarlos Alexandro Beckerbefore adoption19a93b0
line 82why, one step back82}