From e6d9bfab2c41bbb53b9ec3836aed72184c75f94c Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Sun, 21 Jan 2018 01:20:01 +0800 Subject: [PATCH] add filtering --- main.go | 13 ++++++++++++- types.go | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 39a7a3d..22e0d0c 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/jawher/mow.cli" "os" + "sort" "time" ) @@ -48,13 +49,23 @@ func initialize(path *string) func(*cli.Cmd) { 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") + var ( + asJSON = cmd.BoolOpt("json", false, "output task history as JSON") + reverse = cmd.BoolOpt("r reverse", false, "sort tasks assending in age") + limit = cmd.IntOpt("n limit", 0, "limit the number of results by n") + ) cmd.Action = func() { db, err := NewStore(*path) maybe(err) defer db.Close() tasks, err := db.ReadTasks() maybe(err) + if *reverse { + sort.Sort(sort.Reverse(ByID(tasks))) + } + if *limit > 0 && (len(tasks) > *limit) { + tasks = tasks[0:*limit] + } if *asJSON { maybe(json.NewEncoder(os.Stdout).Encode(tasks)) return diff --git a/types.go b/types.go index e879c94..64f5a29 100644 --- a/types.go +++ b/types.go @@ -75,6 +75,13 @@ type Task struct { duration time.Duration } +// ByID is a sortable array of tasks +type ByID []*Task + +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 } + // Pomodoro is a unit of time to spend working // on a single task. type Pomodoro struct {