refactor: add a stop channel

This commit is contained in:
Dan Anglin 2020-01-19 21:26:46 +00:00
parent 45ad772818
commit c8a1d0adfa
No known key found for this signature in database
GPG key ID: 7AC2B18EC1D09F27
4 changed files with 17 additions and 8 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ bin/*
!bin/.gitkeep !bin/.gitkeep
dist/ dist/
Pominal Pominal
cover.out

View file

@ -43,6 +43,7 @@ var pominalTestCases = []struct {
shortBreak: 300, shortBreak: 300,
longBreak: 600, longBreak: 600,
label: "", label: "",
stopChan: nil,
}, },
}, },
{ {
@ -61,6 +62,7 @@ var pominalTestCases = []struct {
shortBreak: 1536, shortBreak: 1536,
longBreak: 1200, longBreak: 1200,
label: "", label: "",
stopChan: nil,
}, },
}, },
{ {
@ -79,6 +81,7 @@ var pominalTestCases = []struct {
shortBreak: 60, shortBreak: 60,
longBreak: 60, longBreak: 60,
label: "", label: "",
stopChan: nil,
}, },
}, },
{ {
@ -97,6 +100,7 @@ var pominalTestCases = []struct {
shortBreak: 300, shortBreak: 300,
longBreak: 1200, longBreak: 1200,
label: "", label: "",
stopChan: nil,
}, },
}, },
} }

View file

@ -93,5 +93,4 @@ func main() {
if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil { if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil {
panic(err) panic(err)
} }
} }

View file

@ -19,9 +19,6 @@
package main package main
import ( import (
"os"
"os/signal"
"syscall"
"time" "time"
"github.com/rivo/tview" "github.com/rivo/tview"
@ -55,6 +52,9 @@ type Pominal struct {
// label labels Pominal based on the session. // label labels Pominal based on the session.
label string label string
// stopChan is a no-data channel used to stop Pominal.
stopChan chan struct{}
} }
// NewPominal creates a new pominal instance // NewPominal creates a new pominal instance
@ -67,22 +67,21 @@ func NewPominal(w, s, l float64, m int) Pominal {
work: setSessionTime(w), work: setSessionTime(w),
shortBreak: setSessionTime(s), shortBreak: setSessionTime(s),
longBreak: setSessionTime(l), longBreak: setSessionTime(l),
stopChan: nil,
} }
} }
// Run Pominal // Run Pominal
func (p *Pominal) Run(infoUI, timerUI *tview.TextView) { func (p *Pominal) Run(infoUI, timerUI *tview.TextView) {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
p.UpdateSession(infoUI) p.UpdateSession(infoUI)
t := time.NewTicker(1 * time.Second) t := time.NewTicker(1 * time.Second)
p.stopChan = make(chan struct{})
infinite: infinite:
for { for {
select { select {
case <-s: case <-p.stopChan:
// fmt.Printf("\n\nReceived signal '%s'. Closing Pominal.\n", sig)
t.Stop() t.Stop()
break infinite break infinite
case <-t.C: 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 // UpdateSession resets Pominal's countdown
// based on the next session. If a 'short break' // based on the next session. If a 'short break'
// session is over the work session counter is incremented. // session is over the work session counter is incremented.