kevinschoon-pomo/task.go

153 lines
3.3 KiB
Go
Raw Normal View History

2018-01-20 13:03:23 +01:00
package main
import (
"fmt"
"github.com/gosuri/uilive"
"time"
)
type TaskRunner struct {
count int
task *Task
store *Store
writer *uilive.Writer
timer *time.Timer
ticker *time.Ticker
notifier Notifier
}
func NewTaskRunner(task *Task, store *Store) (*TaskRunner, error) {
taskID, err := store.CreateTask(*task)
if err != nil {
return nil, err
}
task.ID = taskID
tr := &TaskRunner{
task: task,
store: store,
notifier: NewLibNotifier(),
writer: uilive.New(),
timer: time.NewTimer(task.Duration),
ticker: time.NewTicker(RefreshInterval),
}
tr.writer.Start()
return tr, nil
}
func (t *TaskRunner) Run() error {
for t.count < t.task.NPomodoros {
// ASCII spinner
wheel := &Wheel{}
// This pomodoro
pomodoro := &Pomodoro{}
prompt("press enter to begin")
// Emit a desktop notification
// that the task is beginning.
t.notifier.Begin(t.count, *t.task)
// Record task as started
pomodoro.Start = time.Now()
// Reset the timer
t.timer.Reset(t.task.Duration)
// Wait for either a tick to update
// the UI for the timer to complete
loop:
select {
case <-t.ticker.C:
t.updateUI(Message{
Start: pomodoro.Start,
Duration: t.task.Duration,
Pomodoros: t.task.NPomodoros,
Wheel: wheel,
CurrentPomodoro: t.count,
})
goto loop
case <-t.timer.C:
// Send a notification for the
// user to take a break. We record
// how long it actually takes for
// them to initiate the break.
t.notifier.Break(*t.task)
prompt("press enter to take a break")
// Record the task as complete
pomodoro.End = time.Now()
// Record the session in the db
err := t.store.CreatePomodoro(t.task.ID, *pomodoro)
if err != nil {
return err
}
// Increment the count of completed pomodoros
t.count++
}
}
return nil
}
func (t *TaskRunner) updateUI(msg Message) {
fmt.Fprintf(
t.writer,
"%s %s remaining [ pomodoro %d/%d ]\n",
msg.Wheel,
(msg.Duration - time.Since(msg.Start)).Truncate(time.Second),
msg.CurrentPomodoro,
msg.Pomodoros,
)
}
/*
2018-01-20 13:03:23 +01:00
// 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, notifier Notifier, db *Store) {
2018-01-20 13:03:23 +01:00
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{}
maybe(notifier.Begin(task))
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(notifier.Break(task))
fmt.Println("press enter to 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
}
}
maybe(notifier.Finish(task))
2018-01-20 13:03:23 +01:00
writer.Stop()
}
*/