mirror of
https://github.com/cheat/cheat.git
synced 2024-11-25 15:31:36 +01:00
bc623da74b
Dramatically improves the usefulness of `--search` by outputting "chunked" results. This removes the need (usually) to search and then manually open a cheatsheet.
25 lines
473 B
Go
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)
|
|
}
|