From 6dcdf6c826b1efccd84326043037cac9457707af Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Fri, 9 Feb 2018 12:02:34 -0500 Subject: [PATCH] output tasks only from the last 24 hours by default, add more task output options --- main.go | 13 ++++++++++--- types.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 2da6240..c2a07ce 100644 --- a/main.go +++ b/main.go @@ -58,11 +58,15 @@ func list(path *string) func(*cli.Cmd) { return func(cmd *cli.Cmd) { cmd.Spec = "[OPTIONS]" var ( - asJSON = cmd.BoolOpt("json", false, "output task history as JSON") - assend = cmd.BoolOpt("assend", true, "sort tasks assending in age") - limit = cmd.IntOpt("n limit", 0, "limit the number of results by n") + asJSON = cmd.BoolOpt("json", false, "output task history as JSON") + assend = cmd.BoolOpt("assend", false, "sort tasks assending in age") + all = cmd.BoolOpt("a all", false, "output all tasks") + limit = cmd.IntOpt("n limit", 0, "limit the number of results by n") + duration = cmd.StringOpt("d duration", "24h", "show tasks within this duration") ) cmd.Action = func() { + duration, err := time.ParseDuration(*duration) + maybe(err) db, err := NewStore(*path) maybe(err) defer db.Close() @@ -71,6 +75,9 @@ func list(path *string) func(*cli.Cmd) { if *assend { sort.Sort(sort.Reverse(ByID(tasks))) } + if !*all { + tasks = After(time.Now().Add(-duration), tasks) + } if *limit > 0 && (len(tasks) > *limit) { tasks = tasks[0:*limit] } diff --git a/types.go b/types.go index 67a5dff..ea36f1b 100644 --- a/types.go +++ b/types.go @@ -151,6 +151,20 @@ func (b ByID) Len() int { return len(b) } func (b ByID) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b ByID) Less(i, j int) bool { return b[i].ID < b[j].ID } +// After returns tasks that were started after the +// provided start time. +func After(start time.Time, tasks []*Task) []*Task { + filtered := []*Task{} + for _, task := range tasks { + if len(task.Pomodoros) > 0 { + if start.Before(task.Pomodoros[0].Start) { + filtered = append(filtered, task) + } + } + } + return filtered +} + // Pomodoro is a unit of time to spend working // on a single task. type Pomodoro struct {