-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtermbox.go
109 lines (94 loc) · 2.3 KB
/
termbox.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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/carlmjohnson/pomodoro/display"
"github.com/nsf/termbox-go"
)
func fullscreenCountdown(start, finish time.Time, formatter func(time.Duration) string) {
err := termbox.Init()
if err != nil {
fmt.Fprintln(os.Stderr, "Couldn't open display:", err)
os.Exit(2)
}
defer termbox.Close()
// Leaks a goroutine
ticker := time.Tick(40 * time.Millisecond)
quit := make(chan struct{})
// Leaks if not quit
go func() {
defer close(quit)
for {
e := termbox.PollEvent()
// Quit on any of the common keys for quitting
if strings.ContainsRune("CcDdQqXx", e.Ch) ||
e.Key == termbox.KeyCtrlC ||
e.Key == termbox.KeyCtrlD ||
e.Key == termbox.KeyCtrlQ ||
e.Key == termbox.KeyCtrlX {
return
}
}
}()
for render(start, finish, formatter) {
select {
case <-ticker:
case <-quit:
termbox.Close()
os.Exit(1)
return
}
}
}
func render(start, finish time.Time, formatter func(time.Duration) string) bool {
now := time.Now()
remaining := -now.Sub(finish)
if remaining < 0 {
return false
}
const timeFmt = "3:04:05pm"
screenW, screenH := termbox.Size()
centerX := screenW / 2
centerY := screenH / 2
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
startStr := start.Format(timeFmt)
display.Point{
0, 0,
termbox.ColorBlue, termbox.ColorDefault,
}.Str("Start")
display.Point{
0, 1,
termbox.ColorWhite, termbox.ColorDefault,
}.Str(startStr)
nowStr := now.Format(timeFmt)
display.Point{
centerX - (len("Now") / 2), 0,
termbox.ColorBlue, termbox.ColorDefault,
}.Str("Now")
display.Point{
centerX - (len(nowStr) / 2), 1,
termbox.ColorWhite, termbox.ColorDefault,
}.Str(nowStr)
finishStr := finish.Format(timeFmt)
display.Point{
screenW - len("Finish"), 0,
termbox.ColorBlue, termbox.ColorDefault,
}.Str("Finish")
display.Point{
screenW - len(finishStr), 1,
termbox.ColorWhite, termbox.ColorDefault,
}.Str(finishStr)
remainingStr := formatter(remaining)
display.Point{
centerX - (len(remainingStr) * (display.BigCharWidth + 1) / 2), centerY,
termbox.ColorBlue, termbox.ColorDefault,
}.BigStr(remainingStr)
display.Point{
0, centerY + 6,
termbox.ColorBlue, termbox.ColorWhite,
}.ProgressBar(screenW, int(start.Sub(now)), int(start.Sub(finish)))
termbox.Flush()
return true
}