2021-02-09 13:33:25 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/spinner"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2021-05-14 22:27:10 -04:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2021-02-09 13:33:25 -05:00
|
|
|
"github.com/mattn/go-isatty"
|
|
|
|
"github.com/muesli/reflow/indent"
|
2021-03-11 15:03:52 -05:00
|
|
|
)
|
|
|
|
|
2021-05-14 22:27:10 -04:00
|
|
|
var helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
|
|
|
var (
|
|
|
|
daemonMode bool
|
|
|
|
showHelp bool
|
|
|
|
opts []tea.ProgramOption
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.BoolVar(&daemonMode, "d", false, "run as a daemon")
|
|
|
|
flag.BoolVar(&showHelp, "h", false, "show help")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if showHelp {
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if daemonMode || !isatty.IsTerminal(os.Stdout.Fd()) {
|
|
|
|
// If we're in daemon mode don't render the TUI
|
|
|
|
opts = []tea.ProgramOption{tea.WithoutRenderer()}
|
|
|
|
} else {
|
|
|
|
// If we're in TUI mode, discard log output
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := tea.NewProgram(newModel(), opts...)
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
fmt.Println("Error starting Bubble Tea program:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:03:52 -05:00
|
|
|
type result struct {
|
|
|
|
duration time.Duration
|
|
|
|
emoji string
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
type model struct {
|
|
|
|
spinner spinner.Model
|
2021-03-11 15:03:52 -05:00
|
|
|
results []result
|
2021-02-09 13:33:25 -05:00
|
|
|
quitting bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newModel() model {
|
|
|
|
const showLastResults = 5
|
|
|
|
|
2022-01-10 20:42:10 -05:00
|
|
|
sp := spinner.New()
|
2021-05-14 22:27:10 -04:00
|
|
|
sp.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("206"))
|
|
|
|
|
2021-02-09 13:33:25 -05:00
|
|
|
return model{
|
2021-05-14 22:27:10 -04:00
|
|
|
spinner: sp,
|
2021-03-11 15:03:52 -05:00
|
|
|
results: make([]result, showLastResults),
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
log.Println("Starting work...")
|
|
|
|
return tea.Batch(
|
|
|
|
spinner.Tick,
|
|
|
|
runPretendProcess,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
m.quitting = true
|
|
|
|
return m, tea.Quit
|
|
|
|
case spinner.TickMsg:
|
|
|
|
var cmd tea.Cmd
|
|
|
|
m.spinner, cmd = m.spinner.Update(msg)
|
|
|
|
return m, cmd
|
|
|
|
case processFinishedMsg:
|
|
|
|
d := time.Duration(msg)
|
2021-03-11 15:03:52 -05:00
|
|
|
res := result{emoji: randomEmoji(), duration: d}
|
|
|
|
log.Printf("%s Job finished in %s", res.emoji, res.duration)
|
|
|
|
m.results = append(m.results[1:], res)
|
2021-02-09 13:33:25 -05:00
|
|
|
return m, runPretendProcess
|
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) View() string {
|
2021-03-11 15:03:52 -05:00
|
|
|
s := "\n" +
|
2021-05-14 22:27:10 -04:00
|
|
|
m.spinner.View() + " Doing some work...\n\n"
|
2021-02-09 13:33:25 -05:00
|
|
|
|
2021-03-11 15:03:52 -05:00
|
|
|
for _, res := range m.results {
|
|
|
|
if res.duration == 0 {
|
|
|
|
s += "........................\n"
|
2021-02-09 13:33:25 -05:00
|
|
|
} else {
|
2021-03-11 15:03:52 -05:00
|
|
|
s += fmt.Sprintf("%s Job finished in %s\n", res.emoji, res.duration)
|
2021-02-09 13:33:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 22:27:10 -04:00
|
|
|
s += helpStyle("\nPress any key to exit\n")
|
2021-02-09 13:33:25 -05:00
|
|
|
|
|
|
|
if m.quitting {
|
|
|
|
s += "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return indent.String(s, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// processFinishedMsg is send when a pretend process completes.
|
|
|
|
type processFinishedMsg time.Duration
|
|
|
|
|
|
|
|
// pretendProcess simulates a long-running process.
|
|
|
|
func runPretendProcess() tea.Msg {
|
|
|
|
pause := time.Duration(rand.Int63n(899)+100) * time.Millisecond
|
|
|
|
time.Sleep(pause)
|
|
|
|
return processFinishedMsg(pause)
|
|
|
|
}
|
2021-03-11 15:03:52 -05:00
|
|
|
|
|
|
|
func randomEmoji() string {
|
|
|
|
emojis := []rune("🍦🧋🍡🤠👾😭🦊🐯🦆🥨🎏🍔🍒🍥🎮📦🦁🐶🐸🍕🥐🧲🚒🥇🏆🌽")
|
|
|
|
return string(emojis[rand.Intn(len(emojis))])
|
|
|
|
}
|