diff --git a/cmd/cheat/cmd_list.go b/cmd/cheat/cmd_list.go index 6ed59da..6f03318 100644 --- a/cmd/cheat/cmd_list.go +++ b/cmd/cheat/cmd_list.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "regexp" "sort" "strings" "text/tabwriter" @@ -45,6 +46,39 @@ func cmdList(opts map[string]interface{}, conf config.Config) { return flattened[i].Title < flattened[j].Title }) + // filter if was specified + // NB: our docopt specification is misleading here. When used in conjunction + // with `-l`, `` is really a pattern against which to filter + // sheet titles. + if opts[""] != nil { + + // initialize a slice of filtered sheets + filtered := []sheet.Sheet{} + + // initialize our filter pattern + pattern := "(?i)" + opts[""].(string) + + // compile the regex + reg, err := regexp.Compile(pattern) + if err != nil { + fmt.Fprintln( + os.Stderr, + fmt.Sprintf("failed to compile regexp: %s, %v", pattern, err), + ) + os.Exit(1) + } + + // iterate over each cheatsheet, and pass-through those which match the + // filter pattern + for _, s := range flattened { + if reg.MatchString(s.Title) { + filtered = append(filtered, s) + } + } + + flattened = filtered + } + // exit early if no cheatsheets are available if len(flattened) == 0 { os.Exit(0) diff --git a/cmd/cheat/docopt.txt b/cmd/cheat/docopt.txt index 609f209..5877aab 100644 --- a/cmd/cheat/docopt.txt +++ b/cmd/cheat/docopt.txt @@ -35,6 +35,9 @@ Examples: To list all available cheatsheets: cheat -l + To list all cheatsheets whose titles match "apt": + cheat -l apt + To list all tags in use: cheat -T diff --git a/cmd/cheat/str_usage.go b/cmd/cheat/str_usage.go index dfcc96c..49d7678 100644 --- a/cmd/cheat/str_usage.go +++ b/cmd/cheat/str_usage.go @@ -44,6 +44,9 @@ Examples: To list all available cheatsheets: cheat -l + To list all cheatsheets whose titles match "apt": + cheat -l apt + To list all tags in use: cheat -T