[record,stent,count] -> pomodoro

This commit is contained in:
Kevin Schoon 2018-01-20 20:17:58 +08:00
parent 24672d396e
commit 8c071b0c94
4 changed files with 54 additions and 55 deletions

View File

@ -20,7 +20,7 @@ func start(path *string) func(*cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE" cmd.Spec = "[OPTIONS] MESSAGE"
var ( var (
duration = cmd.StringOpt("d duration", "25m", "duration of each stent") duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
stents = cmd.IntOpt("s stents", 4, "number of working stents") pomodoros = cmd.IntOpt("p pomodoros", 4, "number of pomodoros")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task") message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task") tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
) )
@ -33,7 +33,7 @@ func start(path *string) func(*cli.Cmd) {
task := Task{ task := Task{
Message: *message, Message: *message,
Tags: *tags, Tags: *tags,
stents: *stents, pomodoros: *pomodoros,
duration: parsed, duration: parsed,
} }
run(task, &I3{}, db) run(task, &I3{}, db)

View File

@ -50,12 +50,12 @@ func (s Store) CreateTask(task Task) (int, error) {
return taskID, tx.Commit() return taskID, tx.Commit()
} }
func (s Store) CreateRecord(taskID int, record Record) error { func (s Store) CreatePomodoro(taskID int, pomodoro Pomodoro) error {
_, err := s.db.Exec( _, err := s.db.Exec(
`INSERT INTO record (task_id, start, end) VALUES ($1, $2, $3)`, `INSERT INTO pomodoro (task_id, start, end) VALUES ($1, $2, $3)`,
taskID, taskID,
record.Start, pomodoro.Start,
record.End, pomodoro.End,
) )
return err return err
} }
@ -68,47 +68,47 @@ func (s Store) ReadTasks() ([]*Task, error) {
tasks := []*Task{} tasks := []*Task{}
for rows.Next() { for rows.Next() {
var tags string var tags string
task := &Task{Records: []*Record{}} task := &Task{Pomodoros: []*Pomodoro{}}
err = rows.Scan(&task.ID, &task.Message, &tags) err = rows.Scan(&task.ID, &task.Message, &tags)
if err != nil { if err != nil {
return nil, err return nil, err
} }
task.Tags = strings.Split(tags, ",") task.Tags = strings.Split(tags, ",")
records, err := s.ReadRecords(task.ID) pomodoros, err := s.ReadPomodoros(task.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, record := range records { for _, pomodoro := range pomodoros {
task.Records = append(task.Records, record) task.Pomodoros = append(task.Pomodoros, pomodoro)
} }
tasks = append(tasks, task) tasks = append(tasks, task)
} }
return tasks, nil return tasks, nil
} }
func (s Store) ReadRecords(taskID int) ([]*Record, error) { func (s Store) ReadPomodoros(taskID int) ([]*Pomodoro, error) {
rows, err := s.db.Query(`SELECT start,end FROM record WHERE task_id = $1`, &taskID) rows, err := s.db.Query(`SELECT start,end FROM pomodoro WHERE task_id = $1`, &taskID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
records := []*Record{} pomodoros := []*Pomodoro{}
for rows.Next() { for rows.Next() {
var ( var (
startStr string startStr string
endStr string endStr string
) )
record := &Record{} pomodoro := &Pomodoro{}
err = rows.Scan(&startStr, &endStr) err = rows.Scan(&startStr, &endStr)
if err != nil { if err != nil {
return nil, err return nil, err
} }
start, _ := time.Parse(datetimeFmt, startStr) start, _ := time.Parse(datetimeFmt, startStr)
end, _ := time.Parse(datetimeFmt, endStr) end, _ := time.Parse(datetimeFmt, endStr)
record.Start = start pomodoro.Start = start
record.End = end pomodoro.End = end
records = append(records, record) pomodoros = append(pomodoros, pomodoro)
} }
return records, nil return pomodoros, nil
} }
func (s Store) DeleteTask(taskID int) error { func (s Store) DeleteTask(taskID int) error {
@ -137,7 +137,7 @@ func initDB(db *Store) error {
message TEXT, message TEXT,
tags TEXT tags TEXT
); );
CREATE TABLE record ( CREATE TABLE pomodoro (
task_id INTEGER, task_id INTEGER,
start DATETTIME, start DATETTIME,
end DATETTIME end DATETTIME

31
task.go
View File

@ -9,18 +9,18 @@ import (
// Task Starting.. // Task Starting..
// //
// 20min remaning [stent 1/4] // 20min remaning [pomodoro 1/4]
// .. // ..
// 15min remaining [stent 2/4] // 15min remaining [pomodoro 2/4]
// .. // ..
// Task Completed! // Task Completed!
func display(writer io.Writer, msg Message) { func display(writer io.Writer, msg Message) {
fmt.Fprintf( fmt.Fprintf(
writer, writer,
"%s remaining [ stent %d/%d ]\n", "%s remaining [ pomodoro %d/%d ]\n",
(msg.Duration - time.Since(msg.Start)).Truncate(time.Second), (msg.Duration - time.Since(msg.Start)).Truncate(time.Second),
msg.CurrentStent, msg.CurrentPomodoro,
msg.Stents, msg.Pomodoros,
) )
} }
@ -31,29 +31,28 @@ func run(task Task, prompter Prompter, db *Store) {
writer.Start() writer.Start()
ticker := time.NewTicker(RefreshInterval) ticker := time.NewTicker(RefreshInterval)
timer := time.NewTimer(task.duration) timer := time.NewTimer(task.duration)
var currentStent int var p int
for currentStent < task.stents { for p < task.pomodoros {
record := &Record{} pomodoro := &Pomodoro{}
maybe(prompter.Prompt("Begin working!")) maybe(prompter.Prompt("Begin working!"))
record.Start = time.Now() pomodoro.Start = time.Now()
timer.Reset(task.duration) timer.Reset(task.duration)
loop: loop:
select { select {
case <-ticker.C: case <-ticker.C:
display(writer, Message{ display(writer, Message{
Start: record.Start, Start: pomodoro.Start,
Duration: task.duration, Duration: task.duration,
Stents: task.stents, Pomodoros: task.pomodoros,
CurrentStent: currentStent, CurrentPomodoro: p,
}) })
goto loop goto loop
case <-timer.C: case <-timer.C:
maybe(prompter.Prompt("Take a break!")) maybe(prompter.Prompt("Take a break!"))
record.End = time.Now() pomodoro.End = time.Now()
maybe(db.CreateRecord(taskID, *record)) maybe(db.CreatePomodoro(taskID, *pomodoro))
currentStent++ p++
} }
maybe(db.CreateRecord(taskID, *record))
} }
writer.Stop() writer.Stop()
} }

View File

@ -14,25 +14,25 @@ const RefreshInterval = 800 * time.Millisecond
type Message struct { type Message struct {
Start time.Time Start time.Time
Duration time.Duration Duration time.Duration
Stents int Pomodoros int
CurrentStent int CurrentPomodoro int
} }
// Task describes some activity // Task describes some activity
type Task struct { type Task struct {
ID int `json:"id"` ID int `json:"id"`
Message string `json:"message"` Message string `json:"message"`
Records []*Record `json:"records"` Pomodoros []*Pomodoro `json:"pomodoros"`
// Free-form tags associated with this task // Free-form tags associated with this task
Tags []string `json:"tags"` Tags []string `json:"tags"`
// Number of iterations to perform the task // Number of pomodoros for this task
stents int pomodoros int
duration time.Duration duration time.Duration
} }
// Record is a stetch of work performed on a // Pomodoro is a unit of time to spend working
// specific task. // on a single task.
type Record struct { type Pomodoro struct {
Start time.Time `json:"start"` Start time.Time `json:"start"`
End time.Time `json:"end"` End time.Time `json:"end"`
} }