-
Notifications
You must be signed in to change notification settings - Fork 1
/
pomodoro.go
74 lines (58 loc) · 1.63 KB
/
pomodoro.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
package main
import (
"fmt"
"os"
"time"
"github.com/blakek/go-notifier"
"github.com/jessevdk/go-flags"
)
var arguments struct {
CustomTimer string `long:"custom" short:"c" description:"start a custom timer for given number of minutes" default:"25m"`
RunShortTimer bool `long:"short" short:"s" description:"starts a timer for 5 minutes"`
RunLongTimer bool `long:"long" short:"l" description:"starts a timer for 20 minutes"`
}
func clearCurrentLine() {
fmt.Print("\033[2K\r")
}
func printTime(timeRemaining time.Duration) {
clearCurrentLine()
if timeRemaining <= 0 {
fmt.Printf("Timer finished at %s\n", time.Now().Format(time.Kitchen))
} else {
fmt.Printf("%s remaining", timeRemaining.String())
}
}
func main() {
var duration time.Duration
flags.Parse(&arguments)
if arguments.RunLongTimer {
duration = 20 * time.Minute
} else if arguments.RunShortTimer {
duration = 5 * time.Minute
} else {
var parseError error
duration, parseError = time.ParseDuration(arguments.CustomTimer)
if parseError != nil {
fmt.Fprintln(os.Stderr, parseError)
os.Exit(2)
}
}
ticker := time.NewTicker(time.Second)
timeRemaining := duration
for range ticker.C {
timeRemaining = (timeRemaining - time.Second).Round(time.Second)
printTime(timeRemaining)
if timeRemaining <= 0 {
break
}
}
notification := notifier.Notification{
Title: "Timer Finished",
Message: fmt.Sprintf("Finished %s timer", duration.String()),
Timeout: 15,
}
systemNotifier, _ := notifier.NewNotifier()
systemNotifier.DeliverNotification(notification)
// HACK: tries to deliver notification before exiting
time.Sleep(time.Second)
}