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.
This commit is contained in:
Kevin Schoon 2022-05-30 14:59:03 -05:00
parent 1cac8904da
commit 1b321198fb
4 changed files with 22 additions and 10 deletions

View File

@ -10,7 +10,8 @@ LDFLAGS=\
test \
docs \
pomo-build \
readme
readme \
bin/pomo
default: bin/pomo test

View File

@ -2,6 +2,7 @@ package pomo
import (
"database/sql"
"sync"
"time"
)
@ -19,6 +20,7 @@ type TaskRunner struct {
toggle chan bool
notifier Notifier
duration time.Duration
mu sync.Mutex
}
func NewMockedTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunner, error) {
@ -28,7 +30,7 @@ func NewMockedTaskRunner(task *Task, store *Store, notifier Notifier) (*TaskRunn
nPomodoros: task.NPomodoros,
origDuration: task.Duration,
store: store,
state: State(0),
state: CREATED,
pause: make(chan bool),
toggle: make(chan bool),
notifier: notifier,
@ -90,7 +92,6 @@ func (t *TaskRunner) run() error {
loop:
select {
case <-timer.C:
t.SetState(BREAKING)
t.stopped = time.Now()
t.count++
case <-t.toggle:
@ -126,7 +127,7 @@ func (t *TaskRunner) run() error {
if t.count == t.nPomodoros {
break
}
t.SetState(BREAKING)
t.notifier.Notify("Pomo", "It is time to take a break!")
// Reset the duration incase it
// was paused.
@ -141,11 +142,19 @@ func (t *TaskRunner) run() error {
}
func (t *TaskRunner) Toggle() {
t.toggle <- true
t.mu.Lock()
defer t.mu.Unlock()
if t.state == BREAKING {
t.toggle <- true
}
}
func (t *TaskRunner) Pause() {
t.pause <- true
t.mu.Lock()
defer t.mu.Unlock()
if t.state == PAUSED || t.state == RUNNING {
t.pause <- true
}
}
func (t *TaskRunner) Status() *Status {

View File

@ -12,6 +12,8 @@ type State int
func (s State) String() string {
switch s {
case CREATED:
return "CREATED"
case RUNNING:
return "RUNNING"
case BREAKING:
@ -25,7 +27,8 @@ func (s State) String() string {
}
const (
RUNNING State = iota + 1
CREATED State = iota
RUNNING
BREAKING
COMPLETE
PAUSED

View File

@ -29,14 +29,13 @@ func setContent(wheel *Wheel, status *Status, par *widgets.Paragraph) {
par.Text = fmt.Sprintf(
`It is time to take a break!
Once you are ready, press [Enter]
to begin the next Pomodoro
%s %s pause duration
%s %s break duration
[q] - quit [p] - pause
[q] - quit
`,
wheel,
status.Pauseduration,