From a0f8e2217fae4b873ff7a5870440123e0770090c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 11 Apr 2015 00:15:27 +0200 Subject: [PATCH] Add searching like the client --- README.md | 2 +- fdroidcl.go | 46 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0251f63..283f152 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ makes sense such as `update`, `show`, `install` and `remove`. * Single repo support * Update the index * List all apps + * Search by keywords * Show details of an app ### Missing features - * Searching * Multi-repo support * Interaction with a device - Probably via the command `adb` diff --git a/fdroidcl.go b/fdroidcl.go index 68d2e36..299e454 100644 --- a/fdroidcl.go +++ b/fdroidcl.go @@ -14,6 +14,7 @@ import ( "net/http" "os" "sort" + "strings" ) type Repo struct { @@ -91,7 +92,7 @@ func (app *App) WriteDetailed(w io.Writer) { if app.Tracker != "" { p("Tracker :", "%s", app.Tracker) } - // p("Description :", "%s", app.Desc) TODO: html, 80 column wrapping + // p("Description :", "%s", app.Desc) // TODO: parse html, 80 column wrapping fmt.Println() p("Available Versions :", "") for _, apk := range app.Apks { @@ -158,6 +159,36 @@ func loadApps() map[string]App { return apps } +func appMatches(fields []string, terms []string) bool { + for _, field := range fields { + for _, term := range terms { + if !strings.Contains(field, term) { + goto next + } + } + return true + next: + } + return false +} + +func filterAppsSearch(apps *map[string]App, terms []string) { + for _, term := range terms { + term = strings.ToLower(term) + } + for appID, app := range *apps { + fields := []string{ + strings.ToLower(app.ID), + strings.ToLower(app.Name), + strings.ToLower(app.Summary), + // strings.ToLower(app.Desc), // TODO remove html + } + if !appMatches(fields, terms) { + delete(*apps, appID) + } + } +} + type AppList []App func (al AppList) Len() int { return len(al) } @@ -180,9 +211,10 @@ func init() { fmt.Fprintln(os.Stderr, "Usage: fdroidcl [-h] []") fmt.Fprintln(os.Stderr) fmt.Fprintln(os.Stderr, "Available commands:") - fmt.Fprintln(os.Stderr, " update Updates the index") - fmt.Fprintln(os.Stderr, " list lists all available apps") - fmt.Fprintln(os.Stderr, " show Shows detailed info of an app") + fmt.Fprintln(os.Stderr, " update Update the index") + fmt.Fprintln(os.Stderr, " list List all available apps") + fmt.Fprintln(os.Stderr, " search Search available apps") + fmt.Fprintln(os.Stderr, " show Show detailed info of an app") } } @@ -204,6 +236,12 @@ func main() { for _, app := range sortedApps(apps) { app.WriteShort(os.Stdout) } + case "search": + apps := loadApps() + filterAppsSearch(&apps, args) + for _, app := range sortedApps(apps) { + app.WriteShort(os.Stdout) + } case "show": apps := loadApps() for _, appID := range args {