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 \ test \
docs \ docs \
pomo-build \ pomo-build \
readme readme \
bin/pomo
default: bin/pomo test default: bin/pomo test

View File

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

View File

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

View File

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