-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
experimental: show context of unfiltered lines
- Loading branch information
Showing
4 changed files
with
239 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package parse | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type context struct { | ||
Before, After int | ||
|
||
lines []string | ||
printAfter int | ||
line int | ||
lastPrint int | ||
} | ||
|
||
func (c *context) Print(buf *bytes.Buffer, msg string, selected bool) { | ||
c.line++ | ||
if selected { | ||
c.printAfter = c.After | ||
if c.lastPrint != 0 && (c.After != 0 || c.Before != 0) && c.line-len(c.lines)-c.lastPrint > 1 { | ||
buf.WriteString("---\n") | ||
} | ||
for _, l := range c.lines { | ||
buf.WriteString(l) | ||
} | ||
buf.WriteString(msg) | ||
c.lastPrint = c.line | ||
c.lines = nil | ||
return | ||
} | ||
|
||
if c.printAfter > 0 { | ||
buf.WriteString(msg) | ||
c.lastPrint = c.line | ||
c.printAfter-- | ||
return | ||
} | ||
|
||
if c.Before > 0 { | ||
c.lines = append(c.lines, msg) | ||
if len(c.lines) > c.Before { | ||
c.lines = c.lines[1:] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package parse | ||
|
||
import ( | ||
"bytes" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
) | ||
|
||
func TestContext(t *testing.T) { | ||
testData := []struct { | ||
name string | ||
before, after int | ||
match *regexp.Regexp | ||
input []string | ||
want []string | ||
}{ | ||
{ | ||
name: "select none", | ||
before: 2, | ||
after: 2, | ||
match: regexp.MustCompile(`^never matches$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "select all", | ||
before: 2, | ||
after: 2, | ||
match: regexp.MustCompile(`^.*$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
}, | ||
{ | ||
name: "no context, single match", | ||
before: 0, | ||
after: 0, | ||
match: regexp.MustCompile(`^5$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"5"}, | ||
}, | ||
{ | ||
name: "no context, two matches", | ||
before: 0, | ||
after: 0, | ||
match: regexp.MustCompile(`^5|8$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"5", "8"}, | ||
}, | ||
{ | ||
name: "no context, two contiguous matches", | ||
before: 0, | ||
after: 0, | ||
match: regexp.MustCompile(`^5|6$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"5", "6"}, | ||
}, | ||
{ | ||
name: "basic context, single match", | ||
before: 2, | ||
after: 2, | ||
match: regexp.MustCompile(`^5$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"3", "4", "5", "6", "7"}, | ||
}, | ||
{ | ||
name: "basic context, single match, at start", | ||
before: 2, | ||
after: 2, | ||
match: regexp.MustCompile(`^1$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"1", "2", "3"}, | ||
}, | ||
{ | ||
name: "basic context, single match, at second element", | ||
before: 2, | ||
after: 2, | ||
match: regexp.MustCompile(`^2$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"1", "2", "3", "4"}, | ||
}, | ||
{ | ||
name: "basic context, separated match regions", | ||
before: 1, | ||
after: 1, | ||
match: regexp.MustCompile(`^5|9$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"4", "5", "6", "---", "8", "9", "10"}, | ||
}, | ||
{ | ||
name: "basic context, contiguous match regions", | ||
before: 1, | ||
after: 1, | ||
match: regexp.MustCompile(`^5|8$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"4", "5", "6", "7", "8", "9"}, | ||
}, | ||
{ | ||
name: "after context, separated match regions", | ||
before: 0, | ||
after: 2, | ||
match: regexp.MustCompile(`^2|8$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"2", "3", "4", "---", "8", "9", "10"}, | ||
}, | ||
{ | ||
name: "after context, contiguous match regions", | ||
before: 0, | ||
after: 2, | ||
match: regexp.MustCompile(`^2|3$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"2", "3", "4", "5"}, | ||
}, | ||
{ | ||
name: "before context, separated match regions", | ||
before: 2, | ||
after: 0, | ||
match: regexp.MustCompile(`^4|9$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"2", "3", "4", "---", "7", "8", "9"}, | ||
}, | ||
{ | ||
name: "before context, contiguous match regions", | ||
before: 2, | ||
after: 0, | ||
match: regexp.MustCompile(`^5|8$`), | ||
input: []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, | ||
want: []string{"3", "4", "5", "6", "7", "8"}, | ||
}, | ||
} | ||
|
||
for _, test := range testData { | ||
t.Run(test.name, func(t *testing.T) { | ||
ctx := &context{ | ||
Before: test.before, | ||
After: test.after, | ||
} | ||
out := new(bytes.Buffer) | ||
for _, l := range test.input { | ||
selected := test.match.MatchString(l) | ||
ctx.Print(out, l+"\n", selected) | ||
} | ||
|
||
gotOutput := out.String() | ||
var got []string | ||
if len(gotOutput) > 0 { | ||
got = strings.Split(gotOutput, "\n") | ||
} | ||
if len(got) > 0 && got[len(got)-1] == "" { | ||
got = got[:len(got)-1] | ||
} | ||
if diff := cmp.Diff(got, test.want, cmpopts.EquateEmpty()); diff != "" { | ||
t.Errorf("diff:\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters