-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
timer.go
502 lines (425 loc) · 13.7 KB
/
timer.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Package config is responsible for setting the program config from
// the config file and command-line arguments
package config
import (
"errors"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/charmbracelet/huh"
"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)
type (
SessType string
// Message maps a session to a message.
Message map[SessType]string
// Duration maps a session to time duration value.
Duration map[SessType]time.Duration
// TimerConfig represents the program configuration derived from the config file
// and command-line arguments.
TimerConfig struct {
Duration Duration `json:"duration"`
Message Message `json:"message"`
AmbientSound string `json:"sound"`
BreakSound string `json:"break_sound"`
WorkSound string `json:"work_sound"`
PathToConfig string `json:"path_to_config"`
PathToDB string `json:"path_to_db"`
SessionCmd string `json:"session_cmd"`
WorkColor string `json:"work_color"`
ShortBreakColor string `json:"short_break_color"`
LongBreakColor string `json:"long_break_color"`
Tags []string `json:"tags"`
LongBreakInterval int `json:"long_break_interval"`
Notify bool `json:"notify"`
DarkTheme bool `json:"dark_theme"`
TwentyFourHourClock bool `json:"twenty_four_hour_clock"`
PlaySoundOnBreak bool `json:"sound_on_break"`
AutoStartBreak bool `json:"auto_start_break"`
AutoStartWork bool `json:"auto_start_work"`
Strict bool `json:"strict"`
}
)
const (
Work SessType = "Work session"
ShortBreak SessType = "Short break"
LongBreak SessType = "Long break"
)
const ascii = `
███████╗ ██████╗ ██████╗██╗ ██╗███████╗
██╔════╝██╔═══██╗██╔════╝██║ ██║██╔════╝
█████╗ ██║ ██║██║ ██║ ██║███████╗
██╔══╝ ██║ ██║██║ ██║ ██║╚════██║
██║ ╚██████╔╝╚██████╗╚██████╔╝███████║
╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝`
const (
defaultWorkMins = 25
defaultShortBreakMins = 5
defaultLongBreakMins = 15
defaultLongBreakInterval = 4
)
const (
defaultWorkColor = "#B0DB43"
defaultShortBreakColor = "#12EAEA"
defaultLongBreakColor = "#C492B1"
)
const SoundOff = "off"
// Legacy config.
const (
legacyWorkMins = "work_mins"
legacyShortBreakMins = "short_break_mins"
legacyLongBreakMins = "long_break_mins"
)
const (
configWorkDur = "work_duration"
configWorkMessage = "work_msg"
configAmbientSound = "sound"
configShortBreakDur = "short_break_duration"
configShortBreakMessage = "short_break_msg"
configLongBreakDur = "long_break_duration"
configLongBreakMessage = "long_break_msg"
configLongBreakInterval = "long_break_interval"
configAutoStartWork = "auto_start_work"
configAutoStartBreak = "auto_start_break"
configNotify = "notify"
configSoundOnBreak = "sound_on_break"
configTwentyFourHourClock = "24hr_clock"
configSessionCmd = "session_cmd"
configDarkTheme = "dark_theme"
configBreakSound = "break_sound"
configWorkSound = "work_sound"
configStrict = "strict"
configWorkColor = "work_color"
configShortBreakColor = "short_break_color"
configLongBreakColor = "long_break_color"
)
var once sync.Once
var timerCfg = &TimerConfig{
Message: make(Message),
Duration: make(Duration),
}
var (
errReadingInput = errors.New(
"An error occurred while reading input. Please try again",
)
errExpectedInteger = errors.New(
"Expected an integer that must be greater than zero",
)
errInitFailed = errors.New(
"Unable to initialise Focus settings from the configuration file",
)
)
func parseTime(s, key string, d int) time.Duration {
_, err := strconv.Atoi(s)
if err == nil {
s += "m"
}
dur, err := time.ParseDuration(s)
if err != nil {
warnOnInvalidConfig(key, d)
dur = time.Duration(d) * time.Minute
}
return dur
}
// prompt allows the user to state their preferred values for the most
// important timer settings. It is run only when a configuration file
// is not already present (e.g on first run).
func prompt() error {
var (
workDur int
shortBreakDur int
longBreakDur int
longBreakInterval int
)
pterm.Fprintln(Stderr, pterm.Sprintf("%s\n", ascii))
pterm.Fprintln(
Stderr,
pterm.Sprintf(
"%s: your preferences will be saved to -> %s\n",
pterm.Green("info"),
timerCfg.PathToConfig,
),
)
_ = putils.BulletListFromString(`Follow the prompts below to configure Focus for the first time.
Select your preferred value, or press ENTER to accept the defaults.
Edit the config file with 'focus edit-config' to change any settings.`, " ").
Render()
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[int]().
Title("Work length").
Options(
huh.NewOption("25 minutes", 25).Selected(true),
huh.NewOption("35 minutes", 35),
huh.NewOption("50 minutes", 50),
huh.NewOption("60 minutes", 60),
huh.NewOption("90 minutes", 90),
).
Value(&workDur),
),
huh.NewGroup(
huh.NewSelect[int]().
Title("Short break length").
Options(
huh.NewOption("5 minutes", 5).Selected(true),
huh.NewOption("10 minutes", 10),
huh.NewOption("15 minutes", 15),
huh.NewOption("20 minutes", 20),
huh.NewOption("30 minutes", 30),
).
Value(&shortBreakDur),
),
huh.NewGroup(
huh.NewSelect[int]().
Title("Long break length").
Options(
huh.NewOption("15 minutes", 15).Selected(true),
huh.NewOption("30 minutes", 30),
huh.NewOption("45 minutes", 45),
huh.NewOption("60 minutes", 60),
).
Value(&longBreakDur),
),
huh.NewGroup(
huh.NewSelect[int]().
Title("Long break interval").
Options(
huh.NewOption("4", 4).Selected(true),
huh.NewOption("6", 6),
huh.NewOption("8", 8),
).
Value(&longBreakInterval),
),
)
err := form.Run()
if err != nil {
return err
}
viper.Set(configWorkDur, strconv.Itoa(workDur)+"m")
viper.Set(configShortBreakDur, strconv.Itoa(shortBreakDur)+"m")
viper.Set(configLongBreakDur, strconv.Itoa(longBreakDur)+"m")
viper.Set(configLongBreakInterval, longBreakInterval)
return nil
}
// overrideConfigFromArgs retrieves user-defined configuration set through
// command-line arguments and updates the timer configuration.
func overrideConfigFromArgs(ctx *cli.Context) {
tagArg := ctx.String("tag")
if tagArg != "" {
tags := strings.Split(tagArg, ",")
for i := range tags {
tags[i] = strings.Trim(tags[i], " ")
}
timerCfg.Tags = tags
}
if ctx.Bool("disable-notification") {
timerCfg.Notify = false
}
if ctx.Bool("strict") {
timerCfg.Strict = true
}
timerCfg.PlaySoundOnBreak = ctx.Bool("sound-on-break")
ambientSound := ctx.String("sound")
if ambientSound != "" {
if ambientSound == SoundOff {
timerCfg.AmbientSound = ""
} else {
timerCfg.AmbientSound = ambientSound
}
}
breakSound := ctx.String("break-sound")
if breakSound != "" {
if breakSound == SoundOff {
timerCfg.BreakSound = ""
} else {
timerCfg.BreakSound = breakSound
}
}
workSound := ctx.String("work-sound")
if workSound != "" {
if workSound == SoundOff {
timerCfg.WorkSound = ""
} else {
timerCfg.WorkSound = workSound
}
}
if ctx.String("session-cmd") != "" {
timerCfg.SessionCmd = ctx.String("session-cmd")
}
if ctx.String("work") != "" {
timerCfg.Duration[Work] = parseTime(
ctx.String("work"),
configWorkDur,
defaultWorkMins,
)
}
if ctx.String("short-break") != "" {
timerCfg.Duration[ShortBreak] = parseTime(
ctx.String("short-break"),
configShortBreakDur,
defaultShortBreakMins,
)
}
if ctx.String("long-break") != "" {
timerCfg.Duration[LongBreak] = parseTime(
ctx.String("short-break"),
configShortBreakDur,
defaultShortBreakMins,
)
}
if ctx.Uint("long-break-interval") > 0 {
timerCfg.LongBreakInterval = int(ctx.Uint("long-break-interval"))
}
}
func warnOnInvalidConfig(configKey string, defaultVal int) {
pterm.Warning.Printfln(
"config error: invalid %s value, using default (%d)",
configKey,
defaultVal,
)
}
// updateConfigFromFile retrieves configuration values from the config
// file and uses to update the timer configuration.
func updateConfigFromFile() {
longBreakInterval := viper.GetInt(configLongBreakInterval)
if longBreakInterval < 1 {
warnOnInvalidConfig(configLongBreakInterval, defaultLongBreakInterval)
longBreakInterval = defaultLongBreakInterval
}
workDurConfig := viper.GetString(legacyWorkMins)
if viper.GetString(configWorkDur) != "" {
workDurConfig = viper.GetString(configWorkDur)
}
workDur := parseTime(workDurConfig, configWorkDur, defaultWorkMins)
shortBreakDurConfig := viper.GetString(legacyShortBreakMins)
if viper.GetString(configShortBreakDur) != "" {
shortBreakDurConfig = viper.GetString(configShortBreakDur)
}
shortBreakDur := parseTime(
shortBreakDurConfig,
configShortBreakDur,
defaultShortBreakMins,
)
longBreakDurConfig := viper.GetString(legacyLongBreakMins)
if viper.GetString(configLongBreakDur) != "" {
longBreakDurConfig = viper.GetString(configLongBreakDur)
}
longBreakDur := parseTime(
longBreakDurConfig,
configLongBreakDur,
defaultLongBreakMins,
)
timerCfg.LongBreakInterval = longBreakInterval
timerCfg.Duration[Work] = workDur
timerCfg.Duration[ShortBreak] = shortBreakDur
timerCfg.Duration[LongBreak] = longBreakDur
timerCfg.AutoStartBreak = viper.GetBool(configAutoStartBreak)
timerCfg.AutoStartWork = viper.GetBool(configAutoStartWork)
timerCfg.Strict = viper.GetBool(configStrict)
timerCfg.Notify = viper.GetBool(configNotify)
timerCfg.TwentyFourHourClock = viper.GetBool(configTwentyFourHourClock)
timerCfg.PlaySoundOnBreak = viper.GetBool(configSoundOnBreak)
timerCfg.AmbientSound = viper.GetString(configAmbientSound)
timerCfg.SessionCmd = viper.GetString(configSessionCmd)
timerCfg.BreakSound = viper.GetString(configBreakSound)
timerCfg.WorkSound = viper.GetString(configWorkSound)
timerCfg.WorkColor = viper.GetString(configWorkColor)
timerCfg.ShortBreakColor = viper.GetString(configShortBreakColor)
timerCfg.LongBreakColor = viper.GetString(configLongBreakColor)
if viper.IsSet(configDarkTheme) {
timerCfg.DarkTheme = viper.GetBool(configDarkTheme)
} else {
timerCfg.DarkTheme = true
}
timerCfg.Message[Work] = viper.GetString(configWorkMessage)
timerCfg.Message[ShortBreak] = viper.GetString(
configShortBreakMessage,
)
timerCfg.Message[LongBreak] = viper.GetString(
configLongBreakMessage,
)
}
// setTimerConfig overrides the default configuaration with user-defined
// settings retrieved from the config file and command-line arguments. The
// latter overrides the former.
func setTimerConfig(ctx *cli.Context) {
timerCfg.PathToDB = dbFilePath
// set from config file
updateConfigFromFile()
// set from command-line arguments
overrideConfigFromArgs(ctx)
}
// createTimerConfig saves the user's configuration to disk
// after prompting for key settings.
func createTimerConfig() error {
err := prompt()
if err != nil {
return err
}
timerDefaults()
err = viper.WriteConfigAs(timerCfg.PathToConfig)
if err != nil {
return err
}
pterm.Success.Printfln(
"Your settings have been saved. Thanks for using Focus!\n\n",
)
return nil
}
// timerDefaults sets the timer's default configuration values.
func timerDefaults() {
viper.SetDefault(configWorkDur, defaultWorkMins*time.Minute)
viper.SetDefault(configWorkMessage, "Focus on your task")
viper.SetDefault(configShortBreakMessage, "Take a breather")
viper.SetDefault(configShortBreakDur, defaultShortBreakMins*time.Minute)
viper.SetDefault(configLongBreakMessage, "Take a long break")
viper.SetDefault(configLongBreakDur, defaultLongBreakMins*time.Minute)
viper.SetDefault(configLongBreakInterval, defaultLongBreakInterval)
viper.SetDefault(configAutoStartBreak, true)
viper.SetDefault(configAutoStartWork, false)
viper.SetDefault(configNotify, true)
viper.SetDefault(configSoundOnBreak, false)
viper.SetDefault(configAmbientSound, "")
viper.SetDefault(configSessionCmd, "")
viper.SetDefault(configDarkTheme, true)
viper.SetDefault(configBreakSound, "bell")
viper.SetDefault(configWorkSound, "loud_bell")
viper.SetDefault(configStrict, false)
viper.SetDefault(configWorkColor, defaultWorkColor)
viper.SetDefault(configShortBreakColor, defaultShortBreakColor)
viper.SetDefault(configLongBreakColor, defaultLongBreakColor)
}
// initTimerConfig initialises the application configuration.
// If the config file does not exist,.it prompts the user
// and saves the inputted preferences and defaults in a config file.
func initTimerConfig() error {
viper.SetConfigName(configFileName)
viper.SetConfigType("yaml")
timerCfg.PathToConfig = configFilePath
viper.AddConfigPath(filepath.Dir(timerCfg.PathToConfig))
if err := viper.ReadInConfig(); err != nil {
if errors.As(err, &viper.ConfigFileNotFoundError{}) {
return createTimerConfig()
}
return err
}
return nil
}
// Timer initializes and returns the timer configuration.
func Timer(ctx *cli.Context) *TimerConfig {
once.Do(func() {
err := initTimerConfig()
if err != nil {
pterm.Error.Printfln("%s: %s", errInitFailed.Error(), err.Error())
os.Exit(1)
}
setTimerConfig(ctx)
})
return timerCfg
}