mirror of
https://github.com/cheat/cheat.git
synced 2025-07-11 16:21:58 +02:00
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:
@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/display"
|
"github.com/cheat/cheat/internal/display"
|
||||||
"github.com/cheat/cheat/internal/sheet"
|
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"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
|
// iterate over each cheatpath
|
||||||
// `title` => `sheet` (ie, allow more local cheatsheets to override less
|
out := ""
|
||||||
// local cheatsheets)
|
for _, pathcheats := range 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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort the cheatsheets alphabetically, and search for matches
|
// sort the cheatsheets alphabetically, and search for matches
|
||||||
out := ""
|
for _, sheet := range sheets.Sort(pathcheats) {
|
||||||
for _, sheet := range sheets.Sort(consolidated) {
|
|
||||||
|
// 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>
|
// assume that we want to perform a case-insensitive search for <phrase>
|
||||||
pattern := "(?i)" + phrase
|
pattern := "(?i)" + phrase
|
||||||
@ -97,6 +83,10 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
// indent each line of content
|
// indent each line of content
|
||||||
out += display.Indent(sheet.Text) + "\n"
|
out += display.Indent(sheet.Text) + "\n"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// trim the leading newline
|
||||||
|
out = strings.TrimPrefix(out, "\n")
|
||||||
|
|
||||||
// display the output
|
// display the output
|
||||||
// NB: resist the temptation to call `display.Display` multiple times in
|
// NB: resist the temptation to call `display.Display` multiple times in
|
||||||
|
Reference in New Issue
Block a user