Skip to content
Myr

Spinner example

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 02d3086 · Browse files · 2 files+88 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

2 files+88 added−0 removed

  • other:+88 added−0 removed

2 files+88 added−0 removed

added:examples/spinner/main.go+85 added
1package main23import (4 "fmt"5 "log"67 "github.com/charmbracelet/tea"8 "github.com/charmbracelet/teaparty/spinner"9 "github.com/muesli/termenv"10)1112var (13 color = termenv.ColorProfile()14)1516type Model struct {17 spinner spinner.Model18}1920func main() {21 p := tea.NewProgram(initialize, update, view, subscriptions)22 if err := p.Start(); err != nil {23 log.Fatal(err)24 }25}2627func initialize() (tea.Model, tea.Cmd) {28 m := spinner.NewModel()29 m.Type = spinner.Dot3031 return Model{32 spinner: m,33 }, nil34}3536func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {37 m, ok := model.(Model)38 if !ok {39 return model, nil40 }4142 switch msg := msg.(type) {4344 case tea.KeyMsg:45 switch msg.String() {46 case "q":47 fallthrough48 case "esc":49 fallthrough50 case "ctrl+c":51 return m, tea.Quit52 default:53 return m, nil54 }5556 default:57 m.spinner, _ = spinner.Update(msg, m.spinner)58 return m, nil59 }6061}6263func view(model tea.Model) string {64 m, ok := model.(Model)65 if !ok {66 return tea.ModelAssertionErr.String()67 }68 s := termenv.69 String(spinner.View(m.spinner)).70 Foreground(color.Color("205")).71 String()72 return fmt.Sprintf("\n\n %s Loading forever...press q to quit\n\n", s)73}7475func subscriptions(_ tea.Model) tea.Subs {76 return tea.Subs{77 "tick": func(model tea.Model) tea.Msg {78 m, ok := model.(Model)79 if !ok {80 return tea.ModelAssertionErr81 }82 return spinner.Sub(m.spinner)83 },84 }85}
modified:tea.go+3 added
91 unmodified lines
92func NewErrMsgFromErr(e error) ErrMsg {93 return ErrMsg{e}94}9596// ModelAssertionErr can be used when a model assertion didn't go as planned97var ModelAssertionErr = NewErrMsg("could not perform assertion on model")9899// Quit is a command that tells the program to exit100func Quit(_ Model) Msg {101 return quitMsg{}102}
184 unmodified lines