From 732dc8a41cb907ebd80f75058d66d6b82c279ef9 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 1 Nov 2021 21:48:11 +0900 Subject: [PATCH] If the match strings overlap, set the same coloring as `git grep` --- scanner/scanner.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/scanner/scanner.go b/scanner/scanner.go index 65c95f1..1342a25 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -62,14 +62,26 @@ func Scan(ctx context.Context, fsys fs.FS, w io.Writer, opts *Opts) error { } sort.Slice(matches, func(i, j int) bool { - return matches[i][0] > matches[j][0] + return matches[i][0] < matches[j][0] }) + // TODO: refactor + i := 0 f := [][]int{} - for i := range matches { - if i+1 == len(matches) || matches[i][1] < matches[i+1][0] { + for { + if i+1 == len(matches) { f = append(f, matches[i]) + break + } else if i+1 > len(matches) { + break } + if matches[i][1] < matches[i+1][0] { + f = append(f, matches[i]) + } else if matches[i][1] >= matches[i+1][0] { + f = append(f, matches[i]) + i += 1 + } + i += 1 } matches = f