From 1096c4024f52490373893e7819c4510aa6cd9850 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 18 Jan 2018 14:24:17 -0600 Subject: [PATCH] Filter list by time & tags --- cmd/gime/main.go | 96 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 32 deletions(-) diff --git a/cmd/gime/main.go b/cmd/gime/main.go index aa670be..e5c5688 100644 --- a/cmd/gime/main.go +++ b/cmd/gime/main.go @@ -139,46 +139,78 @@ func cmdPrintHelp(args []string) int { func cmdPrintList(args []string) int { loadActiveAndRecentTimeEntries() + + useDefaultFilter := true + + var beg, end time.Time + searchTags := []string{} + + for _, opt := range args { + var tmpBeg, tmpEnd time.Time + + // Do our best to figure out what timers the user wants to list + var err error + if strings.Contains(opt, "-") { + useDefaultFilter = false + pts := strings.Split(opt, "-") + if len(pts[0]) > 0 { + // This should be the starting date + tmpBeg, err = parseFuzzyTime(pts[0]) + if err != nil { + // We couldn't parse it as a time, + // Probably this is just a tag + searchTags = append(searchTags, opt) + continue + } + } + if len(pts[1]) > 0 { + // This should be the ending date + tmpEnd, err = parseFuzzyTime(pts[1]) + if err != nil { + searchTags = append(searchTags, opt) + continue + } + } + } else { + // Tag filters + searchTags = append(searchTags, opt) + } + if !tmpBeg.IsZero() || !tmpEnd.IsZero() { + beg, end = tmpBeg, tmpEnd + } + } + if end.IsZero() { + end = time.Now() + } + // By default, list all entries ending today or still running - filter := func(t *gime.TimeEntry) bool { + defaultFilter := func(t *gime.TimeEntry) bool { return t.EndsToday() || t.IsRunning() } - /* - for _, opt := range args { - // Do our best to figure out what timers the user wants to list - var err error - var beg, end time.Time - if strings.Contains(opt, "-") { - pts := strings.Split(opt, "-") - if len(pts[0]) > 0 { - // This should be the starting date - if beg, err = parseFuzzyTime(pts[0]); err != nil { - fmt.Println("Start date:", err.Error()) - return 1 - } - } - if len(pts[1]) > 0 { - // This should be the ending date - if end, err = parseFuzzyTime(pts[1]); err != nil { - fmt.Println("End date:", err.Error()) - return 1 - } - } - if end.IsZero() { - end = time.Now() - } - } else if strings.HasPrefix(opt, "week") { - - } else if strings.HasPrefix(opt, "biweek") { + timeSpanFilter := func(t *gime.TimeEntry) bool { + return t.GetStart().After(beg) && t.GetEnd().Before(end) + } + tagFilter := func(t *gime.TimeEntry) bool { + for i := range searchTags { + if !t.HasTag(searchTags[i]) { + return false } - fmt.Println(beg, " - ", end) } - return 0 - */ + return true + } + + compoundFilter := func(t *gime.TimeEntry) bool { + // If we didn't get any other filter specifications, just use the default + if useDefaultFilter { + return defaultFilter(t) + } + // Otherwise we want to filter timespan and tags + return timeSpanFilter(t) && tagFilter(t) + } fmt.Println(time.Now().Format("2006/01/02")) - str := TimerCollectionToString(filterTimerCollection(timeEntries, filter)) + str := TimerCollectionToString(filterTimerCollection(timeEntries, compoundFilter)) str = " " + strings.Replace(str, "\n", "\n ", -1) fmt.Println(str) return 0