diff --git a/.gitignore b/.gitignore index 635c063..6c4e5a1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ bin/* !bin/.gitkeep dist/ Pominal +cover.out diff --git a/cases_test.go b/cases_test.go index b1d516c..1c663dd 100644 --- a/cases_test.go +++ b/cases_test.go @@ -43,6 +43,7 @@ var pominalTestCases = []struct { shortBreak: 300, longBreak: 600, label: "", + stopChan: nil, }, }, { @@ -61,6 +62,7 @@ var pominalTestCases = []struct { shortBreak: 1536, longBreak: 1200, label: "", + stopChan: nil, }, }, { @@ -79,6 +81,7 @@ var pominalTestCases = []struct { shortBreak: 60, longBreak: 60, label: "", + stopChan: nil, }, }, { @@ -97,6 +100,7 @@ var pominalTestCases = []struct { shortBreak: 300, longBreak: 1200, label: "", + stopChan: nil, }, }, } diff --git a/main.go b/main.go index 78bbb8b..4f9ad4d 100644 --- a/main.go +++ b/main.go @@ -93,5 +93,4 @@ func main() { if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil { panic(err) } - } diff --git a/pominal.go b/pominal.go index ceab8f6..bcd26b4 100644 --- a/pominal.go +++ b/pominal.go @@ -19,9 +19,6 @@ package main import ( - "os" - "os/signal" - "syscall" "time" "github.com/rivo/tview" @@ -55,6 +52,9 @@ type Pominal struct { // label labels Pominal based on the session. label string + + // stopChan is a no-data channel used to stop Pominal. + stopChan chan struct{} } // NewPominal creates a new pominal instance @@ -67,22 +67,21 @@ func NewPominal(w, s, l float64, m int) Pominal { work: setSessionTime(w), shortBreak: setSessionTime(s), longBreak: setSessionTime(l), + stopChan: nil, } } // Run Pominal func (p *Pominal) Run(infoUI, timerUI *tview.TextView) { - s := make(chan os.Signal, 1) - signal.Notify(s, syscall.SIGINT, syscall.SIGTERM) p.UpdateSession(infoUI) t := time.NewTicker(1 * time.Second) + p.stopChan = make(chan struct{}) infinite: for { select { - case <-s: - // fmt.Printf("\n\nReceived signal '%s'. Closing Pominal.\n", sig) + case <-p.stopChan: t.Stop() break infinite case <-t.C: @@ -99,6 +98,12 @@ infinite: } } +// Stop sends a signal to the Run method to stop the timer. +// Afterwards the process returns from the Run method. +func (p *Pominal) Stop() { + close(p.stopChan) +} + // UpdateSession resets Pominal's countdown // based on the next session. If a 'short break' // session is over the work session counter is incremented.