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
|
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/eiannone/keyboard"
)
type state int
const (
Working state = iota
Break
LongBreak
Pause
)
var (
currentState = Working
prevState = Working
currentDuration = 25 * 60
pomodoros = 1
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill, os.Interrupt)
defer cancel()
go timer(ctx)
go keys(cancel)
<-ctx.Done()
}
func keys(cancel func()) {
if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
_ = keyboard.Close()
}()
for {
char, _, err := keyboard.GetKey()
if err != nil {
panic(err)
}
switch string(char) {
case "p":
if currentState != Pause {
prevState = currentState
currentState = Pause
} else {
currentState = prevState
}
case "n":
currentDuration = 0
case "q":
cancel()
return
}
}
}
func timer(ctx context.Context) {
needClear := false
for {
if currentState != Pause {
currentDuration--
}
if currentDuration < 0 {
switch currentState {
case Working:
if pomodoros%4 == 0 {
currentState = LongBreak
currentDuration = 15 * 60
} else {
currentState = Break
currentDuration = 5 * 60
}
case Break, LongBreak:
currentState = Working
currentDuration = 25 * 60
pomodoros++
}
bell()
}
if needClear {
clear()
}
needClear = true
displayTimer()
select {
case <-time.After(1 * time.Second):
// Do nothing
case <-ctx.Done():
return
}
}
}
func displayTimer() {
stateText := ""
if currentState == Pause {
stateText = fmt.Sprintf("[Paused] %s", getStateText(prevState))
} else {
stateText = getStateText(currentState)
}
fmt.Printf("%s – %s\n(keys: p - pause/resume timer, n - next phase, q - quit)", stateText, secondsToMinutes(currentDuration))
}
func getStateText(state state) string {
switch state {
case Working:
return fmt.Sprintf("🍅 #%d Working", pomodoros)
case Break:
return fmt.Sprintf("☕️ #%d Break", pomodoros)
case LongBreak:
return fmt.Sprintf("☕️ #%d Long break", pomodoros)
}
return ""
}
func bell() {
fmt.Print("\u0007")
}
func clear() {
fmt.Print("\u001B[2K\u001B[F\u001B[2K\r")
}
func secondsToMinutes(inSeconds int) string {
minutes := inSeconds / 60
seconds := inSeconds % 60
str := fmt.Sprintf("%02d:%02d", minutes, seconds)
return str
}
|