pominal/pominal.go

132 lines
3 KiB
Go

/*
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++
}