mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-18 16:38:56 +03:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
9cb8e8d90a | ||
|
e6219572e5 | ||
|
81feaacf5b | ||
|
9a25d8b8b9 | ||
|
e8e052c64b | ||
|
3d7cd43046 | ||
|
9c38e101d2 |
@@ -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,
|
||||
}
|
||||
|
Reference in New Issue
Block a user