mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-03-21 08:03:43 +03:00
Add a dot spinner type
This commit is contained in:
parent
5c5cc7bb87
commit
b4d35fd529
@ -7,23 +7,37 @@ import (
|
|||||||
"github.com/charmbracelet/tea"
|
"github.com/charmbracelet/tea"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Spinner animation frames
|
// Spinner denotes a type of spinner
|
||||||
|
type Spinner = int
|
||||||
|
|
||||||
|
// Available types of spinners
|
||||||
|
const (
|
||||||
|
Line Spinner = iota
|
||||||
|
Dot
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
simple = []string{"|", "/", "-", "\\"}
|
// Spinner frames
|
||||||
|
spinners = map[Spinner][]string{
|
||||||
|
Line: []string{"|", "/", "-", "\\"},
|
||||||
|
Dot: []string{"⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "},
|
||||||
|
}
|
||||||
|
|
||||||
|
assertionErr = errors.New("could not perform assertion on model to what the spinner expects. are you sure you passed the right value?")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Model contains the state for the spinner. Use NewModel to create new models
|
// Model contains the state for the spinner. Use NewModel to create new models
|
||||||
// rather than using Model as a struct literal.
|
// rather than using Model as a struct literal.
|
||||||
type Model struct {
|
type Model struct {
|
||||||
|
Type Spinner
|
||||||
FPS int
|
FPS int
|
||||||
frame int
|
frame int
|
||||||
}
|
}
|
||||||
|
|
||||||
var assertionErr = errors.New("could not perform assertion on model to what the spinner expects. are you sure you passed the right value?")
|
|
||||||
|
|
||||||
// NewModel returns a model with default values
|
// NewModel returns a model with default values
|
||||||
func NewModel() Model {
|
func NewModel() Model {
|
||||||
return Model{
|
return Model{
|
||||||
|
Type: Line,
|
||||||
FPS: 9,
|
FPS: 9,
|
||||||
frame: 0,
|
frame: 0,
|
||||||
}
|
}
|
||||||
@ -37,7 +51,7 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
switch msg.(type) {
|
switch msg.(type) {
|
||||||
case TickMsg:
|
case TickMsg:
|
||||||
m.frame++
|
m.frame++
|
||||||
if m.frame >= len(simple) {
|
if m.frame >= len(spinners[m.Type]) {
|
||||||
m.frame = 0
|
m.frame = 0
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
@ -48,10 +62,11 @@ func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
|||||||
|
|
||||||
// View renders the model's view
|
// View renders the model's view
|
||||||
func View(model Model) string {
|
func View(model Model) string {
|
||||||
if model.frame >= len(simple) {
|
s := spinners[model.Type]
|
||||||
|
if model.frame >= len(s) {
|
||||||
return "[error]"
|
return "[error]"
|
||||||
}
|
}
|
||||||
return simple[model.frame]
|
return s[model.frame]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sub is the subscription that allows the spinner to spin
|
// Sub is the subscription that allows the spinner to spin
|
||||||
|
Loading…
Reference in New Issue
Block a user