-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (194 loc) · 4.34 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
package main
import (
"bytes"
_ "embed"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/gopxl/beep"
"github.com/gopxl/beep/speaker"
"github.com/gopxl/beep/wav"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/timer"
tea "github.com/charmbracelet/bubbletea"
)
const default_timeout = time.Minute * 4
type model struct {
timer timer.Model
keymap keymap
help help.Model
quitting bool
soundPlaying bool
timeout time.Duration
raw bool
}
type keymap struct {
start key.Binding
stop key.Binding
reset key.Binding
quit key.Binding
}
//go:embed timer_done.wav
var soundFile []byte
func (m model) Init() tea.Cmd {
return m.timer.Init()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case timer.TickMsg:
m.timer, cmd = m.timer.Update(msg)
return m, cmd
case timer.StartStopMsg:
m.timer, cmd = m.timer.Update(msg)
m.keymap.stop.SetEnabled(m.timer.Running())
m.keymap.start.SetEnabled(!m.timer.Running())
return m, cmd
case timer.TimeoutMsg:
m.quitting = true
m.soundPlaying = true
var cmd tea.Cmd
if m.raw {
cmd = tea.Quit
time.Sleep(5000 * time.Millisecond)
} else {
cmd = play_sound()
}
return m, cmd
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, m.keymap.reset):
m.timer.Timeout = default_timeout
if m.timeout != 0 {
m.timer.Timeout = m.timeout
}
var cmds []tea.Cmd
if m.soundPlaying {
cmds = append(cmds, stopSound(), m.timer.Start())
m.soundPlaying = false
m.quitting = false
}
return m, tea.Batch(cmds...)
case key.Matches(msg, m.keymap.start, m.keymap.stop):
return m, m.timer.Toggle()
}
}
return m, cmd
}
func (m model) RunningHelpView() string {
return "\n " + m.help.ShortHelpView([]key.Binding{
m.keymap.start,
m.keymap.stop,
m.keymap.reset,
m.keymap.quit,
})
}
func (m model) DoneHelpView() string {
return "\n " + m.help.ShortHelpView([]key.Binding{
m.keymap.reset,
m.keymap.quit,
})
}
func (m model) View() string {
var s string
if m.raw {
s = m.timer.Timeout.String()
os.WriteFile("/tmp/cofe_status", []byte(s), 0644)
if m.timer.Timedout() {
s = "All done!"
}
} else {
s = "\n " + m.timer.Timeout.String()
s += "\n"
if m.timer.Timedout() {
s = "\n\n All done!\n\n" +
m.DoneHelpView()
}
if !m.quitting {
s = "\n\n Time remaining: " + s
s += m.RunningHelpView()
}
}
return s
}
type processFinishedMsg bool
func play_sound() tea.Cmd {
return func() tea.Msg {
f := bytes.NewReader(soundFile)
streamer, format, err := wav.Decode(f)
if err != nil {
log.Fatal(err)
}
defer streamer.Close()
sr := format.SampleRate * 2
speaker.Init(sr, sr.N(time.Second/10))
resampled := beep.Resample(4, format.SampleRate, sr, streamer)
done := make(chan bool)
speaker.Play(beep.Seq(resampled, beep.Callback(func() {
done <- true
})))
<-done
return processFinishedMsg(true)
}
}
func stopSound() tea.Cmd {
return func() tea.Msg {
speaker.Clear()
speaker.Close()
return nil
}
}
func main() {
timeout := default_timeout
var raw bool
flag.BoolVar(&raw, "raw", false, "Output raw time remaining to stdout")
flag.Parse()
remainingArgs := flag.Args()
if len(remainingArgs) == 1 {
if customTimeout, err := time.ParseDuration(remainingArgs[0]); err == nil {
timeout = customTimeout
} else {
fmt.Println("First argument has to be the desired duration.")
os.Exit(1)
}
}
m := model{
timer: timer.NewWithInterval(timeout, time.Second),
timeout: timeout,
raw: raw,
keymap: keymap{
start: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "start"),
),
stop: key.NewBinding(
key.WithKeys("s"),
key.WithHelp("s", "stop"),
),
reset: key.NewBinding(
key.WithKeys("r"),
key.WithHelp("r", "reset"),
),
quit: key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q", "quit"),
),
},
help: help.New(),
}
m.keymap.start.SetEnabled(false)
var options []tea.ProgramOption
if !m.raw {
options = append(options, tea.WithAltScreen())
}
if _, err := tea.NewProgram(m, options...).Run(); err != nil {
fmt.Println("Uh oh, we encountered an error:", err)
os.Exit(1)
}
}