Replies: 1 comment 2 replies
-
|
Hi! Just stop sending messages to it (i.e. don't call if spin {
// Spin!
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
} else {
// Don't spin.
}Here's an example where you can press s to start and stop the spinner: package main
// A simple program demonstrating the spinner component from the Bubbles
// component library.
import (
"fmt"
"os"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type errMsg error
type model struct {
spinner spinner.Model
spin bool
quitting bool
err error
}
func initialModel() model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return model{
spinner: s,
spin: true,
}
}
func (m model) Init() tea.Cmd {
return m.spinner.Tick
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
case "s":
var cmd tea.Cmd
m.spin = !m.spin // toggle the spinner
if m.spin {
cmd = m.spinner.Tick // restart the spinner
}
return m, cmd
default:
return m, nil
}
case errMsg:
m.err = msg
return m, nil
case spinner.TickMsg:
var cmd tea.Cmd
// If m.spin is false, don't update on the spinner, effectively stopping it.
if m.spin {
m.spinner, cmd = m.spinner.Update(msg)
}
return m, cmd
default:
return m, nil
}
}
func (m model) View() string {
if m.err != nil {
return m.err.Error()
}
str := fmt.Sprintf("\n\n %s Loading forever...press s to toggle and q to quit\n\n", m.spinner.View())
if m.quitting {
return str + "\n"
}
return str
}
func main() {
p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all ,
how can i stop the spinner animation plz ?
Beta Was this translation helpful? Give feedback.
All reactions