mirror of https://github.com/cheat/cheat.git
feat: implements filter on `-l`
Implements filtering by pattern with `-l`. Resolves #504.
This commit is contained in:
parent
e94a1e22df
commit
daa43d3867
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
@ -45,6 +46,39 @@ func cmdList(opts map[string]interface{}, conf config.Config) {
|
||||||
return flattened[i].Title < flattened[j].Title
|
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
|
// exit early if no cheatsheets are available
|
||||||
if len(flattened) == 0 {
|
if len(flattened) == 0 {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
|
@ -35,6 +35,9 @@ Examples:
|
||||||
To list all available cheatsheets:
|
To list all available cheatsheets:
|
||||||
cheat -l
|
cheat -l
|
||||||
|
|
||||||
|
To list all cheatsheets whose titles match "apt":
|
||||||
|
cheat -l apt
|
||||||
|
|
||||||
To list all tags in use:
|
To list all tags in use:
|
||||||
cheat -T
|
cheat -T
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,9 @@ Examples:
|
||||||
To list all available cheatsheets:
|
To list all available cheatsheets:
|
||||||
cheat -l
|
cheat -l
|
||||||
|
|
||||||
|
To list all cheatsheets whose titles match "apt":
|
||||||
|
cheat -l apt
|
||||||
|
|
||||||
To list all tags in use:
|
To list all tags in use:
|
||||||
cheat -T
|
cheat -T
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue