mirror of
https://github.com/Maks1mS/bubbles.git
synced 2025-10-18 16:38:56 +03:00
Compare commits
4 Commits
master
...
stable-v0.
Author | SHA1 | Date | |
---|---|---|---|
|
14d9f9cd76 | ||
|
69bf367d37 | ||
|
2578480343 | ||
|
136e1f05bf |
@@ -109,11 +109,17 @@ func (m Model) ID() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New returns a model with default values.
|
// New returns a model with default values.
|
||||||
func New() Model {
|
func New(opts ...Option) Model {
|
||||||
return Model{
|
m := Model{
|
||||||
Spinner: Line,
|
Spinner: Line,
|
||||||
id: nextID(),
|
id: nextID(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&m)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewModel returns a model with default values.
|
// NewModel returns a model with default values.
|
||||||
@@ -199,3 +205,23 @@ func (m Model) tick(id, tag int) tea.Cmd {
|
|||||||
func Tick() tea.Msg {
|
func Tick() tea.Msg {
|
||||||
return TickMsg{Time: time.Now()}
|
return TickMsg{Time: time.Now()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Option is used to set options in New. For example:
|
||||||
|
//
|
||||||
|
// spinner := New(WithSpinner(Dot))
|
||||||
|
//
|
||||||
|
type Option func(*Model)
|
||||||
|
|
||||||
|
// WithSpinner is an option to set the spinner.
|
||||||
|
func WithSpinner(spinner Spinner) Option {
|
||||||
|
return func(m *Model) {
|
||||||
|
m.Spinner = spinner
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithStyle is an option to set the spinner style.
|
||||||
|
func WithStyle(style lipgloss.Style) Option {
|
||||||
|
return func(m *Model) {
|
||||||
|
m.Style = style
|
||||||
|
}
|
||||||
|
}
|
||||||
|
61
spinner/spinner_test.go
Normal file
61
spinner/spinner_test.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package spinner_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSpinnerNew(t *testing.T) {
|
||||||
|
assertEqualSpinner := func(t *testing.T, exp, got spinner.Spinner) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if exp.FPS != got.FPS {
|
||||||
|
t.Errorf("expecting %d FPS, got %d", exp.FPS, got.FPS)
|
||||||
|
}
|
||||||
|
|
||||||
|
if e, g := len(exp.Frames), len(got.Frames); e != g {
|
||||||
|
t.Fatalf("expecting %d frames, got %d", e, g)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, e := range exp.Frames {
|
||||||
|
if g := got.Frames[i]; e != g {
|
||||||
|
t.Errorf("expecting frame index %d with value %q, got %q", i, e, g)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Run("default", func(t *testing.T) {
|
||||||
|
s := spinner.New()
|
||||||
|
|
||||||
|
assertEqualSpinner(t, spinner.Line, s.Spinner)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("WithSpinner", func(t *testing.T) {
|
||||||
|
customSpinner := spinner.Spinner{
|
||||||
|
Frames: []string{"a", "b", "c", "d"},
|
||||||
|
FPS: 16,
|
||||||
|
}
|
||||||
|
|
||||||
|
s := spinner.New(spinner.WithSpinner(customSpinner))
|
||||||
|
|
||||||
|
assertEqualSpinner(t, customSpinner, s.Spinner)
|
||||||
|
})
|
||||||
|
|
||||||
|
tests := map[string]spinner.Spinner{
|
||||||
|
"Line": spinner.Line,
|
||||||
|
"Dot": spinner.Dot,
|
||||||
|
"MiniDot": spinner.MiniDot,
|
||||||
|
"Jump": spinner.Jump,
|
||||||
|
"Pulse": spinner.Pulse,
|
||||||
|
"Points": spinner.Points,
|
||||||
|
"Globe": spinner.Globe,
|
||||||
|
"Moon": spinner.Moon,
|
||||||
|
"Monkey": spinner.Monkey,
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, s := range tests {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
assertEqualSpinner(t, spinner.New(spinner.WithSpinner(s)).Spinner, s)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@@ -205,7 +205,7 @@ func (m *Model) SetValue(s string) {
|
|||||||
} else {
|
} else {
|
||||||
m.value = runes
|
m.value = runes
|
||||||
}
|
}
|
||||||
if (m.pos == 0 && len(m.value) == 0) || m.pos > len(m.value) {
|
if m.pos == 0 || m.pos > len(m.value) {
|
||||||
m.setCursor(len(m.value))
|
m.setCursor(len(m.value))
|
||||||
}
|
}
|
||||||
m.handleOverflow()
|
m.handleOverflow()
|
||||||
|
@@ -162,9 +162,7 @@ func (m *Model) Start() tea.Cmd {
|
|||||||
|
|
||||||
// Stop pauses the timer. Has no effect if the timer has timed out.
|
// Stop pauses the timer. Has no effect if the timer has timed out.
|
||||||
func (m *Model) Stop() tea.Cmd {
|
func (m *Model) Stop() tea.Cmd {
|
||||||
return func() tea.Msg {
|
return m.startStop(false)
|
||||||
return m.startStop(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle stops the timer if it's running and starts it if it's stopped.
|
// Toggle stops the timer if it's running and starts it if it's stopped.
|
||||||
|
Reference in New Issue
Block a user