kevinschoon-pomo/task.go

62 lines
1.2 KiB
Go
Raw Normal View History

2018-01-20 13:03:23 +01:00
package main
import (
"fmt"
"github.com/gosuri/uilive"
"io"
"time"
)
// Task Starting..
//
2018-01-20 13:17:58 +01:00
// 20min remaning [pomodoro 1/4]
2018-01-20 13:03:23 +01:00
// ..
2018-01-20 13:17:58 +01:00
// 15min remaining [pomodoro 2/4]
2018-01-20 13:03:23 +01:00
// ..
// Task Completed!
func display(writer io.Writer, msg Message) {
fmt.Fprintf(
writer,
2018-01-20 18:49:07 +01:00
"%s %s remaining [ pomodoro %d/%d ]\n",
msg.Wheel,
2018-01-20 13:03:23 +01:00
(msg.Duration - time.Since(msg.Start)).Truncate(time.Second),
2018-01-20 13:17:58 +01:00
msg.CurrentPomodoro,
msg.Pomodoros,
2018-01-20 13:03:23 +01:00
)
}
func run(task Task, prompter Prompter, db *Store) {
taskID, err := db.CreateTask(task)
maybe(err)
writer := uilive.New()
writer.Start()
ticker := time.NewTicker(RefreshInterval)
timer := time.NewTimer(task.Duration)
2018-01-20 18:49:07 +01:00
wheel := &Wheel{}
2018-01-20 13:17:58 +01:00
var p int
for p < task.NPomodoros {
2018-01-20 13:17:58 +01:00
pomodoro := &Pomodoro{}
2018-01-20 13:03:23 +01:00
maybe(prompter.Prompt("Begin working!"))
2018-01-20 13:17:58 +01:00
pomodoro.Start = time.Now()
timer.Reset(task.Duration)
2018-01-20 13:03:23 +01:00
loop:
select {
case <-ticker.C:
display(writer, Message{
2018-01-20 13:17:58 +01:00
Start: pomodoro.Start,
Duration: task.Duration,
Pomodoros: task.NPomodoros,
2018-01-20 18:49:07 +01:00
Wheel: wheel,
2018-01-20 13:17:58 +01:00
CurrentPomodoro: p,
2018-01-20 13:03:23 +01:00
})
goto loop
case <-timer.C:
maybe(prompter.Prompt("Take a break!"))
2018-01-20 13:17:58 +01:00
pomodoro.End = time.Now()
maybe(db.CreatePomodoro(taskID, *pomodoro))
p++
2018-01-20 13:03:23 +01:00
}
}
writer.Stop()
}