Skip to content
Myr

command.go

This file

Path

spin/command.go

82 lines · 2.3 KiB · Go

Last changed by

chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06

History of this file

Take it

The walk stopped after 200 commits: lines older than 6837ed2 point at it.

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 1–6why, one step back
1package spin
23import (4 "fmt"5 "os"6

chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e

lines 7–8why, one step back
7 "charm.land/bubbles/v2/spinner"
8 tea "charm.land/bubbletea/v2"

chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06

lines 9–11why, one step back
9 "charm.land/gum/v2/internal/exit"
10 "charm.land/gum/v2/internal/timeout"11 "charm.land/gum/v2/style"

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 12why, one step back
12 "github.com/charmbracelet/x/term"

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 13–17why, one step back
13)
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 back
18 isOutTTY := term.IsTerminal(os.Stdout.Fd())
19 isErrTTY := term.IsTerminal(os.Stderr.Fd())

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 20–23why, one step back
20
21 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 back
24 top, right, bottom, left := style.ParsePadding(o.Padding)

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 25why, one step back
25 m := model{

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 26–29why, one step back
26 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 back
30 showStdout: (o.ShowOutput || o.ShowStdout) && isOutTTY,
31 showStderr: (o.ShowOutput || o.ShowStderr) && isErrTTY,

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 32why, one step back
32 showError: o.ShowError,

fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9

line 33why, one step back
33 isTTY: isErrTTY,

feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525

line 34why, one step back
34 padding: []int{top, right, bottom, left},

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 35–36why, one step back
35 }
36

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

lines 37–39why, one step back
37 ctx, cancel := timeout.Context(o.Timeout)
38 defer cancel()39

fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9

lines 40–41why, one step back
40 tm, err := tea.NewProgram(
41 m,

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

lines 42–43why, one step back
42 tea.WithOutput(os.Stderr),
43 tea.WithContext(ctx),

fix: spin when not a tty (#813)Carlos Alexandro Beckerbefore adoption05c4bb9

lines 44–45why, one step back
44 tea.WithInput(nil),
45 ).Run()

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

lines 46–47why, one step back
46 if err != nil {
47 return fmt.Errorf("unable to run action: %w", err)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 48–49why, one step back
48 }
49

feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952

line 50why, one step back
50 m = tm.(model)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 51–53why, one step back
51 // If the command succeeds, and we are printing output and we are in a TTY then push the STDOUT we got to the actual
52 // STDOUT for piping or other things.53 //nolint:nestif

fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857

lines 54–70why, one step back
54 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)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 71–75why, one step back
71 }
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 back
76 if _, err := os.Stdout.WriteString(m.output); err != nil {

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

lines 77–80why, one step back
77 return fmt.Errorf("failed to write to stdout: %w", err)
78 }79 }80

refactor: removing huh as a dep (#742)Carlos Alexandro Beckerbefore adoptione30fc5e

line 81why, one step back
81 return exit.ErrExit(m.status)

feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2

line 82why, one step back
82}