From 0bdf85ca96bf7e86941fade16ecf74b2c3b05199 Mon Sep 17 00:00:00 2001 From: lolilolicon Date: Sat, 10 Sep 2016 04:46:56 +0800 Subject: [PATCH] search: filter results by last updated date (#16) Select apps updated today (UTC): fdroidcl search -d 1 Select apps NOT updated since a year ago: fdroidcl search -d -365 The default value `-d 0` disables this filter. Fixes #13 --- cmd/fdroidcl/search.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/fdroidcl/search.go b/cmd/fdroidcl/search.go index 6b98cb6..fe6eac9 100644 --- a/cmd/fdroidcl/search.go +++ b/cmd/fdroidcl/search.go @@ -10,6 +10,7 @@ import ( "regexp" "sort" "strings" + "time" "github.com/mvdan/fdroidcl" "github.com/mvdan/fdroidcl/adb" @@ -24,6 +25,7 @@ var ( quiet = cmdSearch.Flag.Bool("q", false, "Print package names only") installed = cmdSearch.Flag.Bool("i", false, "Filter installed apps") updates = cmdSearch.Flag.Bool("u", false, "Filter apps with updates") + days = cmdSearch.Flag.Int("d", 0, "Select apps last updated in the last days; a negative value drops them instead") category = cmdSearch.Flag.String("c", "", "Filter apps by category") sortBy = cmdSearch.Flag.String("o", "", "Sort order (added, updated)") ) @@ -56,6 +58,9 @@ func runSearch(args []string) { if len(apps) > 0 && *updates { apps = filterAppsUpdates(apps, device) } + if len(apps) > 0 && *days != 0 { + apps = filterAppsLastUpdated(apps, *days) + } if len(apps) > 0 && *category != "" { apps = filterAppsCategory(apps, *category) if apps == nil { @@ -190,6 +195,23 @@ func filterAppsUpdates(apps []fdroidcl.App, device *adb.Device) []fdroidcl.App { return result } +func filterAppsLastUpdated(apps []fdroidcl.App, days int) []fdroidcl.App { + var result []fdroidcl.App + newer := true + if days < 0 { + days = -days + newer = false + } + date := time.Now().Truncate(24 * time.Hour).AddDate(0, 0, 1 - days) + for _, app := range apps { + if app.Updated.Before(date) == newer { + continue + } + result = append(result, app) + } + return result +} + func contains(l []string, s string) bool { for _, s1 := range l { if s1 == s {