forked from Mirrors/bubbletea
101 lines
1.9 KiB
Go
101 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
)
|
|
|
|
var (
|
|
// Available spinners
|
|
spinners = []spinner.Spinner{
|
|
spinner.Line,
|
|
spinner.Dot,
|
|
spinner.MiniDot,
|
|
spinner.Jump,
|
|
spinner.Pulse,
|
|
spinner.Points,
|
|
spinner.Globe,
|
|
spinner.Moon,
|
|
spinner.Monkey,
|
|
}
|
|
|
|
textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("252")).Render
|
|
spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))
|
|
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
|
|
)
|
|
|
|
func main() {
|
|
m := model{}
|
|
m.resetSpinner()
|
|
|
|
if err := tea.NewProgram(m).Start(); err != nil {
|
|
fmt.Println("could not run program:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
type model struct {
|
|
index int
|
|
spinner spinner.Model
|
|
}
|
|
|
|
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 "ctrl+c", "q", "esc":
|
|
return m, tea.Quit
|
|
case "h", "left":
|
|
m.index--
|
|
if m.index <= 0 {
|
|
m.index = len(spinners) - 1
|
|
}
|
|
m.resetSpinner()
|
|
return m, m.spinner.Tick
|
|
case "l", "right":
|
|
m.index++
|
|
if m.index >= len(spinners) {
|
|
m.index = 0
|
|
}
|
|
m.resetSpinner()
|
|
return m, m.spinner.Tick
|
|
default:
|
|
return m, nil
|
|
}
|
|
case spinner.TickMsg:
|
|
var cmd tea.Cmd
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
|
return m, cmd
|
|
default:
|
|
return m, nil
|
|
}
|
|
}
|
|
|
|
func (m *model) resetSpinner() {
|
|
m.spinner = spinner.New()
|
|
m.spinner.Style = spinnerStyle
|
|
m.spinner.Spinner = spinners[m.index]
|
|
}
|
|
|
|
func (m model) View() (s string) {
|
|
var gap string
|
|
switch m.index {
|
|
case 1:
|
|
gap = ""
|
|
default:
|
|
gap = " "
|
|
}
|
|
|
|
s += fmt.Sprintf("\n %s%s%s\n\n", m.spinner.View(), gap, textStyle("Spinning..."))
|
|
s += helpStyle("h/l, ←/→: change spinner • q: exit\n")
|
|
return
|
|
}
|