From cb5779343fc0280aa278beb72ebd4a3375a9c26f Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Sun, 21 Jan 2018 02:54:30 +0800 Subject: [PATCH] improve output, encode duration + target pomodoros --- main.go | 8 ++++---- store.go | 17 +++++++++++++---- task.go | 10 +++++----- types.go | 10 ++++++---- util.go | 28 +++++++++++++++++++++++----- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 48c1ea9..0b2cd8c 100644 --- a/main.go +++ b/main.go @@ -24,10 +24,10 @@ func start(path *string) func(*cli.Cmd) { maybe(err) defer db.Close() task := Task{ - Message: *message, - Tags: *tags, - pomodoros: *pomodoros, - duration: parsed, + Message: *message, + Tags: *tags, + NPomodoros: *pomodoros, + Duration: parsed, } run(task, &I3{}, db) } diff --git a/store.go b/store.go index ac36440..f6b601b 100644 --- a/store.go +++ b/store.go @@ -30,7 +30,9 @@ func (s Store) CreateTask(task Task) (int, error) { if err != nil { return -1, err } - _, err = tx.Exec("INSERT INTO task (message,tags) VALUES ($1,$2)", task.Message, strings.Join(task.Tags, ",")) + _, err = tx.Exec( + "INSERT INTO task (message,pomodoros,duration,tags) VALUES ($1,$2,$3,$4)", + task.Message, task.NPomodoros, task.Duration.String(), strings.Join(task.Tags, ",")) if err != nil { tx.Rollback() return -1, err @@ -54,18 +56,23 @@ func (s Store) CreatePomodoro(taskID int, pomodoro Pomodoro) error { } func (s Store) ReadTasks() ([]*Task, error) { - rows, err := s.db.Query(`SELECT rowid,message,tags FROM task`) + rows, err := s.db.Query(`SELECT rowid,message,pomodoros,duration,tags FROM task`) if err != nil { return nil, err } tasks := []*Task{} for rows.Next() { - var tags string + var ( + tags string + strDuration string + ) task := &Task{Pomodoros: []*Pomodoro{}} - err = rows.Scan(&task.ID, &task.Message, &tags) + err = rows.Scan(&task.ID, &task.Message, &task.NPomodoros, &strDuration, &tags) if err != nil { return nil, err } + duration, _ := time.ParseDuration(strDuration) + task.Duration = duration if tags != "" { task.Tags = strings.Split(tags, ",") } @@ -130,6 +137,8 @@ func initDB(db *Store) error { stmt := ` CREATE TABLE task ( message TEXT, + pomodoros INTEGER, + duration TEXT, tags TEXT ); CREATE TABLE pomodoro ( diff --git a/task.go b/task.go index 1e93324..685717c 100644 --- a/task.go +++ b/task.go @@ -31,21 +31,21 @@ func run(task Task, prompter Prompter, db *Store) { writer := uilive.New() writer.Start() ticker := time.NewTicker(RefreshInterval) - timer := time.NewTimer(task.duration) + timer := time.NewTimer(task.Duration) wheel := &Wheel{} var p int - for p < task.pomodoros { + for p < task.NPomodoros { pomodoro := &Pomodoro{} maybe(prompter.Prompt("Begin working!")) pomodoro.Start = time.Now() - timer.Reset(task.duration) + timer.Reset(task.Duration) loop: select { case <-ticker.C: display(writer, Message{ Start: pomodoro.Start, - Duration: task.duration, - Pomodoros: task.pomodoros, + Duration: task.Duration, + Pomodoros: task.NPomodoros, Wheel: wheel, CurrentPomodoro: p, }) diff --git a/types.go b/types.go index 5a30678..60c2961 100644 --- a/types.go +++ b/types.go @@ -89,14 +89,16 @@ func NewConfig(path string) (*Config, error) { // Task describes some activity type Task struct { - ID int `json:"id"` - Message string `json:"message"` + ID int `json:"id"` + Message string `json:"message"` + // Array of completed pomodoros Pomodoros []*Pomodoro `json:"pomodoros"` // Free-form tags associated with this task Tags []string `json:"tags"` // Number of pomodoros for this task - pomodoros int - duration time.Duration + NPomodoros int `json:"n_pomodoros"` + // Duration of each pomodoro + Duration time.Duration `json:"duration"` } // ByID is a sortable array of tasks diff --git a/util.go b/util.go index d710648..99aa249 100644 --- a/util.go +++ b/util.go @@ -2,9 +2,10 @@ package main import ( "fmt" - //"github.com/fatih/color" + "github.com/fatih/color" "os" "os/user" + "time" ) func maybe(err error) { @@ -22,17 +23,34 @@ func defaultConfigPath() string { func summerizeTasks(config *Config, tasks []*Task) { for _, task := range tasks { - var tags string + fmt.Printf("%d: [%s] ", task.ID, task.Duration.Truncate(time.Second)) + // a list of green/red pomodoros + // green[x x] red[x x] + fmt.Printf("[") + for i := 0; i < task.NPomodoros; i++ { + if i > 0 { + fmt.Printf(" ") + } + if len(task.Pomodoros) >= i { + color.New(color.FgGreen).Printf("X") + } else { + color.New(color.FgRed).Printf("X") + } + } + fmt.Printf("]") if len(task.Tags) > 0 { + fmt.Printf(" [") for i, tag := range task.Tags { if color, ok := config.Colors[tag]; ok { if i > 0 { - tags += " " + fmt.Printf(" ") } - tags += color.SprintfFunc()("%s", tag) + color.Printf("%s", tag) } } + fmt.Printf("]") } - fmt.Printf("%d [%s]: %s\n", task.ID, tags, task.Message) + fmt.Printf(" - %s", task.Message) + fmt.Printf("\n") } }