Debian Codegraph @

spinner/spinner.go

raw symbols SCIP
1 package spinner
2
3 import (
4 "cmp"
5 "context"
6 "io"
7 "os"
8 "strings"
9
10 "github.com/charmbracelet/bubbles/spinner"
11 tea "github.com/charmbracelet/bubbletea"
12 "github.com/charmbracelet/lipgloss"
13 "github.com/muesli/termenv"
14 )
15
16 // Spinner represents a loading spinner.
17 // To get started simply create a new spinner and call `Run`.
18 //
19 // s := spinner.New()
20 // s.Run()
21 //
22 // ⣾ Loading...
23 type Spinner struct {
24 spinner spinner.Model
25 action func(ctx context.Context) error
26 ctx context.Context
27 accessible bool
28 title string
29 titleStyle lipgloss.Style
30 output io.Writer
31 err error
32 }
33
34 type Type spinner.Spinner
35
36 var (
37 Line = Type(spinner.Line)
38 Dots = Type(spinner.Dot)
39 MiniDot = Type(spinner.MiniDot)
40 Jump = Type(spinner.Jump)
41 Points = Type(spinner.Points)
42 Pulse = Type(spinner.Pulse)
43 Globe = Type(spinner.Globe)
44 Moon = Type(spinner.Moon)
45 Monkey = Type(spinner.Monkey)
46 Meter = Type(spinner.Meter)
47 Hamburger = Type(spinner.Hamburger)
48 Ellipsis = Type(spinner.Ellipsis)
49 )
50
51 // Type sets the type of the spinner.
52 func (s *Spinner) Type(t Type) *Spinner {
53 s.spinner.Spinner = spinner.Spinner(t)
54 return s
55 }
56
57 // Title sets the title of the spinner.
58 func (s *Spinner) Title(title string) *Spinner {
59 s.title = title
60 return s
61 }
62
63 // Output set the output for the spinner.
64 // Default is STDOUT when [Spinner.Accessible], STDERR otherwise.
65 func (s *Spinner) Output(w io.Writer) *Spinner {
66 s.output = w
67 return s
68 }
69
70 // Action sets the action of the spinner.
71 func (s *Spinner) Action(action func()) *Spinner {
72 s.action = func(context.Context) error {
73 action()
74 return nil
75 }
76 return s
77 }
78
79 // ActionWithErr sets the action of the spinner.
80 //
81 // This is just like [Spinner.Action], but allows the action to use a `context.Context`
82 // and to return an error.
83 func (s *Spinner) ActionWithErr(action func(context.Context) error) *Spinner {
84 s.action = action
85 return s
86 }
87
88 // Context sets the context of the spinner.
89 func (s *Spinner) Context(ctx context.Context) *Spinner {
90 s.ctx = ctx
91 return s
92 }
93
94 // Style sets the style of the spinner.
95 func (s *Spinner) Style(style lipgloss.Style) *Spinner {
96 s.spinner.Style = style
97 return s
98 }
99
100 // TitleStyle sets the title style of the spinner.
101 func (s *Spinner) TitleStyle(style lipgloss.Style) *Spinner {
102 s.titleStyle = style
103 return s
104 }
105
106 // Accessible sets the spinner to be static.
107 func (s *Spinner) Accessible(accessible bool) *Spinner {
108 s.accessible = accessible
109 return s
110 }
111
112 // New creates a new spinner.
113 func New() *Spinner {
114 s := spinner.New()
115 s.Spinner = spinner.Dot
116
117 s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#F780E2"))
118
119 return &Spinner{
120 spinner: s,
121 title: "Loading...",
122 titleStyle: lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#00020A", Dark: "#FFFDF5"}),
123 }
124 }
125
126 // Init initializes the spinner.
127 func (s *Spinner) Init() tea.Cmd {
128 return tea.Batch(s.spinner.Tick, func() tea.Msg {
129 if s.action != nil {
130 err := s.action(s.ctx)
131 return doneMsg{err}
132 }
133 return nil
134 })
135 }
136
137 // Update updates the spinner.
138 func (s *Spinner) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
139 switch msg := msg.(type) {
140 case doneMsg:
141 s.err = msg.err
142 return s, tea.Quit
143 case tea.KeyMsg:
144 switch msg.String() {
145 case "ctrl+c":
146 return s, tea.Interrupt
147 }
148 }
149
150 var cmd tea.Cmd
151 s.spinner, cmd = s.spinner.Update(msg)
152 return s, cmd
153 }
154
155 // View returns the spinner view.
156 func (s *Spinner) View() string {
157 var title string
158 if s.title != "" {
159 title = s.titleStyle.Render(s.title)
160 }
161 return s.spinner.View() + title
162 }
163
164 // Run runs the spinner.
165 func (s *Spinner) Run() error {
166 if s.ctx == nil && s.action == nil {
167 return nil
168 }
169 if s.ctx == nil {
170 s.ctx = context.Background()
171 }
172 if err := s.ctx.Err(); err != nil {
173 return err
174 }
175
176 if s.accessible {
177 out := cmp.Or[io.Writer](s.output, os.Stdout)
178 return s.runAccessible(out)
179 }
180
181 m, err := tea.NewProgram(
182 s,
183 tea.WithContext(s.ctx),
184 tea.WithOutput(s.output),
185 tea.WithInput(nil),
186 ).Run()
187 mm := m.(*Spinner)
188 if mm.err != nil {
189 return mm.err
190 }
191 return err
192 }
193
194 // runAccessible runs the spinner in an accessible mode (statically).
195 func (s *Spinner) runAccessible(out io.Writer) error {
196 output := termenv.NewOutput(out)
197 output.HideCursor()
198 frame := s.spinner.Style.Render("...")
199 title := s.titleStyle.Render(strings.TrimSuffix(s.title, "..."))
200 _, _ = io.WriteString(out, title+frame)
201
202 defer func() {
203 output.ShowCursor()
204 output.CursorBack(len(frame) + len(title))
205 }()
206
207 actionDone := make(chan error)
208 if s.action != nil {
209 go func() {
210 actionDone <- s.action(s.ctx)
211 }()
212 }
213
214 for {
215 select {
216 case <-s.ctx.Done():
217 return s.ctx.Err()
218 case err := <-actionDone:
219 return err
220 }
221 }
222 }
223
224 type doneMsg struct {
225 err error
226 }