Skip to content
Myr

spin.go

This file

Path

spin/spin.go

200 lines · 4.4 KiB · Go

Last changed by

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

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–17why, one step back
1// Package spin provides a shell script interface for the spinner bubble.
2// https://github.com/charmbracelet/bubbles/tree/master/spinner3//4// It is useful for displaying that some task is running in the background5// while consuming it's output so that it is not shown to the user.6//7// For example, let's do a long running task: $ sleep 58//9// We can simply prepend a spinner to this task to show it to the user, while10// performing the task / command in the background.11//12// $ gum spin -t "Taking a nap..." -- sleep 513//14// The spinner will automatically exit when the task is complete.15package spin1617import (

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

lines 18–19why, one step back
18 "bytes"
19 "context"

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

line 20why, one step back
20 "io"

fix(spin): properly redirect outputCarlos Alexandro Beckerbefore adoption71af32c

line 21why, one step back
21 "os"

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

line 22why, one step back
22 "os/exec"

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

line 23why, one step back
23 "runtime"

fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f

line 24why, one step back
24 "syscall"

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

line 25why, one step back
25

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

lines 26–28why, one step back
26 "charm.land/bubbles/v2/spinner"
27 tea "charm.land/bubbletea/v2"28 "charm.land/lipgloss/v2"

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

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

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

line 30why, one step back
30 "github.com/charmbracelet/x/xpty"

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

lines 31–35why, one step back
31)
3233type model struct {34 spinner spinner.Model35 title string

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

line 36why, one step back
36 padding []int

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

lines 37–39why, one step back
37 align string
38 command []string39 quitting bool

fix(spin): if not a tty, only print title, do not open tty for stdin (#763)Carlos Alexandro Beckerbefore adoptioncf2da64

line 40why, one step back
40 isTTY bool

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

lines 41–44why, one step back
41 status int
42 stdout string43 stderr string44 output string

feat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7

lines 45–46why, one step back
45 showStdout bool
46 showStderr bool

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

line 47why, one step back
47 showError bool

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

line 48why, one step back
48 err error

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

lines 49–51why, one step back
49}
5051var (

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

lines 52–54why, one step back
52 bothbuf bytes.Buffer
53 outbuf bytes.Buffer54 errbuf bytes.Buffer

fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f

lines 55–56why, one step back
55
56 executing *exec.Cmd

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

lines 57–58why, one step back
57)
58

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

lines 59–60why, one step back
59type errorMsg error
60

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

lines 61–74why, one step back
61type finishCommandMsg struct {
62 stdout string63 stderr string64 output string65 status int66}6768func commandStart(command []string) tea.Cmd {69 return func() tea.Msg {70 var args []string71 if len(command) > 1 {72 args = command[1:]73 }74

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

line 75why, one step back
75 executing = exec.CommandContext(context.Background(), command[0], args...) //nolint:gosec

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

lines 76–84why, one step back
76 executing.Stdin = os.Stdin
7778 isTerminal := term.IsTerminal(os.Stdout.Fd())7980 // NOTE(@andreynering): We had issues with Git Bash on Windows81 // when it comes to handling PTYs, so we're falling back to82 // to redirecting stdout/stderr as usual to avoid issues.83 //nolint:nestif84 if isTerminal && runtime.GOOS == "windows" {

fix(spin): properly redirect outputCarlos Alexandro Beckerbefore adoption71af32c

lines 85–86why, one step back
85 executing.Stdout = io.MultiWriter(&bothbuf, &outbuf)
86 executing.Stderr = io.MultiWriter(&bothbuf, &errbuf)

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

lines 87–114why, one step back
87 _ = executing.Run()
88 } else if isTerminal {89 stdoutPty, err := openPty(os.Stdout)90 if err != nil {91 return errorMsg(err)92 }93 defer stdoutPty.Close() //nolint:errcheck9495 stderrPty, err := openPty(os.Stderr)96 if err != nil {97 return errorMsg(err)98 }99 defer stderrPty.Close() //nolint:errcheck100101 if outUnixPty, isOutUnixPty := stdoutPty.(*xpty.UnixPty); isOutUnixPty {102 executing.Stdout = outUnixPty.Slave()103 }104 if errUnixPty, isErrUnixPty := stderrPty.(*xpty.UnixPty); isErrUnixPty {105 executing.Stderr = errUnixPty.Slave()106 }107108 go io.Copy(io.MultiWriter(&bothbuf, &outbuf), stdoutPty) //nolint:errcheck109 go io.Copy(io.MultiWriter(&bothbuf, &errbuf), stderrPty) //nolint:errcheck110111 if err = stdoutPty.Start(executing); err != nil {112 return errorMsg(err)113 }114 _ = xpty.WaitProcess(context.Background(), executing)

fix(spin): properly redirect outputCarlos Alexandro Beckerbefore adoption71af32c

lines 115–116why, one step back
115 } else {
116 executing.Stdout = os.Stdout

fix(spinner): set stdin and stderrCarlos Alexandro Beckerbefore adoptionafb6111

line 117why, one step back
117 executing.Stderr = os.Stderr

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

line 118why, one step back
118 _ = executing.Run()

fix(spin): properly redirect outputCarlos Alexandro Beckerbefore adoption71af32c

line 119why, one step back
119 }

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

line 120why, one step back
120

fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f

line 121why, one step back
121 status := executing.ProcessState.ExitCode()

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

lines 122–134why, one step back
122 if status == -1 {
123 status = 1124 }125126 return finishCommandMsg{127 stdout: outbuf.String(),128 stderr: errbuf.String(),129 output: bothbuf.String(),130 status: status,131 }132 }133}134

fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f

lines 135–138why, one step back
135func commandAbort() tea.Msg {
136 if executing != nil && executing.Process != nil {137 _ = executing.Process.Signal(syscall.SIGINT)138 }

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

line 139why, one step back
139 return tea.InterruptMsg{}

fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f

lines 140–141why, one step back
140}
141

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

lines 142–148why, one step back
142func (m model) Init() tea.Cmd {
143 return tea.Batch(144 m.spinner.Tick,145 commandStart(m.command),146 )147}148

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

line 149why, one step back
149func (m model) View() tea.View {

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

line 150why, one step back
150 if m.quitting {

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

line 151why, one step back
151 return tea.NewView("")

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

lines 152–153why, one step back
152 }
153

feat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7

lines 154–156why, one step back
154 var out string
155 if m.showStderr {156 out += errbuf.String()

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

line 157why, one step back
157 }

feat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7

lines 158–160why, one step back
158 if m.showStdout {
159 out += outbuf.String()160 }

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

line 161why, one step back
161

fix(spin): clear title after finished (#815)Carlos Alexandro Beckerbefore adoption2da9527

line 162why, one step back
162 if !m.isTTY {

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

line 163why, one step back
163 return tea.NewView(m.title)

feat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7

lines 164–165why, one step back
164 }
165

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

lines 166–167why, one step back
166 var header string
167 if m.align == "left" {

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

line 168why, one step back
168 header = m.spinner.View() + " " + m.title

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

line 169why, one step back
169 } else {

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

line 170why, one step back
170 header = m.title + " " + m.spinner.View()

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

line 171why, one step back
171 }

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

line 172why, one step back
172 return tea.NewView(lipgloss.NewStyle().

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

line 173why, one step back
173 Padding(m.padding...).

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

line 174why, one step back
174 Render(header, "", out))

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

lines 175–185why, one step back
175}
176177func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {178 switch msg := msg.(type) {179 case finishCommandMsg:180 m.stdout = msg.stdout181 m.stderr = msg.stderr182 m.output = msg.output183 m.status = msg.status184 m.quitting = true185 return m, tea.Quit

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

line 186why, one step back
186 case tea.KeyPressMsg:

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

lines 187–188why, one step back
187 switch msg.String() {
188 case "ctrl+c":

fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f

line 189why, one step back
189 return m, commandAbort

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

line 190why, one step back
190 }

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

lines 191–194why, one step back
191 case errorMsg:
192 m.err = msg193 m.quitting = true194 return m, tea.Quit

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

lines 195–196why, one step back
195 }
196

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

line 197why, one step back
197 var cmd tea.Cmd

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

lines 198–200why, one step back
198 m.spinner, cmd = m.spinner.Update(msg)
199 return m, cmd200}