2020-02-10 12:46:12 -05:00
|
|
|
package main
|
|
|
|
|
2020-10-14 11:51:04 -04:00
|
|
|
// A simple program demonstrating the spinner component from the Bubbles
|
|
|
|
// component library.
|
|
|
|
|
2020-02-10 12:46:12 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2020-05-12 17:05:16 -04:00
|
|
|
"os"
|
2020-02-10 12:46:12 -05:00
|
|
|
|
2020-05-25 20:10:15 -04:00
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
2020-05-25 19:26:40 -04:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2021-04-13 21:28:39 -04:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2020-02-10 12:46:12 -05:00
|
|
|
)
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
type errMsg error
|
2020-02-10 12:46:12 -05:00
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
type model struct {
|
2020-05-12 17:05:16 -04:00
|
|
|
spinner spinner.Model
|
|
|
|
quitting bool
|
|
|
|
err error
|
2020-02-10 12:46:12 -05:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func initialModel() model {
|
2022-01-10 20:42:10 -05:00
|
|
|
s := spinner.New()
|
2020-11-12 11:30:03 -05:00
|
|
|
s.Spinner = spinner.Dot
|
2021-04-13 21:28:39 -04:00
|
|
|
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
|
2020-10-15 19:48:42 -04:00
|
|
|
return model{spinner: s}
|
2020-02-10 12:46:12 -05:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
2022-01-10 14:20:15 -05:00
|
|
|
return m.spinner.Tick
|
2020-10-15 19:48:42 -04:00
|
|
|
}
|
2020-02-10 12:46:12 -05:00
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2020-02-10 12:46:12 -05:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
case tea.KeyMsg:
|
2020-02-10 12:46:12 -05:00
|
|
|
switch msg.String() {
|
2021-05-01 09:28:58 -04:00
|
|
|
case "q", "esc", "ctrl+c":
|
2020-05-12 17:05:16 -04:00
|
|
|
m.quitting = true
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.Quit
|
2020-02-10 12:46:12 -05:00
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-05-04 18:33:17 -04:00
|
|
|
case errMsg:
|
|
|
|
m.err = msg
|
|
|
|
return m, nil
|
|
|
|
|
2020-02-10 12:46:12 -05:00
|
|
|
default:
|
2020-05-25 19:26:40 -04:00
|
|
|
var cmd tea.Cmd
|
2020-10-31 20:58:24 -04:00
|
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
2020-05-12 17:56:30 -04:00
|
|
|
return m, cmd
|
2020-02-10 12:46:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func (m model) View() string {
|
2020-05-04 18:33:17 -04:00
|
|
|
if m.err != nil {
|
|
|
|
return m.err.Error()
|
2020-02-10 12:46:12 -05:00
|
|
|
}
|
2021-05-14 22:27:10 -04:00
|
|
|
str := fmt.Sprintf("\n\n %s Loading forever...press q to quit\n\n", m.spinner.View())
|
2020-05-12 17:05:16 -04:00
|
|
|
if m.quitting {
|
|
|
|
return str + "\n"
|
|
|
|
}
|
|
|
|
return str
|
2020-02-10 12:46:12 -05:00
|
|
|
}
|
2021-05-01 09:28:58 -04:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
p := tea.NewProgram(initialModel())
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|