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