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 fileTake 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 back1// 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 back18 "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 back20 "io"fix(spin): properly redirect outputCarlos Alexandro Beckerbefore adoption71af32c
line 21why, one step back21 "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 back22 "os/exec"fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 23why, one step back23 "runtime"fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f
line 24why, one step back24 "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 back25chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
lines 26–28why, one step back26 "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 back29 "github.com/charmbracelet/x/term"fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 30why, one step back30 "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 back31)3233type model struct {34 spinner spinner.Model35 title stringfeat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 36why, one step back36 padding []intfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 37–39why, one step back37 align string38 command []string39 quitting boolfix(spin): if not a tty, only print title, do not open tty for stdin (#763)Carlos Alexandro Beckerbefore adoptioncf2da64
line 40why, one step back40 isTTY boolfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 41–44why, one step back41 status int42 stdout string43 stderr string44 output stringfeat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7
lines 45–46why, one step back45 showStdout bool46 showStderr boolfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 47why, one step back47 showError boolfix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 48why, one step back48 err errorfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 49–51why, one step back49}5051var (fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
lines 52–54why, one step back52 bothbuf bytes.Buffer53 outbuf bytes.Buffer54 errbuf bytes.Bufferfix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f
lines 55–56why, one step back5556 executing *exec.Cmdfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 57–58why, one step back57)58fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
lines 59–60why, one step back59type errorMsg error60feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 61–74why, one step back61type 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 }74feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 75why, one step back75 executing = exec.CommandContext(context.Background(), command[0], args...) //nolint:gosecfix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
lines 76–84why, one step back76 executing.Stdin = os.Stdin7778 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 back85 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 back87 _ = 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 back115 } else {116 executing.Stdout = os.Stdoutfix(spinner): set stdin and stderrCarlos Alexandro Beckerbefore adoptionafb6111
line 117why, one step back117 executing.Stderr = os.Stderrfix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 118why, one step back118 _ = executing.Run()fix(spin): properly redirect outputCarlos Alexandro Beckerbefore adoption71af32c
line 119why, one step back119 }fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 120why, one step back120fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f
line 121why, one step back121 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 back122 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}134fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f
lines 135–138why, one step back135func 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 back139 return tea.InterruptMsg{}fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f
lines 140–141why, one step back140}141feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 142–148why, one step back142func (m model) Init() tea.Cmd {143 return tea.Batch(144 m.spinner.Tick,145 commandStart(m.command),146 )147}148chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 149why, one step back149func (m model) View() tea.View {fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 150why, one step back150 if m.quitting {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 151why, one step back151 return tea.NewView("")fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
lines 152–153why, one step back152 }153feat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7
lines 154–156why, one step back154 var out string155 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 back157 }feat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7
lines 158–160why, one step back158 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 back161fix(spin): clear title after finished (#815)Carlos Alexandro Beckerbefore adoption2da9527
line 162why, one step back162 if !m.isTTY {chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 163why, one step back163 return tea.NewView(m.title)feat(spin): --show-stdout --show-stderr (#774)Carlos Alexandro Beckerbefore adoption32786f7
lines 164–165why, one step back164 }165feat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 166–167why, one step back166 var header string167 if m.align == "left" {feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 168why, one step back168 header = m.spinner.View() + " " + m.titlefeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 169why, one step back169 } else {feat: handle interrupts and timeouts (#747)Carlos Alexandro Beckerbefore adoption4f46952
line 170why, one step back170 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 back171 }chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 172why, one step back172 return tea.NewView(lipgloss.NewStyle().feat: adding --padding to most commands (#960)Carlos Alexandro Beckerbefore adoption6045525
line 173why, one step back173 Padding(m.padding...).chore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 174why, one step back174 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 back175}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.Quitchore: update to bubbletea v2 (#1112)Drew Smirnoffbefore adoptionf6dd28e
line 186why, one step back186 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 back187 switch msg.String() {188 case "ctrl+c":fix(spin): interrupt child process on ctrl+c (#732)Carlos Alexandro Beckerbefore adoptionc3e836f
line 189why, one step back189 return m, commandAbortfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
line 190why, one step back190 }fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
lines 191–194why, one step back191 case errorMsg:192 m.err = msg193 m.quitting = true194 return m, tea.Quitfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 195–196why, one step back195 }196fix(spin): preserve color output when `--show-output` is given (#850)Andrey Neringbefore adoption93f6857
line 197why, one step back197 var cmd tea.Cmdfeat(deps): bump github.com/charmbracelet/bubbletea from 0.27.0 to 1.0.0 (#661)dependabot[bot]before adoption6837ed2
lines 198–200why, one step back198 m.spinner, cmd = m.spinner.Update(msg)199 return m, cmd200}