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"
|
2020-02-10 12:46:12 -05:00
|
|
|
"github.com/muesli/termenv"
|
|
|
|
)
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
var term = termenv.ColorProfile()
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-10-15 19:48:42 -04:00
|
|
|
p := tea.NewProgram(initialModel())
|
2020-02-10 12:46:12 -05:00
|
|
|
if err := p.Start(); err != nil {
|
2020-05-12 17:05:16 -04:00
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
2020-02-10 12:46:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
func initialModel() model {
|
2020-05-12 17:56:30 -04:00
|
|
|
s := spinner.NewModel()
|
2020-07-14 18:31:35 -04:00
|
|
|
s.Frames = spinner.Dot
|
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 {
|
2020-10-31 20:58:24 -04:00
|
|
|
return 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() {
|
|
|
|
case "q":
|
|
|
|
fallthrough
|
|
|
|
case "esc":
|
|
|
|
fallthrough
|
|
|
|
case "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
|
|
|
}
|
|
|
|
s := termenv.
|
2020-10-31 20:58:24 -04:00
|
|
|
String(m.spinner.View()).
|
2020-10-15 19:48:42 -04:00
|
|
|
Foreground(term.Color("205")).
|
2020-02-10 12:46:12 -05:00
|
|
|
String()
|
2020-05-12 17:05:16 -04:00
|
|
|
str := fmt.Sprintf("\n\n %s Loading forever...press q to quit\n\n", s)
|
|
|
|
if m.quitting {
|
|
|
|
return str + "\n"
|
|
|
|
}
|
|
|
|
return str
|
2020-02-10 12:46:12 -05:00
|
|
|
}
|