Skip to content
Myr

Add spinner browser to examples

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent fce5ce7 · Browse files · 1 file+99 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+99 added−0 removed

  • other:+99 added−0 removed

1 file+99 added−0 removed

added:examples/spinners/main.go+99 added
1package main23import (4 "fmt"5 "os"67 "github.com/charmbracelet/bubbles/spinner"8 tea "github.com/charmbracelet/bubbletea"9 "github.com/muesli/termenv"10)1112var (13 // Available spinners14 spinners = []spinner.Spinner{15 spinner.Line,16 spinner.Dot,17 spinner.MiniDot,18 spinner.Jump,19 spinner.Pulse,20 spinner.Points,21 spinner.Globe,22 spinner.Moon,23 spinner.Monkey,24 }2526 color = termenv.ColorProfile().Color27 textStyle = termenv.Style{}.Foreground(color("252")).Styled28 helpStyle = termenv.Style{}.Foreground(color("241")).Styled29)3031func main() {32 m := model{}33 m.resetSpinner()3435 if err := tea.NewProgram(m).Start(); err != nil {36 fmt.Println("could not run program:", err)37 os.Exit(1)38 }39}4041type model struct {42 index int43 spinner spinner.Model44}4546func (m model) Init() tea.Cmd {47 return spinner.Tick48}4950func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {51 switch msg := msg.(type) {52 case tea.KeyMsg:53 switch msg.String() {54 case "h", "left":55 m.index--56 if m.index <= 0 {57 m.index = len(spinners) - 158 }59 m.resetSpinner()60 return m, nil61 case "l", "right":62 m.index++63 if m.index >= len(spinners) {64 m.index = 065 }66 m.resetSpinner()67 return m, nil68 case "ctrl+c", "q":69 return m, tea.Quit70 default:71 return m, nil72 }73 case spinner.TickMsg:74 var cmd tea.Cmd75 m.spinner, cmd = m.spinner.Update(msg)76 return m, cmd77 default:78 return m, nil79 }80}8182func (m *model) resetSpinner() {83 m.spinner = spinner.NewModel()84 m.spinner.ForegroundColor = "69"85 m.spinner.Spinner = spinners[m.index]86}8788func (m model) View() (s string) {89 gap := " "90 switch m.index {91 case 1:92 gap = ""93 default:94 gap = " "95 }96 s += fmt.Sprintf("\n %s%s%s\n\n", m.spinner.View(), gap, textStyle("Spinning..."))97 s += helpStyle("j/k, ←/→: change spinner • q: exit\n")98 return99}