-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (115 loc) · 2.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mbndr/figlet4go"
"github.com/Natouche68/go-tempo/styles"
timeutils "github.com/Natouche68/go-tempo/time-utils"
)
type Model struct {
timeLeft int
workTime int
pauseTime int
currentPhase string
ticking bool
}
type TickMsg time.Time
func doTick() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return TickMsg(t)
})
}
func (m Model) Init() tea.Cmd {
return doTick()
}
var ascii = figlet4go.NewAsciiRender()
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case " ":
m.ticking = !m.ticking
case "r":
if m.currentPhase == "work" {
m.timeLeft = m.workTime
} else if m.currentPhase == "pause" {
m.timeLeft = m.pauseTime
}
}
case TickMsg:
if m.ticking {
m.timeLeft--
if m.timeLeft < 0 {
if m.currentPhase == "work" {
m.currentPhase = "pause"
m.timeLeft = m.pauseTime
} else if m.currentPhase == "pause" {
m.currentPhase = "work"
m.timeLeft = m.workTime
}
}
}
return m, doTick()
}
return m, nil
}
func (m Model) View() string {
var s string
title := styles.TitleStyle.Render("Go Tempo")
clock, err := ascii.Render(timeutils.SecondToClock(m.timeLeft))
if err != nil {
return styles.ErrorStyle.Render("An error occured : \n" + err.Error())
}
clock = strings.TrimSuffix(clock, "\n")
var label string
switch m.currentPhase {
case "work":
if m.ticking {
label = styles.LabelStyle.
Copy().
Foreground(styles.WorkColor).
Render("- Work -")
} else {
label = styles.LabelStyle.
Copy().
Foreground(styles.WorkColor).
Italic(true).
Render("- Work (paused) -")
}
case "pause":
if m.ticking {
label = styles.LabelStyle.
Copy().
Foreground(styles.PauseColor).
Render("- Pause -")
} else {
label = styles.LabelStyle.
Copy().
Foreground(styles.PauseColor).
Italic(true).
Render("- Pause (paused) -")
}
}
help := styles.HelpStyle.Render("q : quit • space : pause/resume • r : reset")
s = lipgloss.JoinVertical(lipgloss.Center, title, styles.ClockStyle.Render(clock), label, help)
return s
}
func main() {
p := tea.NewProgram(Model{
timeLeft: 25 * 60,
workTime: 25 * 60,
pauseTime: 5 * 60,
currentPhase: "work",
ticking: true,
}, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Println(styles.ErrorStyle.Render("There's been an error : " + err.Error()))
os.Exit(1)
}
}