forked from claudiodangelis/i3-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
257 lines (234 loc) · 5.73 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"time"
)
var alarmCommandFlag = flag.String("alarm-command", "",
"command to be executed when the alarm fires")
var debugFlag = flag.Bool("debug", false, "print debug messages")
var colorsFlag = flag.Bool("colors", false, "colorized timer")
var autostartFlag = flag.Bool("autostart", false, "start timer automatically")
var recurringFlag = flag.Bool("recurring", false, "restart timer after alarm")
var durationFlag = flag.Int("duration", 5, "default duration in minutes")
func debug(args ...interface{}) {
if *debugFlag {
log.Println(args...)
}
}
// Timer holds data of the current timer
type Timer struct {
Duration time.Duration `json:"duration"`
StartTime time.Time `json:"startTime"`
ShowElapsed bool `json:"showElapsed"`
}
// Remaining returns the remaining duration
func (t *Timer) Remaining() time.Duration {
var elapsed time.Duration
if t.IsRunning() {
elapsed = time.Since(t.StartTime)
}
return (t.Duration - elapsed).Truncate(time.Duration(time.Second))
}
// IsRunning check if the timer is running
func (t *Timer) IsRunning() bool {
return !time.Time.IsZero(t.StartTime)
}
// IsNotRunning check if the timer is not running
func (t *Timer) IsNotRunning() bool {
return !t.IsRunning()
}
// AddMinute adds one minute to the duration of the timer
func (t *Timer) AddMinute() {
t.Duration += time.Duration(time.Minute)
t.Save()
}
// SubtractMinute removes one minute from the duration of the timer
func (t *Timer) SubtractMinute() {
if t.Duration > time.Duration(0) {
t.Duration -= time.Duration(time.Minute)
if t.Duration < 0 {
t.Duration = 0
}
t.Save()
}
}
// Alarm executes what has been passed as `-alarm-command`
func (t *Timer) Alarm() {
if *alarmCommandFlag != "" {
cmd := exec.Command("sh", "-c", *alarmCommandFlag)
cmd.Start()
}
}
// Reset the timer
func (t *Timer) Reset() {
t.StartTime = time.Time{}
t.Duration = time.Duration(*durationFlag) * time.Minute
t.ShowElapsed = false
t.Save()
}
// Start the timer
func (t *Timer) Start() {
t.StartTime = time.Now()
t.Save()
}
// ToggleView will toggle between showing elapsed and remaining time.
func (t *Timer) ToggleView() {
if t.ShowElapsed {
t.ShowElapsed = false
} else {
t.ShowElapsed = true
}
t.Save()
}
// String formats the timer
func (t *Timer) String() string {
var elapsed time.Duration
if t.IsRunning() {
elapsed = time.Since(t.StartTime)
}
markupStart := ""
markupEnd := ""
if *colorsFlag && t.IsRunning() {
// Get elapsed
r := t.Remaining()
if r < t.Duration/4 {
// If remaining is < 25% of duration, print it red
markupStart = "<span color='red'>"
} else if r < t.Duration/2 {
// If remaining is < 50% of duration, print it yellow
markupStart = "<span color='yellow'>"
} else if r > t.Duration/2 {
// If remaining is > 50% of duration, print it green
markupStart = "<span color='#00ff00'>"
}
markupEnd = "</span>"
}
// Default to showing remaining time.
timerValue := (t.Duration - elapsed).Truncate(time.Duration(time.Second))
// Status string
status := "I"
if t.IsRunning() {
status = "R"
if t.ShowElapsed {
status = "E"
}
}
// Show elapsed if timer have been toggled.
if t.ShowElapsed {
timerValue = elapsed.Truncate(time.Duration(time.Second))
}
return fmt.Sprintf("%sTimer: %s [%s]%s",
markupStart,
timerValue,
status,
markupEnd)
}
// Save the content of the timer
func (t *Timer) Save() error {
currentUser, err := user.Current()
if err != nil {
return err
}
configFile := filepath.Join(currentUser.HomeDir, ".i3-timer.json")
j, err := json.Marshal(t)
if err != nil {
return err
}
return ioutil.WriteFile(configFile, j, 0644)
}
// Button represents the mouse button click
type Button string
// LoadTimer reads timer data from file
func LoadTimer() (Timer, error) {
var t Timer
// Check if timer exists
currentUser, err := user.Current()
if err != nil {
return t, err
}
configFile := filepath.Join(currentUser.HomeDir, ".i3-timer.json")
// If config file does not exist yet, create a default one
if _, err := os.Stat(configFile); os.IsNotExist(err) {
t.Duration = time.Duration(*durationFlag) * time.Minute
j, err := json.Marshal(t)
if err != nil {
return t, err
}
if err := ioutil.WriteFile(configFile, j, 0644); err != nil {
return t, err
}
} else {
// TODO: Check invalid timer
b, err := ioutil.ReadFile(configFile)
if err != nil {
return t, err
}
if err := json.Unmarshal(b, &t); err != nil {
return t, err
}
}
return t, nil
}
const (
// LeftButton toggles the timer display (elapsed/remaining)
LeftButton Button = "1"
// MiddleButton starts the timer
MiddleButton Button = "2"
// RightButton stops the timer
RightButton Button = "3"
// ScrollUpButton adds 1 minute to duration
ScrollUpButton Button = "4"
// ScrollDownButton removes 1 minute to duration
ScrollDownButton Button = "5"
)
func main() {
flag.Parse()
timer, err := LoadTimer()
if err != nil {
panic(err)
}
switch Button(os.Getenv("BLOCK_BUTTON")) {
case LeftButton:
// Toggle elapsed/remaining.
if timer.IsRunning() {
timer.ToggleView()
}
case MiddleButton:
// Start the timer if not started yet
if time.Time.IsZero(timer.StartTime) {
timer.Start()
}
case RightButton:
// Stop the timer if it's started
if timer.IsRunning() {
timer.Reset()
}
case ScrollUpButton:
if timer.IsNotRunning() {
timer.AddMinute()
}
case ScrollDownButton:
if timer.IsNotRunning() {
timer.SubtractMinute()
}
}
if timer.IsRunning() && timer.Remaining() <= 0 {
timer.Alarm()
timer.Reset()
if *recurringFlag {
timer.Start()
}
}
if !timer.IsRunning() && *autostartFlag {
timer.Start()
}
fmt.Println(timer.String())
}