From 834a857227fb047005e93d98b006f40531fe34f2 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 26 Mar 2026 07:54:02 -0500 Subject: [PATCH] BuildFilters should _and_ contexts/projects/tags --- cmd/time.go | 2 +- util/helpers.go | 52 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/cmd/time.go b/cmd/time.go index 375875f..c590b6d 100644 --- a/cmd/time.go +++ b/cmd/time.go @@ -16,7 +16,7 @@ import ( // timeCmd represents the time command var timeCmd = &cobra.Command{ Use: "time", - Short: "A brief description of your command", + Short: "Output the total time for the given contexts/projects", RunE: opShowTimers, } diff --git a/util/helpers.go b/util/helpers.go index ecda574..aa3ba70 100644 --- a/util/helpers.go +++ b/util/helpers.go @@ -169,6 +169,23 @@ func ParseFuzzyTime(t string) (time.Time, error) { return ret, nil } } + // Check for some custom string values + now := time.Now() + y, m, d := now.Date() + switch t { + case "today", "today-start", "day", "day-start": + return time.Date(y, m, d, 0, 0, 0, 0, now.Location()), nil + case "today-end", "day-end": + return time.Date(y, m, d, 23, 59, 59, 0, now.Location()).Add(-time.Duration(time.Nanosecond)), nil + case "month", "month-start": + return time.Date(y, m, 1, 0, 0, 0, 0, now.Location()), nil + case "month-end": + return time.Date(y, m+1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Duration(time.Nanosecond)), nil + case "year", "year-start": + return time.Date(y, 1, 1, 0, 0, 0, 0, now.Location()), nil + case "year-end": + return time.Date(y+1, 1, 1, 0, 0, 0, 0, now.Location()).Add(-time.Duration(time.Nanosecond)), nil + } return time.Time{}, errors.New("Unable to parse time: " + t) } @@ -369,18 +386,32 @@ func BuildFilterFromArgs(args []string) func(*timertxt.Timer) bool { tagFilters, args = GetAdditionalTagsFromSlice(args) } if len(args) > 0 { + // Bounded by time. Parse the start time. + if args[0] == "today" || args[0] == "day" || args[0] == "month" || args[0] == "year" { + args[0] = fmt.Sprintf("%s-start", args[0]) + if len(args) == 1 { + args = append(args, fmt.Sprintf("%s-end", args[0])) + } + } var err error if start, err = ParseFuzzyTime(args[0]); err != nil { + // Error parsing start date y, m, d := time.Now().Date() start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location()) } else { + // Not sure what this arg is, discard it. args = args[1:] } if len(args) > 0 { + if args[0] == "today" || args[0] == "day" || args[0] == "month" || args[0] == "year" { + args[0] = fmt.Sprintf("%s-end", args[0]) + } + // Parse the end time if end, err = ParseFuzzyTime(args[0]); err != nil { y, m, d := time.Now().Date() end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location()) } else { + // Not sure what this arg is, discard it. args = args[1:] } } @@ -398,33 +429,36 @@ func BuildFilterFromArgs(args []string) func(*timertxt.Timer) bool { allFilters = append(allFilters, func(t timertxt.Timer) bool { for _, v := range contextFilters { v = strings.TrimPrefix(v, "@") - if t.HasContext(v) { - return true + if !t.HasContext(v) { + return false } } - return false + return true }) } if len(projectFilters) > 0 { allFilters = append(allFilters, func(t timertxt.Timer) bool { for _, v := range projectFilters { v = strings.TrimPrefix(v, "+") - if t.HasProject(v) { - return true + if !t.HasProject(v) { + return false } } - return false + return true }) } if len(tagFilters) > 0 { allFilters = append(allFilters, func(t timertxt.Timer) bool { for _, v := range tagFilters { pts := strings.Split(v, ":") - if len(pts) == 2 && t.HasTag(pts[0]) && t.GetTag(pts[0]) == pts[1] { - return true + if len(pts) < 2 || !t.HasTag(pts[0]) || t.GetTag(pts[0]) != pts[1] { + return false } + //if len(pts) == 2 && t.HasTag(pts[0]) && t.GetTag(pts[0]) == pts[1] { + // return true + //} } - return false + return true }) } doFilters := func(t *timertxt.Timer) bool {