2020-02-10 19:40:52 +03:00
|
|
|
package spinner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/tea"
|
2020-03-27 21:10:09 +03:00
|
|
|
"github.com/muesli/termenv"
|
2020-02-10 19:40:52 +03:00
|
|
|
)
|
|
|
|
|
2020-02-10 19:51:08 +03:00
|
|
|
// Spinner denotes a type of spinner
|
|
|
|
type Spinner = int
|
|
|
|
|
|
|
|
// Available types of spinners
|
|
|
|
const (
|
|
|
|
Line Spinner = iota
|
|
|
|
Dot
|
|
|
|
)
|
|
|
|
|
2020-02-10 19:40:52 +03:00
|
|
|
var (
|
2020-02-10 19:51:08 +03:00
|
|
|
// Spinner frames
|
|
|
|
spinners = map[Spinner][]string{
|
2020-04-22 21:29:30 +03:00
|
|
|
Line: {"|", "/", "-", "\\"},
|
|
|
|
Dot: {"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "},
|
2020-02-10 19:51:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
assertionErr = errors.New("could not perform assertion on model to what the spinner expects. are you sure you passed the right value?")
|
2020-03-27 21:10:09 +03:00
|
|
|
|
|
|
|
color = termenv.ColorProfile().Color
|
2020-02-10 19:40:52 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Model contains the state for the spinner. Use NewModel to create new models
|
|
|
|
// rather than using Model as a struct literal.
|
|
|
|
type Model struct {
|
2020-03-27 21:10:09 +03:00
|
|
|
Type Spinner
|
|
|
|
FPS int
|
|
|
|
ForegroundColor string
|
|
|
|
BackgroundColor string
|
|
|
|
|
2020-02-10 19:40:52 +03:00
|
|
|
frame int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewModel returns a model with default values
|
|
|
|
func NewModel() Model {
|
|
|
|
return Model{
|
2020-02-10 19:51:08 +03:00
|
|
|
Type: Line,
|
2020-02-10 19:40:52 +03:00
|
|
|
FPS: 9,
|
|
|
|
frame: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TickMsg indicates that the timer has ticked and we should render a frame
|
|
|
|
type TickMsg struct{}
|
|
|
|
|
|
|
|
// Update is the Tea update function
|
|
|
|
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|
|
|
switch msg.(type) {
|
|
|
|
case TickMsg:
|
|
|
|
m.frame++
|
2020-02-10 19:51:08 +03:00
|
|
|
if m.frame >= len(spinners[m.Type]) {
|
2020-02-10 19:40:52 +03:00
|
|
|
m.frame = 0
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// View renders the model's view
|
|
|
|
func View(model Model) string {
|
2020-02-10 19:51:08 +03:00
|
|
|
s := spinners[model.Type]
|
|
|
|
if model.frame >= len(s) {
|
2020-02-10 19:40:52 +03:00
|
|
|
return "[error]"
|
|
|
|
}
|
2020-03-27 21:10:09 +03:00
|
|
|
|
|
|
|
str := s[model.frame]
|
|
|
|
|
|
|
|
if model.ForegroundColor != "" || model.BackgroundColor != "" {
|
|
|
|
return termenv.
|
|
|
|
String(str).
|
|
|
|
Foreground(color(model.ForegroundColor)).
|
|
|
|
Background(color(model.BackgroundColor)).
|
|
|
|
String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
2020-02-10 19:40:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sub is the subscription that allows the spinner to spin
|
|
|
|
func Sub(model tea.Model) tea.Msg {
|
|
|
|
m, ok := model.(Model)
|
|
|
|
if !ok {
|
|
|
|
return assertionErr
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second / time.Duration(m.FPS))
|
|
|
|
return TickMsg{}
|
|
|
|
}
|