feat: issue #260

Dramatically improves the usefulness of `--search` by outputting
"chunked" results. This removes the need (usually) to search and then
manually open a cheatsheet.
This commit is contained in:
Chris Lane
2020-02-15 15:56:25 -05:00
parent a6c25d4b9c
commit bc623da74b
2 changed files with 9 additions and 14 deletions

View File

@@ -9,21 +9,16 @@ import (
func (s *Sheet) Search(reg *regexp.Regexp) string {
// record matches
matches := []string{}
matches := ""
// search through the cheatsheet's text line by line
// TODO: searching line-by-line is surely the "naive" approach. Revisit this
// later with an eye for performance improvements.
for _, line := range strings.Split(s.Text, "\n") {
for _, line := range strings.Split(s.Text, "\n\n") {
// exit early if the line doesn't match the regex
if !reg.MatchString(line) {
continue
if reg.MatchString(line) {
matches += line + "\n\n"
}
// record the match
matches = append(matches, line)
}
return strings.Join(matches, "\n")
return strings.TrimSpace(matches)
}