Make tui-daemon-combo demo look a little more enticing

This commit is contained in:
Christian Rocha 2021-03-11 15:03:52 -05:00
parent a87e82a3e8
commit 01878b5650
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 28 additions and 10 deletions

View File

@ -13,6 +13,11 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
"github.com/muesli/reflow/indent" "github.com/muesli/reflow/indent"
"github.com/muesli/termenv"
)
var (
color = termenv.ColorProfile().Color
) )
func main() { func main() {
@ -48,9 +53,14 @@ func main() {
} }
} }
type result struct {
duration time.Duration
emoji string
}
type model struct { type model struct {
spinner spinner.Model spinner spinner.Model
results []time.Duration results []result
quitting bool quitting bool
} }
@ -59,7 +69,7 @@ func newModel() model {
return model{ return model{
spinner: spinner.NewModel(), spinner: spinner.NewModel(),
results: make([]time.Duration, showLastResults), results: make([]result, showLastResults),
} }
} }
@ -82,8 +92,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd return m, cmd
case processFinishedMsg: case processFinishedMsg:
d := time.Duration(msg) d := time.Duration(msg)
log.Printf("Finished job in %s", d) res := result{emoji: randomEmoji(), duration: d}
m.results = append(m.results[1:], d) log.Printf("%s Job finished in %s", res.emoji, res.duration)
m.results = append(m.results[1:], res)
return m, runPretendProcess return m, runPretendProcess
default: default:
return m, nil return m, nil
@ -91,17 +102,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m model) View() string { func (m model) View() string {
s := "\n" + m.spinner.View() + " Doing some work...\n\n" s := "\n" +
termenv.String(m.spinner.View()).Foreground(color("206")).String() +
" Doing some work...\n\n"
for _, dur := range m.results { for _, res := range m.results {
if dur == 0 { if res.duration == 0 {
s += ".....................\n" s += "........................\n"
} else { } else {
s += fmt.Sprintf("Job finished in %s\n", dur) s += fmt.Sprintf("%s Job finished in %s\n", res.emoji, res.duration)
} }
} }
s += "\nPress any key to exit\n" s += termenv.String("\nPress any key to exit\n").Foreground(color("240")).String()
if m.quitting { if m.quitting {
s += "\n" s += "\n"
@ -119,3 +132,8 @@ func runPretendProcess() tea.Msg {
time.Sleep(pause) time.Sleep(pause)
return processFinishedMsg(pause) return processFinishedMsg(pause)
} }
func randomEmoji() string {
emojis := []rune("🍦🧋🍡🤠👾😭🦊🐯🦆🥨🎏🍔🍒🍥🎮📦🦁🐶🐸🍕🥐🧲🚒🥇🏆🌽")
return string(emojis[rand.Intn(len(emojis))])
}