cheat/internal/sheet/search.go
Chris Lane bc623da74b 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.
2020-02-15 15:56:25 -05:00

25 lines
473 B
Go

package sheet
import (
"regexp"
"strings"
)
// Search returns lines within a sheet's Text that match the search regex
func (s *Sheet) Search(reg *regexp.Regexp) string {
// record matches
matches := ""
// search through the cheatsheet's text line by line
for _, line := range strings.Split(s.Text, "\n\n") {
// exit early if the line doesn't match the regex
if reg.MatchString(line) {
matches += line + "\n\n"
}
}
return strings.TrimSpace(matches)
}