-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
main.go
225 lines (187 loc) · 4.35 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/nsf/termbox-go"
)
const (
usage = `
countdown [-up] [-say] [-title text] <duration>
Usage
countdown 25s
countdown -title "Coffee Break" 14:15
countdown 02:15PM
Flags
`
tick = time.Second
inputDelayMS = 500 * time.Millisecond
)
var (
timer *time.Timer
ticker *time.Ticker
queues chan termbox.Event
w, h int
inputStartTime time.Time
isPaused bool
)
func main() {
countUp := flag.Bool("up", false, "count up from zero")
sayTheTime := flag.Bool("say", false, "announce the time left")
title := flag.String("title", "", "display title below the countdown")
flag.Parse()
args := flag.Args()
if len(args) != 1 {
stderr(usage)
flag.PrintDefaults()
os.Exit(2)
}
timeLeft, err := parseTime(args[0])
if err != nil {
timeLeft, err = time.ParseDuration(args[0])
if err != nil {
stderr("error: invalid duration or time: %v\n", args[0])
os.Exit(2)
}
}
err = termbox.Init()
if err != nil {
panic(err)
}
queues = make(chan termbox.Event)
go func() {
for {
queues <- termbox.PollEvent()
}
}()
countdown(timeLeft, *countUp, *sayTheTime, *title)
}
func start(d time.Duration) {
timer = time.NewTimer(d)
ticker = time.NewTicker(tick)
}
func stop() {
timer.Stop()
ticker.Stop()
}
func durationToDraw(timeLeft, totalDuration time.Duration, countUp bool) time.Duration {
if countUp {
return totalDuration - timeLeft
}
return timeLeft
}
func countdown(totalDuration time.Duration, countUp bool, sayTheTime bool, title string) {
timeLeft := totalDuration
var exitCode int
isPaused = false
w, h = termbox.Size()
start(timeLeft)
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h, title)
if sayTheTime {
go say(timeLeft)
}
loop:
for {
select {
case ev := <-queues:
if ev.Key == termbox.KeyEsc || ev.Key == termbox.KeyCtrlC {
exitCode = 1
break loop
}
if pressTime := time.Now(); ev.Key == termbox.KeySpace && pressTime.Sub(inputStartTime) > inputDelayMS {
if isPaused {
start(timeLeft)
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h, title)
} else {
stop()
drawPause(w, h)
}
isPaused = !isPaused
inputStartTime = time.Now()
}
if ev.Type == termbox.EventResize {
w, h = termbox.Size()
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h, title)
if isPaused {
drawPause(w, h)
}
}
case <-ticker.C:
timeLeft -= tick
draw(durationToDraw(timeLeft, totalDuration, countUp), w, h, title)
if sayTheTime {
go say(timeLeft)
}
case <-timer.C:
break loop
}
}
termbox.Close()
if exitCode != 0 {
os.Exit(exitCode)
}
}
func draw(d time.Duration, w int, h int, title string) {
clear()
str := format(d)
digits := toText(str)
startX, startY := w/2-digits.width()/2, h/2-digits.height()/2
x, y := startX, startY
for _, s := range digits {
echo(s, x, y)
x += s.width()
}
// Draw title if provided
if title != "" {
titleY := startY + digits.height() + 2 // 2 lines below the numbers
titleX := w/2 - len(title)/2 // Center the title
for i, c := range title {
termbox.SetCell(titleX+i, titleY, c, termbox.ColorWhite, termbox.ColorDefault)
}
}
flush()
}
func drawPause(w int, h int) {
startX := w/2 - pausedText.width()/2
startY := h * 3 / 4
echo(pausedText, startX, startY)
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 say(d time.Duration) {
if d.Seconds() <= 10 {
cmd := exec.Command("say", fmt.Sprintf("%v", d.Seconds()))
_ = cmd.Run()
}
}
func parseTime(date string) (time.Duration, error) {
targetTime, err := time.Parse(time.Kitchen, strings.ToUpper(date))
if err != nil {
targetTime, err = time.Parse("15:04", 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
}