forked from antonmedv/countdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (111 loc) · 1.92 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
package main
import (
"fmt"
"os"
"time"
"github.com/nsf/termbox-go"
)
const (
usage = `usage:
countdown 25s
countdown 1m50s
countdown 2h45m50s
`
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(left time.Duration) {
var exitCode int
start(left)
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(left)
}
case <-ticker.C:
left -= time.Duration(tick)
draw(left)
case <-timer.C:
break loop
}
}
termbox.Close()
if exitCode != 0 {
os.Exit(exitCode)
}
}
func main() {
if len(os.Args) != 2 {
stderr(usage)
os.Exit(2)
}
duration, err := time.ParseDuration(os.Args[1])
if err != nil {
stderr("error: invalid duration: %v\n", os.Args[1])
os.Exit(2)
}
left := duration
err = termbox.Init()
if err != nil {
panic(err)
}
queues = make(chan termbox.Event)
go func() {
for {
queues <- termbox.PollEvent()
}
}()
draw(left)
countdown(left)
}