mirror of https://github.com/cheat/cheat.git
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:
parent
a6c25d4b9c
commit
bc623da74b
|
@ -9,21 +9,16 @@ import (
|
||||||
func (s *Sheet) Search(reg *regexp.Regexp) string {
|
func (s *Sheet) Search(reg *regexp.Regexp) string {
|
||||||
|
|
||||||
// record matches
|
// record matches
|
||||||
matches := []string{}
|
matches := ""
|
||||||
|
|
||||||
// search through the cheatsheet's text line by line
|
// search through the cheatsheet's text line by line
|
||||||
// TODO: searching line-by-line is surely the "naive" approach. Revisit this
|
for _, line := range strings.Split(s.Text, "\n\n") {
|
||||||
// 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
|
// exit early if the line doesn't match the regex
|
||||||
if !reg.MatchString(line) {
|
if reg.MatchString(line) {
|
||||||
continue
|
matches += line + "\n\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
// record the match
|
|
||||||
matches = append(matches, line)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(matches, "\n")
|
return strings.TrimSpace(matches)
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ func TestSearchSingleMatch(t *testing.T) {
|
||||||
|
|
||||||
// mock a cheatsheet
|
// mock a cheatsheet
|
||||||
sheet := Sheet{
|
sheet := Sheet{
|
||||||
Text: "The quick brown fox\njumped over\nthe lazy dog.",
|
Text: "The quick brown fox\njumped over\n\nthe lazy dog.",
|
||||||
}
|
}
|
||||||
|
|
||||||
// compile the search regex
|
// compile the search regex
|
||||||
|
@ -49,7 +49,7 @@ func TestSearchSingleMatch(t *testing.T) {
|
||||||
matches := sheet.Search(reg)
|
matches := sheet.Search(reg)
|
||||||
|
|
||||||
// specify the expected results
|
// specify the expected results
|
||||||
want := "The quick brown fox"
|
want := "The quick brown fox\njumped over"
|
||||||
|
|
||||||
// assert that the correct matches were returned
|
// assert that the correct matches were returned
|
||||||
if matches != want {
|
if matches != want {
|
||||||
|
@ -67,7 +67,7 @@ func TestSearchMultiMatch(t *testing.T) {
|
||||||
|
|
||||||
// mock a cheatsheet
|
// mock a cheatsheet
|
||||||
sheet := Sheet{
|
sheet := Sheet{
|
||||||
Text: "The quick brown fox\njumped over\nthe lazy dog.",
|
Text: "The quick brown fox\n\njumped over\n\nthe lazy dog.",
|
||||||
}
|
}
|
||||||
|
|
||||||
// compile the search regex
|
// compile the search regex
|
||||||
|
@ -80,7 +80,7 @@ func TestSearchMultiMatch(t *testing.T) {
|
||||||
matches := sheet.Search(reg)
|
matches := sheet.Search(reg)
|
||||||
|
|
||||||
// specify the expected results
|
// specify the expected results
|
||||||
want := "The quick brown fox\nthe lazy dog."
|
want := "The quick brown fox\n\nthe lazy dog."
|
||||||
|
|
||||||
// assert that the correct matches were returned
|
// assert that the correct matches were returned
|
||||||
if !reflect.DeepEqual(matches, want) {
|
if !reflect.DeepEqual(matches, want) {
|
||||||
|
|
Loading…
Reference in New Issue