Skip to content
Myr

Fix and improve 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 39ec5b5 · Browse files · 1 file+13 added−2 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+13 added−2 removed

  • other:+13 added−2 removed

1 file+13 added−2 removed

modified:examples/spinner/main.go+13 added-2 removed
1package main23import (4 "errors"5 "fmt"6 "log"78 "github.com/charmbracelet/tea"
6 unmodified lines
15)1617type Model struct {18 spinner spinner.Model19 err error20}2122type errMsg error2324func main() {25 p := tea.NewProgram(initialize, update, view, subscriptions)26 if err := p.Start(); err != nil {27 log.Fatal(err)
28 unmodified lines
56 default:57 return m, nil58 }5960 case errMsg:61 m.err = msg62 return m, nil6364 default:65 m.spinner, _ = spinner.Update(msg, m.spinner)66 return m, nil67 }
2 unmodified lines
7071func view(model tea.Model) string {72 m, ok := model.(Model)73 if !ok {66 return tea.ModelAssertionErr.String()74 return "could not perform assertion on model in view"75 }76 if m.err != nil {77 return m.err.Error()78 }79 s := termenv.80 String(spinner.View(m.spinner)).81 Foreground(color.Color("205")).82 String()
4 unmodified lines
87 return tea.Subs{88 "tick": func(model tea.Model) tea.Msg {89 m, ok := model.(Model)90 if !ok {80 return tea.ModelAssertionErr91 return errMsg(errors.New("could perform assertion on model in subscription"))92 }93 return spinner.Sub(m.spinner)94 },95 }
1 unmodified line