-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.go
173 lines (139 loc) · 2.74 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
"os"
"time"
"github.com/nsf/termbox-go"
)
const (
usage = `
countdown
Usage:
countdown 25s
countdown 1m50s
countdown 3h40m50s
Options:
-up : count up from zero
`
tick = time.Second
)
var (
timer *time.Timer
ticker *time.Ticker
queues chan termbox.Event
startDone bool
startX, startY int
)
func draw(d time.Duration) {
w, h := termbox.Size()
clear()
str := format(d)
text := toText(str)
if !startDone {
startDone = true
startX, startY = w/2-text.width()/2, h/2-text.height()/2
}
x, y := startX, startY
for _, s := range text {
echo(s, x, y)
x += s.width()
}
flush()
}
func format(d time.Duration) string {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
if h < 1 {
return fmt.Sprintf("%02d:%02d", m, s)
}
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}
func start(d time.Duration) {
timer = time.NewTimer(d)
ticker = time.NewTicker(tick)
}
func stop() {
timer.Stop()
ticker.Stop()
}
func countdown(timeLeft time.Duration, countUp bool) {
var exitCode int
start(timeLeft)
if countUp {
timeLeft = 0
}
draw(timeLeft)
loop:
for {
select {
case ev := <-queues:
if ev.Type == termbox.EventKey && (ev.Key == termbox.KeyEsc || ev.Key == termbox.KeyCtrlC) {
exitCode = 1
break loop
}
if ev.Ch == 'p' || ev.Ch == 'P' {
stop()
}
if ev.Ch == 'c' || ev.Ch == 'C' {
start(timeLeft)
}
case <-ticker.C:
if countUp {
timeLeft += tick
} else {
timeLeft -= tick
}
draw(timeLeft)
case <-timer.C:
break loop
}
}
termbox.Close()
if exitCode != 0 {
os.Exit(exitCode)
}
}
func getKitchenTimeDuration(date string) (time.Duration, error) {
targetTime, err := time.Parse(time.Kitchen, date)
if err != nil {
return time.Duration(0), err
}
now := time.Now()
originTime := time.Date(0, time.January, 1, now.Hour(), now.Minute(), now.Second(), 0, time.UTC)
// the time of day has already passed, so target tomorrow
if targetTime.Before(originTime) {
targetTime = targetTime.AddDate(0, 0, 1)
}
duration := targetTime.Sub(originTime)
return duration, err
}
func main() {
if len(os.Args) < 2 || len(os.Args) > 3 {
stderr(usage)
os.Exit(2)
}
timeLeft, err := getKitchenTimeDuration(os.Args[1])
if err != nil {
timeLeft, err = time.ParseDuration(os.Args[1])
if err != nil {
stderr("error: invalid duration or kitchen time: %v\n", os.Args[1])
os.Exit(2)
}
}
err = termbox.Init()
if err != nil {
panic(err)
}
queues = make(chan termbox.Event)
go func() {
for {
queues <- termbox.PollEvent()
}
}()
countUp := len(os.Args) == 3 && os.Args[2] == "-up"
countdown(timeLeft, countUp)
}