|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 |
| - "os" |
6 |
| - "strconv" |
7 |
| - "time" |
8 |
| - |
9 |
| - "github.com/ctcpip/notifize" |
10 |
| - "github.com/kardianos/osext" |
11 |
| - "github.com/nsf/termbox-go" |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ctcpip/notifize" |
| 10 | + "github.com/kardianos/osext" |
| 11 | + "github.com/nsf/termbox-go" |
12 | 12 | )
|
13 | 13 |
|
14 | 14 | type app struct{}
|
15 | 15 |
|
16 | 16 | func (a *app) init() {
|
17 | 17 |
|
18 |
| - var k keyboard |
| 18 | + var k keyboard |
19 | 19 |
|
20 |
| - booContinue := true |
| 20 | + booContinue := true |
21 | 21 |
|
22 |
| - if len(os.Args) > 1 { |
| 22 | + if len(os.Args) > 1 { |
23 | 23 |
|
24 |
| - if a, err := strconv.ParseFloat(os.Args[1], 64); err == nil { |
25 |
| - duration = time.Millisecond * time.Duration(a*60000) |
26 |
| - } else { |
27 |
| - printHelp(os.Args[1]) |
28 |
| - booContinue = false |
29 |
| - } |
| 24 | + if a, err := strconv.ParseFloat(os.Args[1], 64); err == nil { |
| 25 | + duration = time.Millisecond * time.Duration(a*60000) |
| 26 | + } else { |
| 27 | + printHelp(os.Args[1]) |
| 28 | + booContinue = false |
| 29 | + } |
30 | 30 |
|
31 |
| - } |
| 31 | + } |
32 | 32 |
|
33 |
| - if booContinue { |
| 33 | + if booContinue { |
34 | 34 |
|
35 |
| - if duration == 0 { |
36 |
| - duration = time.Minute * 25 |
37 |
| - } |
| 35 | + if duration == 0 { |
| 36 | + duration = time.Minute * 25 |
| 37 | + } |
38 | 38 |
|
39 |
| - duration += time.Second * 1 |
| 39 | + duration += time.Second * 1 |
40 | 40 |
|
41 |
| - timer = time.NewTimer(duration) |
42 |
| - abort := make(chan bool, 1) |
| 41 | + timer = time.NewTimer(duration) |
| 42 | + abort := make(chan bool, 1) |
43 | 43 |
|
44 |
| - scr.init() |
| 44 | + scr.init() |
45 | 45 |
|
46 |
| - go countdown(duration, abort) |
| 46 | + go countdown(duration, abort) |
47 | 47 |
|
48 |
| - go func() { |
49 |
| - <-timer.C |
50 |
| - abort <- false |
51 |
| - go alertBell() |
52 |
| - go alertVisual() |
53 |
| - alertNotification() |
54 |
| - }() |
| 48 | + go func() { |
| 49 | + <-timer.C |
| 50 | + abort <- false |
| 51 | + go alertBell() |
| 52 | + go alertVisual() |
| 53 | + alertNotification() |
| 54 | + }() |
55 | 55 |
|
56 |
| - k.init() |
| 56 | + k.init() |
57 | 57 |
|
58 |
| - } |
| 58 | + } |
59 | 59 |
|
60 | 60 | }
|
61 | 61 |
|
62 | 62 | func countdown(d time.Duration, abort chan bool) {
|
63 | 63 |
|
64 |
| - var toggle bool |
65 |
| - t := time.NewTicker(time.Millisecond * 500) |
66 |
| - endTime := time.Now().Add(d) |
| 64 | + var toggle bool |
| 65 | + t := time.NewTicker(time.Millisecond * 500) |
| 66 | + endTime := time.Now().Add(d) |
67 | 67 |
|
68 |
| - drawTime(endTime) |
| 68 | + drawTime(endTime) |
69 | 69 |
|
70 |
| - for { |
| 70 | + for { |
71 | 71 |
|
72 |
| - select { |
| 72 | + select { |
73 | 73 |
|
74 |
| - case <-t.C: |
| 74 | + case <-t.C: |
75 | 75 |
|
76 |
| - if !timerPaused { |
77 |
| - clearTime() |
78 |
| - drawTime(endTime) |
79 |
| - } else { |
| 76 | + if !timerPaused { |
| 77 | + clearTime() |
| 78 | + drawTime(endTime) |
| 79 | + } else { |
80 | 80 |
|
81 |
| - if toggle { |
82 |
| - clearPauseDisplay() |
83 |
| - } else { |
84 |
| - scr.drawColoredText("PAUSED", 3, 5, termbox.ColorWhite, termbox.ColorRed) |
85 |
| - termbox.Flush() |
86 |
| - } |
| 81 | + if toggle { |
| 82 | + clearPauseDisplay() |
| 83 | + } else { |
| 84 | + scr.drawColoredText("PAUSED", 3, 5, termbox.ColorWhite, termbox.ColorRed) |
| 85 | + termbox.Flush() |
| 86 | + } |
87 | 87 |
|
88 |
| - toggle = !toggle |
| 88 | + toggle = !toggle |
89 | 89 |
|
90 |
| - } |
| 90 | + } |
91 | 91 |
|
92 |
| - case <-pause: |
| 92 | + case <-pause: |
93 | 93 |
|
94 |
| - timerPaused = !timerPaused |
| 94 | + timerPaused = !timerPaused |
95 | 95 |
|
96 |
| - if timerPaused { |
97 |
| - timer.Stop() |
98 |
| - d = endTime.Sub(time.Now()) |
99 |
| - } else { |
100 |
| - timer.Reset(d) |
101 |
| - endTime = time.Now().Add(d) |
102 |
| - clearPauseDisplay() |
103 |
| - toggle = false |
104 |
| - } |
| 96 | + if timerPaused { |
| 97 | + timer.Stop() |
| 98 | + d = endTime.Sub(time.Now()) |
| 99 | + } else { |
| 100 | + timer.Reset(d) |
| 101 | + endTime = time.Now().Add(d) |
| 102 | + clearPauseDisplay() |
| 103 | + toggle = false |
| 104 | + } |
105 | 105 |
|
106 |
| - case <-abort: |
107 |
| - return |
108 |
| - } |
| 106 | + case <-abort: |
| 107 | + return |
| 108 | + } |
109 | 109 |
|
110 |
| - } |
| 110 | + } |
111 | 111 |
|
112 | 112 | }
|
113 | 113 |
|
114 | 114 | func clearPauseDisplay() {
|
115 |
| - scr.drawText(" ", 3, 5) |
116 |
| - termbox.Flush() |
| 115 | + scr.drawText(" ", 3, 5) |
| 116 | + termbox.Flush() |
117 | 117 | }
|
118 | 118 |
|
119 | 119 | func clearTime() {
|
120 |
| - for x := 0; x < 80; x++ { |
121 |
| - termbox.SetCell(x, 3, ' ', termbox.ColorDefault, termbox.ColorDefault) |
122 |
| - } |
| 120 | + for x := 0; x < 80; x++ { |
| 121 | + termbox.SetCell(x, 3, ' ', termbox.ColorDefault, termbox.ColorDefault) |
| 122 | + } |
123 | 123 | }
|
124 | 124 |
|
125 | 125 | func drawTime(endTime time.Time) {
|
126 |
| - remainingTime := endTime.Sub(time.Now()) |
127 |
| - scr.drawText(getTimeString(remainingTime), 3, 3) |
128 |
| - termbox.Flush() |
| 126 | + remainingTime := endTime.Sub(time.Now()) |
| 127 | + scr.drawText(getTimeString(remainingTime), 3, 3) |
| 128 | + termbox.Flush() |
129 | 129 | }
|
130 | 130 |
|
131 | 131 | func alertNotification() {
|
132 | 132 |
|
133 |
| - appPath, err := osext.ExecutableFolder() |
134 |
| - if err != nil { |
135 |
| - panic(err) |
136 |
| - } |
| 133 | + appPath, err := osext.ExecutableFolder() |
| 134 | + if err != nil { |
| 135 | + panic(err) |
| 136 | + } |
137 | 137 |
|
138 |
| - notifize.Display("timezilla", "time is up!", true, appPath+"/clock.png") |
| 138 | + notifize.Display("timezilla", "time is up!", true, appPath+"/clock.png") |
139 | 139 |
|
140 | 140 | }
|
141 | 141 |
|
142 | 142 | func alertBell() {
|
143 | 143 |
|
144 |
| - // ring the terminal bell |
| 144 | + // ring the terminal bell |
145 | 145 |
|
146 |
| - fmt.Print("\a") |
| 146 | + fmt.Print("\a") |
147 | 147 |
|
148 |
| - t := time.NewTicker(time.Second * 30) |
| 148 | + t := time.NewTicker(time.Second * 30) |
149 | 149 |
|
150 |
| - for _ = range t.C { |
151 |
| - fmt.Print("\a") |
152 |
| - } |
| 150 | + for _ = range t.C { |
| 151 | + fmt.Print("\a") |
| 152 | + } |
153 | 153 |
|
154 | 154 | }
|
155 | 155 |
|
156 | 156 | func alertVisual() {
|
157 | 157 |
|
158 |
| - var b bool |
159 |
| - var c termbox.Attribute |
| 158 | + var b bool |
| 159 | + var c termbox.Attribute |
160 | 160 |
|
161 |
| - t := time.NewTicker(time.Second * 1) |
| 161 | + t := time.NewTicker(time.Second * 1) |
162 | 162 |
|
163 |
| - for _ = range t.C { |
| 163 | + for _ = range t.C { |
164 | 164 |
|
165 |
| - if b { |
166 |
| - c = termbox.ColorBlack |
167 |
| - } else { |
168 |
| - c = termbox.ColorRed |
169 |
| - } |
| 165 | + if b { |
| 166 | + c = termbox.ColorBlack |
| 167 | + } else { |
| 168 | + c = termbox.ColorRed |
| 169 | + } |
170 | 170 |
|
171 |
| - for y := 2; y < 24; y++ { |
| 171 | + for y := 2; y < 24; y++ { |
172 | 172 |
|
173 |
| - for x := 0; x < 80; x++ { |
174 |
| - termbox.SetCell(x, y, ' ', c, c) |
175 |
| - } |
| 173 | + for x := 0; x < 80; x++ { |
| 174 | + termbox.SetCell(x, y, ' ', c, c) |
| 175 | + } |
176 | 176 |
|
177 |
| - } |
| 177 | + } |
178 | 178 |
|
179 |
| - termbox.Flush() |
| 179 | + termbox.Flush() |
180 | 180 |
|
181 |
| - b = !b |
| 181 | + b = !b |
182 | 182 |
|
183 |
| - } |
| 183 | + } |
184 | 184 |
|
185 | 185 | }
|
186 | 186 |
|
|
0 commit comments