refactor: move Pominal type & methods to new file
This commit is contained in:
parent
91077e94bf
commit
09002e6d91
2 changed files with 139 additions and 108 deletions
115
main.go
115
main.go
|
@ -24,8 +24,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -52,17 +50,6 @@ func init() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pominal struct {
|
|
||||||
timer *time.Timer
|
|
||||||
finish time.Time
|
|
||||||
work time.Duration
|
|
||||||
shortBreak time.Duration
|
|
||||||
longBreak time.Duration
|
|
||||||
maxWorkCycles int
|
|
||||||
session int
|
|
||||||
label string
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
if printVersion {
|
if printVersion {
|
||||||
|
@ -90,108 +77,20 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pominal := NewPominal(workTimeDuration, shortBreakTimeDuration, longBreakTimeDuration, maxWorkCycles)
|
pominal := NewPominal(
|
||||||
|
workTimeDuration,
|
||||||
|
shortBreakTimeDuration,
|
||||||
|
longBreakTimeDuration,
|
||||||
|
maxWorkCycles,
|
||||||
|
)
|
||||||
|
|
||||||
pominal.Run()
|
pominal.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPominal creates a new pominal instance
|
|
||||||
func NewPominal(w, s, l time.Duration, m int) Pominal {
|
|
||||||
|
|
||||||
return Pominal{
|
|
||||||
work: w,
|
|
||||||
shortBreak: s,
|
|
||||||
longBreak: l,
|
|
||||||
maxWorkCycles: m,
|
|
||||||
session: 1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run Pominal
|
|
||||||
func (p *Pominal) Run() {
|
|
||||||
workCycleCount := 1
|
|
||||||
t := time.NewTicker(1 * time.Second)
|
|
||||||
s := make(chan os.Signal, 1)
|
|
||||||
|
|
||||||
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
p.Start(workTimerLabel, workCycleCount)
|
|
||||||
|
|
||||||
infinite:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case sig := <-s:
|
|
||||||
fmt.Printf("\n\nReceived signal '%s'. Closing Pominal.\n", sig)
|
|
||||||
p.timer.Stop()
|
|
||||||
t.Stop()
|
|
||||||
break infinite
|
|
||||||
case <-t.C:
|
|
||||||
printScreen(p.TimeRemaining(), p.session, workCycleCount, p.maxWorkCycles, p.label)
|
|
||||||
case <-p.timer.C:
|
|
||||||
fmt.Printf("\n%s timer has finished\n", p.label)
|
|
||||||
p.timer.Stop()
|
|
||||||
t.Stop()
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
oldLabel := p.label
|
|
||||||
switch p.label {
|
|
||||||
case workTimerLabel:
|
|
||||||
if workCycleCount >= p.maxWorkCycles {
|
|
||||||
p.Start(longBreakTimerLabel, workCycleCount)
|
|
||||||
} else {
|
|
||||||
p.Start(shortBreakTimerLabel, workCycleCount)
|
|
||||||
}
|
|
||||||
case shortBreakTimerLabel:
|
|
||||||
workCycleCount++
|
|
||||||
p.Start(workTimerLabel, workCycleCount)
|
|
||||||
case longBreakTimerLabel:
|
|
||||||
workCycleCount = 1
|
|
||||||
p.IncrementCount()
|
|
||||||
p.Start(workTimerLabel, workCycleCount)
|
|
||||||
}
|
|
||||||
t = time.NewTicker(1 * time.Second)
|
|
||||||
newLabel := p.label
|
|
||||||
alert(oldLabel, newLabel)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start begins the timer specified by the
|
|
||||||
// label argument.
|
|
||||||
func (p *Pominal) Start(label string, workCycleCount int) {
|
|
||||||
var d time.Duration
|
|
||||||
|
|
||||||
switch label {
|
|
||||||
case workTimerLabel:
|
|
||||||
d = p.work
|
|
||||||
case shortBreakTimerLabel:
|
|
||||||
d = p.shortBreak
|
|
||||||
case longBreakTimerLabel:
|
|
||||||
d = p.longBreak
|
|
||||||
}
|
|
||||||
|
|
||||||
p.label = label
|
|
||||||
if p.timer == nil {
|
|
||||||
p.timer = time.NewTimer(d)
|
|
||||||
} else {
|
|
||||||
p.timer.Reset(d)
|
|
||||||
}
|
|
||||||
|
|
||||||
p.finish = time.Now().Add(d)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncrementCount increments the pominal session count
|
|
||||||
func (p *Pominal) IncrementCount() {
|
|
||||||
p.session++
|
|
||||||
}
|
|
||||||
|
|
||||||
// TimeRemaining returns the remaining time left
|
|
||||||
// on the timer
|
|
||||||
func (p *Pominal) TimeRemaining() float64 {
|
|
||||||
return p.finish.Sub(time.Now()).Seconds()
|
|
||||||
}
|
|
||||||
|
|
||||||
// printScreen prints the details of the Pominal session on screen including
|
// printScreen prints the details of the Pominal session on screen including
|
||||||
// the current work cycle number, the timer's current label and the time remaining
|
// the current work cycle number, the timer's current label and the time remaining
|
||||||
// on the timer.
|
// on the timer.
|
||||||
|
// TODO: To be removed when TUI is implemented.
|
||||||
func printScreen(remaining float64, pominalCount, workCycle, maxWorkCycle int, label string) {
|
func printScreen(remaining float64, pominalCount, workCycle, maxWorkCycle int, label string) {
|
||||||
clearScreen()
|
clearScreen()
|
||||||
remainingSecs := int(math.Ceil(remaining))
|
remainingSecs := int(math.Ceil(remaining))
|
||||||
|
|
132
pominal.go
Normal file
132
pominal.go
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
/*
|
||||||
|
Pominal
|
||||||
|
Copyright (C) 2019 Daniel Anglin
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Pominal struct {
|
||||||
|
timer *time.Timer
|
||||||
|
finish time.Time
|
||||||
|
work time.Duration
|
||||||
|
shortBreak time.Duration
|
||||||
|
longBreak time.Duration
|
||||||
|
maxWorkCycles int
|
||||||
|
session int
|
||||||
|
label string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPominal creates a new pominal instance
|
||||||
|
func NewPominal(w, s, l time.Duration, m int) Pominal {
|
||||||
|
|
||||||
|
return Pominal{
|
||||||
|
work: w,
|
||||||
|
shortBreak: s,
|
||||||
|
longBreak: l,
|
||||||
|
maxWorkCycles: m,
|
||||||
|
session: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run Pominal
|
||||||
|
func (p *Pominal) Run() {
|
||||||
|
workCycleCount := 1
|
||||||
|
t := time.NewTicker(1 * time.Second)
|
||||||
|
s := make(chan os.Signal, 1)
|
||||||
|
|
||||||
|
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
p.Start(workTimerLabel, workCycleCount)
|
||||||
|
|
||||||
|
infinite:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case sig := <-s:
|
||||||
|
fmt.Printf("\n\nReceived signal '%s'. Closing Pominal.\n", sig)
|
||||||
|
p.timer.Stop()
|
||||||
|
t.Stop()
|
||||||
|
break infinite
|
||||||
|
case <-t.C:
|
||||||
|
printScreen(p.TimeRemaining(), p.session, workCycleCount, p.maxWorkCycles, p.label)
|
||||||
|
case <-p.timer.C:
|
||||||
|
fmt.Printf("\n%s timer has finished\n", p.label)
|
||||||
|
p.timer.Stop()
|
||||||
|
t.Stop()
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
oldLabel := p.label
|
||||||
|
switch p.label {
|
||||||
|
case workTimerLabel:
|
||||||
|
if workCycleCount >= p.maxWorkCycles {
|
||||||
|
p.Start(longBreakTimerLabel, workCycleCount)
|
||||||
|
} else {
|
||||||
|
p.Start(shortBreakTimerLabel, workCycleCount)
|
||||||
|
}
|
||||||
|
case shortBreakTimerLabel:
|
||||||
|
workCycleCount++
|
||||||
|
p.Start(workTimerLabel, workCycleCount)
|
||||||
|
case longBreakTimerLabel:
|
||||||
|
workCycleCount = 1
|
||||||
|
p.IncrementCount()
|
||||||
|
p.Start(workTimerLabel, workCycleCount)
|
||||||
|
}
|
||||||
|
t = time.NewTicker(1 * time.Second)
|
||||||
|
newLabel := p.label
|
||||||
|
alert(oldLabel, newLabel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start begins the timer specified by the
|
||||||
|
// label argument.
|
||||||
|
func (p *Pominal) Start(label string, workCycleCount int) {
|
||||||
|
var d time.Duration
|
||||||
|
|
||||||
|
switch label {
|
||||||
|
case workTimerLabel:
|
||||||
|
d = p.work
|
||||||
|
case shortBreakTimerLabel:
|
||||||
|
d = p.shortBreak
|
||||||
|
case longBreakTimerLabel:
|
||||||
|
d = p.longBreak
|
||||||
|
}
|
||||||
|
|
||||||
|
p.label = label
|
||||||
|
if p.timer == nil {
|
||||||
|
p.timer = time.NewTimer(d)
|
||||||
|
} else {
|
||||||
|
p.timer.Reset(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
p.finish = time.Now().Add(d)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimeRemaining returns the remaining time left
|
||||||
|
// on the timer
|
||||||
|
func (p *Pominal) TimeRemaining() float64 {
|
||||||
|
return p.finish.Sub(time.Now()).Seconds()
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrementCount increments the pominal session count
|
||||||
|
func (p *Pominal) IncrementCount() {
|
||||||
|
p.session++
|
||||||
|
}
|
Loading…
Reference in a new issue