poms/timer/fancy-timer.go
2024-05-19 17:32:04 -07:00

123 lines
3.3 KiB
Go

package timer
import (
"strconv"
// "os"
// "time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)
var (
colon = []string{" ", " ", " __ ", " |__| ", " __ ", " |__| ", " ", " "}
one = []string{" _____ ", " | _ | ", " |.| | ", " `-|. | ", " |: | ", " |::.| ", " `---' "}
two = []string{" _______ ", " | |", " |___| |", " / ___/ ", " |: 1 \\ ", " |::.. . |", " '-------'"}
three = []string{" _______ ", " | _ |", " |___| |", " _(__ |", " |: 1 |", " |::.. . |", " '-------'"}
four = []string{" ___ ___ ", " | Y |", " | | |", " |____ |", " |: |", " |::.|", " '---'"}
five = []string{" _______ ", " | _ |", " | 1___|", " |____ |", " |: 1 |", " |::.. . |", " '-------'"}
six = []string{" _______ ", " | _ |", " | 1___|", " |. ", " |: 1 |", " |::.. . |", " '-------'"}
seven = []string{" _______ ", " | _ |", " |___| |", " / / ", " | | ", " | | ", " '---' "}
eight = []string{" _______ ", " | _ |", " |. | |", " |. _ |", " |: 1 |", " |::.. . |", " '-------'"}
nine = []string{" _______ ", " | _ |", " | | |", " \\___ |", " |: 1 |", " |::.. . |", " '-------'"}
zero = []string{" _______ ", " | _ |", " |. | |", " |. | |", " |: 1 |", " |::.. . |", " '-------'"}
numbers = [][]string{zero, one, two, three, four, five, six, seven, eight, nine}
)
type model struct {
timer Model
keymap keymap
help help.Model
}
type keymap struct {
start key.Binding
stop key.Binding
reset key.Binding
quit key.Binding
}
func (m model) Init() tea.Cmd {
return m.timer.Init()
}
func printIntegers(num float64) string {
var s string
digits := strconv.FormatFloat(num, 'f', -1, 64)
for j := 0; j < 7; j++ {
for _, val := range digits {
n, _ := strconv.Atoi(string(val))
s += numbers[n][j] + " "
}
s += "\n"
}
return s
}
func PrintTime(min float64, sec float64) string {
var s string
mins := strconv.FormatFloat(min, 'f', 0, 64)
secs := strconv.FormatFloat(sec, 'f', 0, 64)
time := mins+secs
for j := 0; j < 7; j++ {
for i, val := range time {
if i == 2 {
s += colon[j]
}
n, _ := strconv.Atoi(string(val))
s += numbers[n][j]
}
s += "\n"
}
return s
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case TickMsg:
var cmd tea.Cmd
m.timer, cmd = m.timer.Update(msg)
return m, cmd
case StartStopMsg:
var cmd tea.Cmd
m.timer, cmd = m.timer.Update(msg)
m.keymap.stop.SetEnabled(m.timer.Running())
m.keymap.start.SetEnabled(!m.timer.Running())
return m, cmd
case TimeoutMsg:
// m.quitting = true
return m, tea.Quit
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.quit):
// m.quitting = true
return m, tea.Quit
case key.Matches(msg, m.keymap.reset):
//m.timer.Timeout = timeout
case key.Matches(msg, m.keymap.start, m.keymap.stop):
return m, m.timer.Toggle()
}
}
return m, nil
}
func (m model) helpView() string {
return "\n" + m.help.ShortHelpView([]key.Binding{
m.keymap.start,
m.keymap.stop,
m.keymap.reset,
m.keymap.quit,
})
}
func (m model) View() string {
return PrintTime(m.timer.Minutes(), m.timer.Seconds())
}