feat(search): search all cheatpaths

Update the search function. It now searches all cheatpaths all the time,
as if `--all` were implicitly passed.
This commit is contained in:
Chris Lane 2020-11-27 22:31:16 -05:00
parent a58294859e
commit 4ef4c35d8c
1 changed files with 51 additions and 61 deletions

View File

@ -8,7 +8,6 @@ import (
"github.com/cheat/cheat/internal/config"
"github.com/cheat/cheat/internal/display"
"github.com/cheat/cheat/internal/sheet"
"github.com/cheat/cheat/internal/sheets"
)
@ -32,31 +31,18 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
)
}
// consolidate the cheatsheets found on all paths into a single map of
// `title` => `sheet` (ie, allow more local cheatsheets to override less
// local cheatsheets)
consolidated := sheets.Consolidate(cheatsheets)
// if <cheatsheet> was provided, search that single sheet only
if opts["<cheatsheet>"] != nil {
cheatsheet := opts["<cheatsheet>"].(string)
// assert that the cheatsheet exists
s, ok := consolidated[cheatsheet]
if !ok {
fmt.Printf("No cheatsheet found for '%s'.\n", cheatsheet)
os.Exit(2)
}
consolidated = map[string]sheet.Sheet{
cheatsheet: s,
}
}
// iterate over each cheatpath
out := ""
for _, pathcheats := range cheatsheets {
// sort the cheatsheets alphabetically, and search for matches
out := ""
for _, sheet := range sheets.Sort(consolidated) {
for _, sheet := range sheets.Sort(pathcheats) {
// if <cheatsheet> was provided, constrain the search only to
// matching cheatsheets
if opts["<cheatsheet>"] != nil && sheet.Title != opts["<cheatsheet>"] {
continue
}
// assume that we want to perform a case-insensitive search for <phrase>
pattern := "(?i)" + phrase
@ -97,6 +83,10 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
// indent each line of content
out += display.Indent(sheet.Text) + "\n"
}
}
// trim the leading newline
out = strings.TrimPrefix(out, "\n")
// display the output
// NB: resist the temptation to call `display.Display` multiple times in