BuildFilters should _and_ contexts/projects/tags

This commit is contained in:
2026-03-26 07:54:02 -05:00
parent 729084ae74
commit 834a857227
2 changed files with 44 additions and 10 deletions

View File

@@ -16,7 +16,7 @@ import (
// timeCmd represents the time command // timeCmd represents the time command
var timeCmd = &cobra.Command{ var timeCmd = &cobra.Command{
Use: "time", Use: "time",
Short: "A brief description of your command", Short: "Output the total time for the given contexts/projects",
RunE: opShowTimers, RunE: opShowTimers,
} }

View File

@@ -169,6 +169,23 @@ func ParseFuzzyTime(t string) (time.Time, error) {
return ret, nil 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) 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) tagFilters, args = GetAdditionalTagsFromSlice(args)
} }
if len(args) > 0 { 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 var err error
if start, err = ParseFuzzyTime(args[0]); err != nil { if start, err = ParseFuzzyTime(args[0]); err != nil {
// Error parsing start date
y, m, d := time.Now().Date() y, m, d := time.Now().Date()
start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location()) start = time.Date(y, m, d, 0, 0, 0, 0, time.Now().Location())
} else { } else {
// Not sure what this arg is, discard it.
args = args[1:] args = args[1:]
} }
if len(args) > 0 { 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 { if end, err = ParseFuzzyTime(args[0]); err != nil {
y, m, d := time.Now().Date() y, m, d := time.Now().Date()
end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location()) end = time.Date(y, m, d, 23, 59, 59, 0, time.Now().Location())
} else { } else {
// Not sure what this arg is, discard it.
args = args[1:] args = args[1:]
} }
} }
@@ -398,33 +429,36 @@ func BuildFilterFromArgs(args []string) func(*timertxt.Timer) bool {
allFilters = append(allFilters, func(t timertxt.Timer) bool { allFilters = append(allFilters, func(t timertxt.Timer) bool {
for _, v := range contextFilters { for _, v := range contextFilters {
v = strings.TrimPrefix(v, "@") v = strings.TrimPrefix(v, "@")
if t.HasContext(v) { if !t.HasContext(v) {
return true return false
} }
} }
return false return true
}) })
} }
if len(projectFilters) > 0 { if len(projectFilters) > 0 {
allFilters = append(allFilters, func(t timertxt.Timer) bool { allFilters = append(allFilters, func(t timertxt.Timer) bool {
for _, v := range projectFilters { for _, v := range projectFilters {
v = strings.TrimPrefix(v, "+") v = strings.TrimPrefix(v, "+")
if t.HasProject(v) { if !t.HasProject(v) {
return true return false
} }
} }
return false return true
}) })
} }
if len(tagFilters) > 0 { if len(tagFilters) > 0 {
allFilters = append(allFilters, func(t timertxt.Timer) bool { allFilters = append(allFilters, func(t timertxt.Timer) bool {
for _, v := range tagFilters { for _, v := range tagFilters {
pts := strings.Split(v, ":") pts := strings.Split(v, ":")
if len(pts) == 2 && t.HasTag(pts[0]) && t.GetTag(pts[0]) == pts[1] { if len(pts) < 2 || !t.HasTag(pts[0]) || t.GetTag(pts[0]) != pts[1] {
return true 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 { doFilters := func(t *timertxt.Timer) bool {