Author: Kevin Schoon [me@kevinschoon.com]
Hash: 1b321198fbc47216065616301e0cbd30e193b44d
Timestamp: Mon, 30 May 2022 19:59:03 +0000 (2 years ago)

+22 -10 +/-4 browse
fix issue where interface can block
fix issue where interface can block

Fixed an issue where the UI can block when certain key combinations are
pressed in different states. An alternative and more robust approach would
likely be to re-write the runner code as a finite state machine, however
these quick fixes work okay.

Additionally cleaned up some spacing in console messages and added a CREATED
state which is the default state of a pomodoro.
1diff --git a/Makefile b/Makefile
2index c616043..2387871 100644
3--- a/Makefile
4+++ b/Makefile
5 @@ -10,7 +10,8 @@ LDFLAGS=\
6 test \
7 docs \
8 pomo-build \
9- readme
10+ readme \
11+ bin/pomo
12
13 default: bin/pomo test
14
15 diff --git a/pkg/internal/runner.go b/pkg/internal/runner.go
16index af12800..58a0039 100644
17--- a/pkg/internal/runner.go
18+++ b/pkg/internal/runner.go
19 @@ -2,6 +2,7 @@ package pomo
20
21 import (
22 "database/sql"
23+ "sync"
24 "time"
25 )
26
27 @@ -19,6 +20,7 @@ type TaskRunner struct {
28 toggle chan bool
29 notifier Notifier
30 duration time.Duration
31+ mu sync.Mutex
32 }
33
34 func NewMockedTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) {
35 @@ -28,7 +30,7 @@ func NewMockedTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunn
36 nPomodoros: task.NPomodoros,
37 origDuration: task.Duration,
38 store: store,
39- state: State(0),
40+ state: CREATED,
41 pause: make(chan bool),
42 toggle: make(chan bool),
43 notifier: notifier,
44 @@ -90,7 +92,6 @@ func (t *TaskRunner) run() error {
45 loop:
46 select {
47 case <-timer.C:
48- t.SetState(BREAKING)
49 t.stopped = time.Now()
50 t.count++
51 case <-t.toggle:
52 @@ -126,7 +127,7 @@ func (t *TaskRunner) run() error {
53 if t.count == t.nPomodoros {
54 break
55 }
56-
57+ t.SetState(BREAKING)
58 t.notifier.Notify("Pomo", "It is time to take a break!")
59 // Reset the duration incase it
60 // was paused.
61 @@ -141,11 +142,19 @@ func (t *TaskRunner) run() error {
62 }
63
64 func (t *TaskRunner) Toggle() {
65- t.toggle <- true
66+ t.mu.Lock()
67+ defer t.mu.Unlock()
68+ if t.state == BREAKING {
69+ t.toggle <- true
70+ }
71 }
72
73 func (t *TaskRunner) Pause() {
74- t.pause <- true
75+ t.mu.Lock()
76+ defer t.mu.Unlock()
77+ if t.state == PAUSED || t.state == RUNNING {
78+ t.pause <- true
79+ }
80 }
81
82 func (t *TaskRunner) Status() *Status {
83 diff --git a/pkg/internal/types.go b/pkg/internal/types.go
84index 3043a06..4485493 100644
85--- a/pkg/internal/types.go
86+++ b/pkg/internal/types.go
87 @@ -12,6 +12,8 @@ type State int
88
89 func (s State) String() string {
90 switch s {
91+ case CREATED:
92+ return "CREATED"
93 case RUNNING:
94 return "RUNNING"
95 case BREAKING:
96 @@ -25,7 +27,8 @@ func (s State) String() string {
97 }
98
99 const (
100- RUNNING State = iota + 1
101+ CREATED State = iota
102+ RUNNING
103 BREAKING
104 COMPLETE
105 PAUSED
106 diff --git a/pkg/internal/ui.go b/pkg/internal/ui.go
107index 9747a4b..787f749 100644
108--- a/pkg/internal/ui.go
109+++ b/pkg/internal/ui.go
110 @@ -29,14 +29,13 @@ func setContent(wheel *Wheel, status *Status, par *widgets.Paragraph) {
111 par.Text = fmt.Sprintf(
112 `It is time to take a break!
113
114-
115 Once you are ready, press [Enter]
116 to begin the next Pomodoro
117
118- %s %s pause duration
119+ %s %s break duration
120
121
122- [q] - quit [p] - pause
123+ [q] - quit
124 `,
125 wheel,
126 status.Pauseduration,