2019-10-20 16:02:28 +02:00
|
|
|
package sheet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Search searches for regexp matches in a cheatsheet's text, and optionally
|
|
|
|
// colorizes matching strings.
|
2020-02-15 16:01:44 +01:00
|
|
|
func (s *Sheet) Search(reg *regexp.Regexp) []Match {
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// record matches
|
|
|
|
matches := []Match{}
|
|
|
|
|
|
|
|
// 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 linenum, line := range strings.Split(s.Text, "\n") {
|
|
|
|
|
|
|
|
// exit early if the line doesn't match the regex
|
|
|
|
if !reg.MatchString(line) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// init the match
|
|
|
|
m := Match{
|
|
|
|
Line: linenum + 1,
|
|
|
|
Text: strings.TrimSpace(line),
|
|
|
|
}
|
|
|
|
|
|
|
|
// record the match
|
|
|
|
matches = append(matches, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|