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
Provenance
Commit
5b001ee
Tree
ea271dc
Author
Christian Rocha <christian@rocha.is>
Committer
Christian Rocha <christian@rocha.is>
Parent
02d3086
2 files changed+88 added−0 removed
added:examples/spinner/main.go+85 added
1+package main2+3+import (4+ "fmt"5+ "log"6+7+ "github.com/charmbracelet/tea"8+ "github.com/charmbracelet/teaparty/spinner"9+ "github.com/muesli/termenv"10+)11+12+var (13+ color = termenv.ColorProfile()14+)15+16+type Model struct {17+ spinner spinner.Model18+}19+20+func main() {21+ p := tea.NewProgram(initialize, update, view, subscriptions)22+ if err := p.Start(); err != nil {23+ log.Fatal(err)24+ }25+}26+27+func initialize() (tea.Model, tea.Cmd) {28+ m := spinner.NewModel()29+ m.Type = spinner.Dot30+31+ return Model{32+ spinner: m,33+ }, nil34+}35+36+func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {37+ m, ok := model.(Model)38+ if !ok {39+ return model, nil40+ }41+42+ switch msg := msg.(type) {43+44+ 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+ }55+56+ default:57+ m.spinner, _ = spinner.Update(msg, m.spinner)58+ return m, nil59+ }60+61+}62+63+func 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+}74+75+func 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 planned97+var ModelAssertionErr = NewErrMsg("could not perform assertion on model")98+99// Quit is a command that tells the program to exit100func Quit(_ Model) Msg {101 return quitMsg{}102}