From 1b321198fbc47216065616301e0cbd30e193b44d Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Mon, 30 May 2022 14:59:03 -0500 Subject: [PATCH] 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. --- Makefile | 3 ++- pkg/internal/runner.go | 19 ++++++++++++++----- pkg/internal/types.go | 5 ++++- pkg/internal/ui.go | 5 ++--- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index c616043..2387871 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,8 @@ LDFLAGS=\ test \ docs \ pomo-build \ - readme + readme \ + bin/pomo default: bin/pomo test diff --git a/pkg/internal/runner.go b/pkg/internal/runner.go index af12800..58a0039 100644 --- a/pkg/internal/runner.go +++ b/pkg/internal/runner.go @@ -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 { diff --git a/pkg/internal/types.go b/pkg/internal/types.go index 3043a06..4485493 100644 --- a/pkg/internal/types.go +++ b/pkg/internal/types.go @@ -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 diff --git a/pkg/internal/ui.go b/pkg/internal/ui.go index 9747a4b..787f749 100644 --- a/pkg/internal/ui.go +++ b/pkg/internal/ui.go @@ -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,