output tasks only from the last 24 hours by default, add more task output options

This commit is contained in:
Kevin Schoon 2018-02-09 12:02:34 -05:00
parent 1d59d0f7d0
commit 6dcdf6c826
2 changed files with 24 additions and 3 deletions

13
main.go
View File

@ -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]
}

View File

@ -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 {