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
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 (18 "bytes"19 "context"20 "io"21 "os"22 "os/exec"23 "runtime"24 "syscall"2526 "charm.land/bubbles/v2/spinner"27 tea "charm.land/bubbletea/v2"28 "charm.land/lipgloss/v2"29 "github.com/charmbracelet/x/term"30 "github.com/charmbracelet/x/xpty"31)3233type model struct {34 spinner spinner.Model35 title string36 padding []int37 align string38 command []string39 quitting bool40 isTTY bool41 status int42 stdout string43 stderr string44 output string45 showStdout bool46 showStderr bool47 showError bool48 err error49}5051var (52 bothbuf bytes.Buffer53 outbuf bytes.Buffer54 errbuf bytes.Buffer5556 executing *exec.Cmd57)5859type errorMsg error6061type 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 }7475 executing = exec.CommandContext(context.Background(), command[0], args...) //nolint:gosec76 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" {85 executing.Stdout = io.MultiWriter(&bothbuf, &outbuf)86 executing.Stderr = io.MultiWriter(&bothbuf, &errbuf)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)115 } else {116 executing.Stdout = os.Stdout117 executing.Stderr = os.Stderr118 _ = executing.Run()119 }120121 status := executing.ProcessState.ExitCode()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}134135func commandAbort() tea.Msg {136 if executing != nil && executing.Process != nil {137 _ = executing.Process.Signal(syscall.SIGINT)138 }139 return tea.InterruptMsg{}140}141142func (m model) Init() tea.Cmd {143 return tea.Batch(144 m.spinner.Tick,145 commandStart(m.command),146 )147}148149func (m model) View() tea.View {150 if m.quitting {151 return tea.NewView("")152 }153154 var out string155 if m.showStderr {156 out += errbuf.String()157 }158 if m.showStdout {159 out += outbuf.String()160 }161162 if !m.isTTY {163 return tea.NewView(m.title)164 }165166 var header string167 if m.align == "left" {168 header = m.spinner.View() + " " + m.title169 } else {170 header = m.title + " " + m.spinner.View()171 }172 return tea.NewView(lipgloss.NewStyle().173 Padding(m.padding...).174 Render(header, "", out))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.Quit186 case tea.KeyPressMsg:187 switch msg.String() {188 case "ctrl+c":189 return m, commandAbort190 }191 case errorMsg:192 m.err = msg193 m.quitting = true194 return m, tea.Quit195 }196197 var cmd tea.Cmd198 m.spinner, cmd = m.spinner.Update(msg)199 return m, cmd200}