add filtering

This commit is contained in:
Kevin Schoon 2018-01-21 01:20:01 +08:00
parent 497f5c30e1
commit e6d9bfab2c
2 changed files with 19 additions and 1 deletions

13
main.go
View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"github.com/jawher/mow.cli" "github.com/jawher/mow.cli"
"os" "os"
"sort"
"time" "time"
) )
@ -48,13 +49,23 @@ func initialize(path *string) func(*cli.Cmd) {
func list(path *string) func(*cli.Cmd) { func list(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]" 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() { cmd.Action = func() {
db, err := NewStore(*path) db, err := NewStore(*path)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
tasks, err := db.ReadTasks() tasks, err := db.ReadTasks()
maybe(err) maybe(err)
if *reverse {
sort.Sort(sort.Reverse(ByID(tasks)))
}
if *limit > 0 && (len(tasks) > *limit) {
tasks = tasks[0:*limit]
}
if *asJSON { if *asJSON {
maybe(json.NewEncoder(os.Stdout).Encode(tasks)) maybe(json.NewEncoder(os.Stdout).Encode(tasks))
return return

View File

@ -75,6 +75,13 @@ type Task struct {
duration time.Duration 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 // Pomodoro is a unit of time to spend working
// on a single task. // on a single task.
type Pomodoro struct { type Pomodoro struct {