mirror of
https://github.com/cheat/cheat.git
synced 2024-11-13 23:51:09 +01:00
feat: implements filter on -l
Implements filtering by pattern with `-l`. Resolves #504.
This commit is contained in:
parent
e94a1e22df
commit
daa43d3867
3 changed files with 40 additions and 0 deletions
|
@ -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 <cheatsheet> was specified
|
||||
// NB: our docopt specification is misleading here. When used in conjunction
|
||||
// with `-l`, `<cheatsheet>` is really a pattern against which to filter
|
||||
// sheet titles.
|
||||
if opts["<cheatsheet>"] != nil {
|
||||
|
||||
// initialize a slice of filtered sheets
|
||||
filtered := []sheet.Sheet{}
|
||||
|
||||
// initialize our filter pattern
|
||||
pattern := "(?i)" + opts["<cheatsheet>"].(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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue