mirror of
https://github.com/cheat/cheat.git
synced 2024-11-16 17:08:29 +01:00
a6c25d4b9c
- Deprecates the `Match` struct - Applies syntax highlighting to search results output in a manner consistent with the 'View' output - Refactors search to move colorization functionality outside of its concern
29 lines
652 B
Go
29 lines
652 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 := []string{}
|
|
|
|
// 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") {
|
|
|
|
// exit early if the line doesn't match the regex
|
|
if !reg.MatchString(line) {
|
|
continue
|
|
}
|
|
|
|
// record the match
|
|
matches = append(matches, line)
|
|
}
|
|
|
|
return strings.Join(matches, "\n")
|
|
}
|