Fix spinner frame skipping and remove custom message functionality

This commit is contained in:
Christian Rocha 2020-06-22 14:49:21 -04:00
parent 2525319d72
commit 35c3cd626d
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -17,7 +17,7 @@ const (
) )
const ( const (
defaultFPS = 9 defaultFPS = time.Second / 10
) )
var ( var (
@ -38,7 +38,7 @@ type Model struct {
Type Spinner Type Spinner
// FPS is the speed at which the ticker should tick // FPS is the speed at which the ticker should tick
FPS int FPS time.Duration
// ForegroundColor sets the background color of the spinner. It can be a // ForegroundColor sets the background color of the spinner. It can be a
// hex code or one of the 256 ANSI colors. If the terminal emulator can't // hex code or one of the 256 ANSI colors. If the terminal emulator can't
@ -52,12 +52,6 @@ type Model struct {
// (per github.com/muesli/termenv). // (per github.com/muesli/termenv).
BackgroundColor string BackgroundColor string
// CustomMsgFunc can be used to a custom message on tick. This can be
// useful when you have spinners in different parts of your application and
// want to differentiate between the messages for clarity and simplicity.
// If nil, this setting is ignored.
CustomMsgFunc func() tea.Msg
frame int frame int
} }
@ -76,14 +70,14 @@ type TickMsg struct{}
// every time it's called, regardless the message passed, so be sure the logic // every time it's called, regardless the message passed, so be sure the logic
// is setup so as not to call this Update needlessly. // is setup so as not to call this Update needlessly.
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) { func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
m.frame++ if _, ok := msg.(TickMsg); ok {
if m.frame >= len(spinners[m.Type]) { m.frame++
m.frame = 0 if m.frame >= len(spinners[m.Type]) {
} m.frame = 0
if m.CustomMsgFunc != nil { }
return m, Tick(m) return m, Tick(m)
} }
return m, Tick(m) return m, nil
} }
// View renders the model's view. // View renders the model's view.
@ -107,12 +101,8 @@ func View(model Model) string {
} }
// Tick is the command used to advance the spinner one frame. // Tick is the command used to advance the spinner one frame.
func Tick(model Model) tea.Cmd { func Tick(m Model) tea.Cmd {
return func() tea.Msg { return tea.Tick(m.FPS, func(time.Time) tea.Msg {
time.Sleep(time.Second / time.Duration(model.FPS))
if model.CustomMsgFunc != nil {
return model.CustomMsgFunc()
}
return TickMsg{} return TickMsg{}
} })
} }