2020-01-10 16:02:04 -05:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2020-01-14 13:47:36 -05:00
|
|
|
|
"math"
|
|
|
|
|
"strings"
|
2020-01-13 17:10:23 -05:00
|
|
|
|
"time"
|
2020-01-14 13:47:36 -05:00
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-01-14 13:47:36 -05:00
|
|
|
|
"github.com/fogleman/ease"
|
2020-01-10 16:02:04 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2020-05-25 19:26:40 -04:00
|
|
|
|
p := tea.NewProgram(
|
2020-01-18 22:18:19 -05:00
|
|
|
|
initialize,
|
2020-01-13 19:07:04 -05:00
|
|
|
|
update,
|
|
|
|
|
view,
|
|
|
|
|
)
|
2020-01-11 10:45:15 -05:00
|
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
|
fmt.Println("could not start program:", err)
|
|
|
|
|
}
|
2020-01-10 16:02:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
// MESSAGES
|
2020-05-05 14:26:06 -04:00
|
|
|
|
|
2020-05-12 17:56:30 -04:00
|
|
|
|
type tickMsg struct{}
|
|
|
|
|
type frameMsg struct{}
|
2020-05-05 14:26:06 -04:00
|
|
|
|
|
|
|
|
|
// MODEL
|
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
type model struct {
|
2020-05-05 14:26:06 -04:00
|
|
|
|
Choice int
|
|
|
|
|
Chosen bool
|
|
|
|
|
Ticks int
|
|
|
|
|
Frames int
|
|
|
|
|
Progress float64
|
|
|
|
|
Loaded bool
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
|
func initialize() (tea.Model, tea.Cmd) {
|
2020-07-30 12:32:24 -04:00
|
|
|
|
return model{0, false, 10, 0, 0, false}, tick()
|
2020-01-18 22:18:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 17:56:30 -04:00
|
|
|
|
// CMDS
|
2020-01-14 13:47:36 -05:00
|
|
|
|
|
2020-06-05 14:12:02 -04:00
|
|
|
|
func tick() tea.Cmd {
|
|
|
|
|
return tea.Tick(time.Second, func(time.Time) tea.Msg {
|
|
|
|
|
return tickMsg{}
|
|
|
|
|
})
|
2020-05-12 17:56:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 14:12:02 -04:00
|
|
|
|
func frame() tea.Cmd {
|
|
|
|
|
return tea.Tick(time.Second/60, func(time.Time) tea.Msg {
|
|
|
|
|
return frameMsg{}
|
|
|
|
|
})
|
2020-01-25 21:27:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 14:26:06 -04:00
|
|
|
|
// UPDATE
|
2020-01-14 13:47:36 -05:00
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
// Main update function. This just hands off the message and model to the
|
|
|
|
|
// approprate update loop based on the current state.
|
|
|
|
|
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
|
|
|
|
|
m, _ := mdl.(model)
|
2020-01-10 16:02:04 -05:00
|
|
|
|
|
2020-01-14 13:47:36 -05:00
|
|
|
|
if !m.Chosen {
|
|
|
|
|
return updateChoices(msg, m)
|
|
|
|
|
}
|
|
|
|
|
return updateChosen(msg, m)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
// Update loop for the first view where you're choosing a task
|
|
|
|
|
func updateChoices(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
|
2020-01-10 16:02:04 -05:00
|
|
|
|
switch msg := msg.(type) {
|
2020-01-13 17:10:23 -05:00
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
|
case tea.KeyMsg:
|
2020-01-17 20:46:34 -05:00
|
|
|
|
switch msg.String() {
|
2020-01-10 16:02:04 -05:00
|
|
|
|
case "j":
|
2020-01-10 23:12:25 -05:00
|
|
|
|
fallthrough
|
|
|
|
|
case "down":
|
2020-01-13 17:10:23 -05:00
|
|
|
|
m.Choice += 1
|
|
|
|
|
if m.Choice > 3 {
|
|
|
|
|
m.Choice = 3
|
2020-01-10 16:02:04 -05:00
|
|
|
|
}
|
|
|
|
|
case "k":
|
2020-01-10 23:12:25 -05:00
|
|
|
|
fallthrough
|
|
|
|
|
case "up":
|
2020-01-13 17:10:23 -05:00
|
|
|
|
m.Choice -= 1
|
|
|
|
|
if m.Choice < 0 {
|
|
|
|
|
m.Choice = 0
|
2020-01-10 16:02:04 -05:00
|
|
|
|
}
|
2020-01-14 13:47:36 -05:00
|
|
|
|
case "enter":
|
|
|
|
|
m.Chosen = true
|
2020-06-05 14:12:02 -04:00
|
|
|
|
return m, frame()
|
2020-01-10 16:02:04 -05:00
|
|
|
|
case "q":
|
2020-01-10 23:46:46 -05:00
|
|
|
|
fallthrough
|
|
|
|
|
case "esc":
|
2020-01-11 11:15:01 -05:00
|
|
|
|
fallthrough
|
2020-01-26 16:46:30 -05:00
|
|
|
|
case "ctrl+c":
|
2020-05-25 19:26:40 -04:00
|
|
|
|
return m, tea.Quit
|
2020-01-10 16:02:04 -05:00
|
|
|
|
}
|
2020-01-13 17:10:23 -05:00
|
|
|
|
|
2020-01-14 13:47:36 -05:00
|
|
|
|
case tickMsg:
|
2020-01-13 19:07:04 -05:00
|
|
|
|
if m.Ticks == 0 {
|
2020-05-25 19:26:40 -04:00
|
|
|
|
return m, tea.Quit
|
2020-01-13 19:07:04 -05:00
|
|
|
|
}
|
|
|
|
|
m.Ticks -= 1
|
2020-06-05 14:12:02 -04:00
|
|
|
|
return m, tick()
|
2020-01-10 16:02:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 14:12:02 -04:00
|
|
|
|
return m, nil
|
2020-01-10 16:02:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
// Update loop for the second view after a choice has been made
|
|
|
|
|
func updateChosen(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
|
2020-01-14 13:47:36 -05:00
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
|
case tea.KeyMsg:
|
2020-01-17 20:46:34 -05:00
|
|
|
|
switch msg.String() {
|
2020-01-14 13:47:36 -05:00
|
|
|
|
case "q":
|
|
|
|
|
fallthrough
|
|
|
|
|
case "esc":
|
|
|
|
|
fallthrough
|
2020-01-26 16:46:30 -05:00
|
|
|
|
case "ctrl+c":
|
2020-05-25 19:26:40 -04:00
|
|
|
|
return m, tea.Quit
|
2020-01-14 13:47:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case frameMsg:
|
2020-01-14 16:24:01 -05:00
|
|
|
|
if !m.Loaded {
|
|
|
|
|
m.Frames += 1
|
2020-06-05 14:12:02 -04:00
|
|
|
|
m.Progress = ease.OutBounce(float64(m.Frames) / float64(100))
|
2020-01-14 16:24:01 -05:00
|
|
|
|
if m.Progress >= 1 {
|
|
|
|
|
m.Progress = 1
|
|
|
|
|
m.Loaded = true
|
|
|
|
|
m.Ticks = 3
|
2020-06-05 14:12:02 -04:00
|
|
|
|
return m, tick()
|
2020-01-14 16:24:01 -05:00
|
|
|
|
}
|
2020-06-05 14:12:02 -04:00
|
|
|
|
return m, frame()
|
2020-01-14 13:47:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 16:24:01 -05:00
|
|
|
|
case tickMsg:
|
|
|
|
|
if m.Loaded {
|
|
|
|
|
if m.Ticks == 0 {
|
2020-05-25 19:26:40 -04:00
|
|
|
|
return m, tea.Quit
|
2020-01-14 16:24:01 -05:00
|
|
|
|
}
|
|
|
|
|
m.Ticks -= 1
|
2020-06-05 14:12:02 -04:00
|
|
|
|
return m, tick()
|
2020-01-14 16:24:01 -05:00
|
|
|
|
}
|
2020-01-14 13:47:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 14:12:02 -04:00
|
|
|
|
return m, nil
|
2020-01-14 13:47:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-05 14:26:06 -04:00
|
|
|
|
// VIEW
|
2020-01-14 13:47:36 -05:00
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
// The main view, which just calls the approprate sub-view
|
|
|
|
|
func view(mdl tea.Model) string {
|
|
|
|
|
m, _ := mdl.(model)
|
2020-01-14 13:47:36 -05:00
|
|
|
|
if !m.Chosen {
|
2020-05-12 17:05:16 -04:00
|
|
|
|
return choicesView(m) + "\n"
|
2020-01-14 13:47:36 -05:00
|
|
|
|
}
|
2020-05-12 17:05:16 -04:00
|
|
|
|
return chosenView(m) + "\n"
|
2020-01-13 17:10:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
// The first view, where you're choosing a task
|
|
|
|
|
func choicesView(m model) string {
|
2020-01-13 17:10:23 -05:00
|
|
|
|
c := m.Choice
|
2020-01-10 16:02:04 -05:00
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
tpl := "What to do today?\n\n"
|
|
|
|
|
tpl += "%s\n\n"
|
|
|
|
|
tpl += "Program quits in %d seconds\n\n"
|
|
|
|
|
tpl += "(press j/k or up/down to select, enter to choose, and q or esc to quit)`"
|
|
|
|
|
|
2020-01-10 16:02:04 -05:00
|
|
|
|
choices := fmt.Sprintf(
|
|
|
|
|
"%s\n%s\n%s\n%s",
|
2020-01-13 17:10:23 -05:00
|
|
|
|
checkbox("Plant carrots", c == 0),
|
|
|
|
|
checkbox("Go to the market", c == 1),
|
|
|
|
|
checkbox("Read something", c == 2),
|
|
|
|
|
checkbox("See friends", c == 3),
|
2020-01-10 16:02:04 -05:00
|
|
|
|
)
|
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
return fmt.Sprintf(tpl, choices, m.Ticks)
|
2020-01-14 13:47:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 12:32:24 -04:00
|
|
|
|
// The second view, after a task has been chosen
|
|
|
|
|
func chosenView(m model) string {
|
2020-01-14 13:47:36 -05:00
|
|
|
|
var msg string
|
|
|
|
|
|
|
|
|
|
switch m.Choice {
|
|
|
|
|
case 0:
|
|
|
|
|
msg = "Carrot planting?\n\nCool, we'll need libgarden and vegeutils..."
|
|
|
|
|
case 1:
|
|
|
|
|
msg = "A trip to the market?\n\nOkay, then we should install marketkit and libshopping..."
|
|
|
|
|
case 2:
|
2020-01-14 16:24:01 -05:00
|
|
|
|
msg = "Reading time?\n\nOkay, cool, then we’ll need a library. Yes, an actual library."
|
2020-01-14 13:47:36 -05:00
|
|
|
|
default:
|
|
|
|
|
msg = "It’s always good to see friends.\n\nFetching social-skills and conversationutils..."
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 16:24:01 -05:00
|
|
|
|
label := "Downloading..."
|
|
|
|
|
if m.Loaded {
|
|
|
|
|
label = fmt.Sprintf("Downloaded. Exiting in %d...", m.Ticks)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-15 22:41:45 -05:00
|
|
|
|
return msg + "\n\n " + label + "\n" + progressbar(80, m.Progress) + "%"
|
2020-01-10 16:02:04 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkbox(label string, checked bool) string {
|
|
|
|
|
check := " "
|
|
|
|
|
if checked {
|
|
|
|
|
check = "x"
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("[%s] %s", check, label)
|
|
|
|
|
}
|
2020-01-14 13:47:36 -05:00
|
|
|
|
|
|
|
|
|
func progressbar(width int, percent float64) string {
|
|
|
|
|
metaChars := 7
|
|
|
|
|
w := float64(width - metaChars)
|
|
|
|
|
fullSize := int(math.Round(w * percent))
|
|
|
|
|
emptySize := int(w) - fullSize
|
|
|
|
|
fullCells := strings.Repeat("#", fullSize)
|
|
|
|
|
emptyCells := strings.Repeat(".", emptySize)
|
|
|
|
|
return fmt.Sprintf("|%s%s| %3.0f", fullCells, emptyCells, math.Round(percent*100))
|
|
|
|
|
}
|