7 Commits

Author SHA1 Message Date
Christian Rocha
9cb8e8d90a Remove spaces after emoji spinners 2020-11-11 15:39:42 -05:00
Christian Rocha
e6219572e5 Add points spinner 2020-11-11 15:39:42 -05:00
Christian Rocha
81feaacf5b Remove bit8 spinner 2020-11-11 15:39:42 -05:00
Christian Rocha
9a25d8b8b9 Add pulse spinner 2020-11-11 15:39:42 -05:00
Christian Rocha
e8e052c64b Add mini dot spinner 2020-11-11 15:39:42 -05:00
Christian Rocha
3d7cd43046 Rework Spinner so that default spinners have FPS built-in 2020-11-11 15:39:42 -05:00
Christian Rocha
9c38e101d2 Remove internal msg in spinner so all tick msgs can be caught externally 2020-11-10 15:44:42 -05:00

View File

@@ -10,33 +10,49 @@ import (
const defaultFPS = time.Second / 10 const defaultFPS = time.Second / 10
// Spinner is a set of frames used in animating the spinner. // Spinner is a set of frames used in animating the spinner.
type Spinner = []string type Spinner struct {
Frames []string
FPS time.Duration
}
var ( var (
// Some spinners to choose from. You could also make your own. // Some spinners to choose from. You could also make your own.
Line = Spinner{"|", "/", "-", "\\"} Line = Spinner{
Dot = Spinner{"⣾ ", "⣽ ", "⣻ ", "", "", "", "⣯ ", "⣷ "} Frames: []string{"|", "/", "-", "\\"},
Globe = Spinner{"🌍 ", "🌎 ", "🌏 "} FPS: time.Second / 10,
Moon = Spinner{"🌑 ", "🌒 ", "🌓 ", "🌔 ", "🌕 ", "🌖 ", "🌗 ", "🌘 "} }
Monkey = Spinner{"🙈 ", "🙈 ", "🙉 ", "🙊 "} Dot = Spinner{
Jump = Spinner{"", "", "", "", "", "", ""} Frames: []string{"", "", "", "", "", "", "⣯ ", "⣷ "},
Bit8 = Spinner{ FPS: time.Second / 10,
"", "⠁", "⠂", "⠃", "⠄", "⠅", "⠆", "⠇", "⡀", "⡁", "⡂", "⡃", "⡄", "⡅", "⡆", "⡇", }
"⠈", "⠉", "⠊", "⠋", "⠌", "⠍", "⠎", "⠏", "⡈", "⡉", "⡊", "⡋", "⡌", "⡍", "⡎", "⡏", MiniDot = Spinner{
"⠐", "⠑", "⠒", "⠓", "⠔", "", "", "", "", "", "", "", "", "", "", "⡗", Frames: []string{"", "", "", "", "", "", "", "", "", ""},
"⠘", "⠙", "⠚", "⠛", "⠜", "⠝", "⠞", "⠟", "⡘", "⡙", "⡚", "⡛", "⡜", "⡝", "⡞", "⡟", FPS: time.Second / 12,
"⠠", "⠡", "⠢", "⠣", "⠤", "⠥", "⠦", "⠧", "⡠", "⡡", "⡢", "⡣", "⡤", "⡥", "⡦", "⡧", }
"⠨", "⠩", "⠪", "⠫", "⠬", "⠭", "⠮", "⠯", "⡨", "⡩", "⡪", "⡫", "⡬", "⡭", "⡮", "⡯", Jump = Spinner{
"⠰", "⠱", "⠲", "⠳", "", "", "", "", "⡰", "⡱", "⡲", "⡳", "", "", "⡶", "⡷", Frames: []string{"", "", "", "", "", "", ""},
"⠸", "⠹", "⠺", "⠻", "⠼", "⠽", "⠾", "⠿", "⡸", "⡹", "⡺", "⡻", "⡼", "⡽", "⡾", "⡿", FPS: time.Second / 10,
"⢀", "⢁", "⢂", "⢃", "⢄", "⢅", "⢆", "⢇", "⣀", "⣁", "⣂", "⣃", "⣄", "⣅", "⣆", "⣇", }
"⢈", "⢉", "⢊", "⢋", "⢌", "⢍", "⢎", "⢏", "⣈", "⣉", "⣊", "⣋", "⣌", "⣍", "⣎", "⣏", Pulse = Spinner{
"⢐", "⢑", "⢒", "⢓", "⢔", "⢕", "⢖", "⢗", "⣐", "⣑", "⣒", "", "", "", "", "⣗", Frames: []string{"", "", "", ""},
"⢘", "⢙", "⢚", "⢛", "⢜", "⢝", "⢞", "⢟", "⣘", "⣙", "⣚", "⣛", "⣜", "⣝", "⣞", "⣟", FPS: time.Second / 8,
"⢠", "⢡", "⢢", "⢣", "⢤", "⢥", "⢦", "⢧", "⣠", "⣡", "⣢", "⣣", "⣤", "⣥", "⣦", "⣧", }
"⢨", "⢩", "⢪", "⢫", "⢬", "⢭", "⢮", "⢯", "⣨", "⣩", "⣪", "⣫", "⣬", "⣭", "⣮", "⣯", Points = Spinner{
"⢰", "⢱", "⢲", "⢳", "⢴", "⢵", "⢶", "⢷", "⣰", "⣱", "⣲", "⣳", "", "", "⣶", "⣷", Frames: []string{"∙∙∙", "●∙∙", "∙●∙", "∙∙●"},
"⢸", "⢹", "⢺", "⢻", "⢼", "⢽", "⢾", "⢿", "⣸", "⣹", "⣺", "⣻", "⣼", "⣽", "⣾", "⣿"} FPS: time.Second / 7,
}
Globe = Spinner{
Frames: []string{"🌍", "🌎", "🌏"},
FPS: time.Second / 4,
}
Moon = Spinner{
Frames: []string{"🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"},
FPS: time.Second / 8,
}
Monkey = Spinner{
Frames: []string{"🙈", "🙉", "🙊"},
FPS: time.Second / 3,
}
color = termenv.ColorProfile().Color color = termenv.ColorProfile().Color
) )
@@ -45,22 +61,19 @@ var (
// rather than using Model as a struct literal. // rather than using Model as a struct literal.
type Model struct { type Model struct {
// Type is the set of frames to use. See Spinner. // Spinner settings to use. See type Spinner.
Frames Spinner Spinner Spinner
// FPS is the speed at which the ticker should tick. // ForegroundColor sets the background color of the spinner. It can be
FPS time.Duration // a hex code or one of the 256 ANSI colors. If the terminal emulator can't
// support the color specified it will automatically degrade (per
// ForegroundColor sets the background color of the spinner. It can be a // github.com/muesli/termenv).
// hex code or one of the 256 ANSI colors. If the terminal emulator can't
// doesn't support the color specified it will automatically degrade
// (per github.com/muesli/termenv).
ForegroundColor string ForegroundColor string
// BackgroundColor sets the background color of the spinner. It can be a // BackgroundColor sets the background color of the spinner. It can be
// hex code or one of the 256 ANSI colors. If the terminal emulator can't // a hex code or one of the 256 ANSI colors. If the terminal emulator can't
// doesn't support the color specified it will automatically degrade // support the color specified it will automatically degrade (per
// (per github.com/muesli/termenv). // github.com/muesli/termenv).
BackgroundColor string BackgroundColor string
// MinimumLifetime is the minimum amount of time the spinner can run. Any // MinimumLifetime is the minimum amount of time the spinner can run. Any
@@ -137,10 +150,7 @@ func (m Model) Visible() bool {
// NewModel returns a model with default values. // NewModel returns a model with default values.
func NewModel() Model { func NewModel() Model {
return Model{ return Model{Spinner: Line}
Frames: Line,
FPS: defaultFPS,
}
} }
// TickMsg indicates that the timer has ticked and we should render a frame. // TickMsg indicates that the timer has ticked and we should render a frame.
@@ -148,18 +158,14 @@ type TickMsg struct {
Time time.Time Time time.Time
} }
type startTick struct{}
// Update is the Tea update function. This will advance the spinner one frame // Update is the Tea update function. This will advance the spinner one frame
// 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 (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg.(type) { switch msg.(type) {
case startTick:
return m, m.tick()
case TickMsg: case TickMsg:
m.frame++ m.frame++
if m.frame >= len(m.Frames) { if m.frame >= len(m.Spinner.Frames) {
m.frame = 0 m.frame = 0
} }
return m, m.tick() return m, m.tick()
@@ -170,11 +176,11 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
// View renders the model's view. // View renders the model's view.
func (m Model) View() string { func (m Model) View() string {
if m.frame >= len(m.Frames) { if m.frame >= len(m.Spinner.Frames) {
return "error" return "(error)"
} }
frame := m.Frames[m.frame] frame := m.Spinner.Frames[m.frame]
if m.ForegroundColor != "" || m.BackgroundColor != "" { if m.ForegroundColor != "" || m.BackgroundColor != "" {
return termenv. return termenv.
@@ -189,11 +195,11 @@ func (m Model) View() 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() tea.Msg { func Tick() tea.Msg {
return startTick{} return TickMsg{Time: time.Now()}
} }
func (m Model) tick() tea.Cmd { func (m Model) tick() tea.Cmd {
return tea.Tick(m.FPS, func(t time.Time) tea.Msg { return tea.Tick(m.Spinner.FPS, func(t time.Time) tea.Msg {
return TickMsg{ return TickMsg{
Time: t, Time: t,
} }