mirror of https://github.com/daffainfo/nuclei.git
Highlight the longest occurrences when there are matches that are substrings of each otherdev
parent
883cb22c39
commit
09eba6c0cf
|
@ -1,6 +1,7 @@
|
|||
package responsehighlighter
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -14,18 +15,14 @@ var colorFunction = aurora.Green
|
|||
func Highlight(operatorResult *operators.Result, response string, noColor, hexDump bool) string {
|
||||
result := response
|
||||
if operatorResult != nil && !noColor {
|
||||
for _, matches := range operatorResult.Matches {
|
||||
if len(matches) > 0 {
|
||||
for _, currentMatch := range matches {
|
||||
if hexDump {
|
||||
highlightedHexDump, err := toHighLightedHexDump(result, currentMatch)
|
||||
if err == nil {
|
||||
result = highlightedHexDump.String()
|
||||
}
|
||||
} else {
|
||||
result = strings.ReplaceAll(result, currentMatch, addColor(currentMatch))
|
||||
}
|
||||
for _, currentMatch := range getSortedMatches(operatorResult) {
|
||||
if hexDump {
|
||||
highlightedHexDump, err := toHighLightedHexDump(result, currentMatch)
|
||||
if err == nil {
|
||||
result = highlightedHexDump.String()
|
||||
}
|
||||
} else {
|
||||
result = highlightASCII(currentMatch, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +30,27 @@ func Highlight(operatorResult *operators.Result, response string, noColor, hexDu
|
|||
return result
|
||||
}
|
||||
|
||||
func highlightASCII(currentMatch string, result string) string {
|
||||
var coloredMatchBuilder strings.Builder
|
||||
for _, char := range currentMatch {
|
||||
coloredMatchBuilder.WriteString(addColor(string(char)))
|
||||
}
|
||||
|
||||
return strings.ReplaceAll(result, currentMatch, coloredMatchBuilder.String())
|
||||
}
|
||||
|
||||
func getSortedMatches(operatorResult *operators.Result) []string {
|
||||
sortedMatches := make([]string, 0, len(operatorResult.Matches))
|
||||
for _, matches := range operatorResult.Matches {
|
||||
sortedMatches = append(sortedMatches, matches...)
|
||||
}
|
||||
|
||||
sort.Slice(sortedMatches, func(i, j int) bool {
|
||||
return len(sortedMatches[i]) > len(sortedMatches[j])
|
||||
})
|
||||
return sortedMatches
|
||||
}
|
||||
|
||||
func CreateStatusCodeSnippet(response string, statusCode int) string {
|
||||
if strings.HasPrefix(response, "HTTP/") {
|
||||
strStatusCode := strconv.Itoa(statusCode)
|
||||
|
|
|
@ -12,15 +12,16 @@ import (
|
|||
const input = "abcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmnabcdefghijklmn"
|
||||
|
||||
func TestHexDumpHighlighting(t *testing.T) {
|
||||
const highlightedHexDumpResponse = `00000000 61 62 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e 61 62 |abc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmnab|
|
||||
00000010 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e 61 62 63 [32m64[0m |c[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmnabc[32md[0m|
|
||||
00000020 [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e 61 62 63 [32m64[0m [32m65[0m [32m66[0m |[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmnabc[32md[0m[32me[0m[32mf[0m|
|
||||
00000030 [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e 61 62 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m |[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmnabc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m|
|
||||
00000040 [32m69[0m [32m6a[0m 6b 6c 6d 6e 61 62 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m |[32mi[0m[32mj[0mklmnabc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0m|
|
||||
00000050 6b 6c 6d 6e 61 62 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c |klmnabc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mkl|
|
||||
00000060 6d 6e 61 62 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e |mnabc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn|
|
||||
00000070 61 62 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e |abc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn|
|
||||
`
|
||||
highlightedHexDumpResponse :=
|
||||
"00000000 61 62 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e 61 62 |abc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmnab|\n" +
|
||||
"00000010 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e 61 62 63 \x1b[32m64\x1b[0m |c\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmnabc\x1b[32md\x1b[0m|\n" +
|
||||
"00000020 \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e 61 62 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m |\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmnabc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m|\n" +
|
||||
"00000030 \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e 61 62 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m |\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmnabc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m|\n" +
|
||||
"00000040 \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e 61 62 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m |\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmnabc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0m|\n" +
|
||||
"00000050 6b 6c 6d 6e 61 62 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c |klmnabc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mkl|\n" +
|
||||
"00000060 6d 6e 61 62 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e |mnabc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn|\n" +
|
||||
"00000070 61 62 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e |abc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn|\n"
|
||||
|
||||
t.Run("Test highlighting when the snippet is wrapped", func(t *testing.T) {
|
||||
result, err := toHighLightedHexDump(hex.Dump([]byte(input)), "defghij")
|
||||
assert.Nil(t, err)
|
||||
|
@ -31,24 +32,25 @@ func TestHexDumpHighlighting(t *testing.T) {
|
|||
value := "asdfasdfasda|basdfadsdfs|"
|
||||
result, err := toHighLightedHexDump(hex.Dump([]byte(value)), "a|b")
|
||||
|
||||
expected := `00000000 61 73 64 66 61 73 64 66 61 73 64 [32m61[0m [32m7c[0m [32m62[0m 61 73 |asdfasdfasd[32ma[0m[32m|[0m[32mb[0mas|
|
||||
00000010 64 66 61 64 73 64 66 73 7c |dfadsdfs||
|
||||
`
|
||||
expected :=
|
||||
"00000000 61 73 64 66 61 73 64 66 61 73 64 \x1b[32m61\x1b[0m \x1b[32m7c\x1b[0m \x1b[32m62\x1b[0m 61 73 |asdfasdfasd\x1b[32ma\x1b[0m\x1b[32m|\x1b[0m\x1b[32mb\x1b[0mas|\n" +
|
||||
"00000010 64 66 61 64 73 64 66 73 7c |dfadsdfs||\n"
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, expected, result.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestHighlight(t *testing.T) {
|
||||
const multiSnippetHighlightHexDumpResponse = `00000000 [32m61[0m [32m62[0m 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e [32m61[0m [32m62[0m |[32ma[0m[32mb[0mc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn[32ma[0m[32mb[0m|
|
||||
00000010 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e [32m61[0m [32m62[0m 63 [32m64[0m |c[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn[32ma[0m[32mb[0mc[32md[0m|
|
||||
00000020 [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e [32m61[0m [32m62[0m 63 [32m64[0m [32m65[0m [32m66[0m |[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn[32ma[0m[32mb[0mc[32md[0m[32me[0m[32mf[0m|
|
||||
00000030 [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e [32m61[0m [32m62[0m 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m |[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn[32ma[0m[32mb[0mc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m|
|
||||
00000040 [32m69[0m [32m6a[0m 6b 6c 6d 6e [32m61[0m [32m62[0m 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m |[32mi[0m[32mj[0mklmn[32ma[0m[32mb[0mc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0m|
|
||||
00000050 6b 6c 6d 6e [32m61[0m [32m62[0m 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c |klmn[32ma[0m[32mb[0mc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mkl|
|
||||
00000060 6d 6e [32m61[0m [32m62[0m 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e |mn[32ma[0m[32mb[0mc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn|
|
||||
00000070 [32m61[0m [32m62[0m 63 [32m64[0m [32m65[0m [32m66[0m [32m67[0m [32m68[0m [32m69[0m [32m6a[0m 6b 6c 6d 6e |[32ma[0m[32mb[0mc[32md[0m[32me[0m[32mf[0m[32mg[0m[32mh[0m[32mi[0m[32mj[0mklmn|
|
||||
`
|
||||
const multiSnippetHighlightHexDumpResponse = "00000000 \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m |\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32ma\x1b[0m\x1b[32mb\x1b[0m|\n" +
|
||||
"00000010 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m |c\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m|\n" +
|
||||
"00000020 \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m |\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m|\n" +
|
||||
"00000030 \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m |\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m|\n" +
|
||||
"00000040 \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m |\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0m|\n" +
|
||||
"00000050 6b 6c 6d 6e \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c |klmn\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mkl|\n" +
|
||||
"00000060 6d 6e \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e |mn\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn|\n" +
|
||||
"00000070 \x1b[32m61\x1b[0m \x1b[32m62\x1b[0m 63 \x1b[32m64\x1b[0m \x1b[32m65\x1b[0m \x1b[32m66\x1b[0m \x1b[32m67\x1b[0m \x1b[32m68\x1b[0m \x1b[32m69\x1b[0m \x1b[32m6a\x1b[0m 6b 6c 6d 6e |\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn|\n"
|
||||
|
||||
matches := map[string][]string{
|
||||
"first": {"defghij"},
|
||||
"second": {"ab"},
|
||||
|
@ -62,7 +64,17 @@ func TestHighlight(t *testing.T) {
|
|||
|
||||
t.Run("Test highlighting without hexdump", func(t *testing.T) {
|
||||
result := Highlight(&operatorResult, input, false, false)
|
||||
expected := `[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn[32mab[0mc[32mdefghij[0mklmn`
|
||||
expected :=
|
||||
"\x1b[32ma\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn\x1b[32m" +
|
||||
"a\x1b[0m\x1b[32mb\x1b[0mc\x1b[32md\x1b[0m\x1b[32me\x1b[0m\x1b[32mf\x1b[0m\x1b[32mg\x1b[0m\x1b[32mh\x1b[0m\x1b[32mi\x1b[0m\x1b[32mj\x1b[0mklmn"
|
||||
print(result)
|
||||
assert.Equal(t, expected, result)
|
||||
})
|
||||
|
||||
|
@ -76,3 +88,24 @@ func TestHighlight(t *testing.T) {
|
|||
assert.Equal(t, hex.Dump([]byte(input)), result)
|
||||
})
|
||||
}
|
||||
|
||||
func TestMultiSubstringMatchHighlight(t *testing.T) {
|
||||
const input = `
|
||||
start ValueToMatch end
|
||||
start ValueToMatch-1.2.3 end
|
||||
start ValueToMatch-2.1 end
|
||||
`
|
||||
matches := map[string][]string{
|
||||
"first": {"ValueToMatch"},
|
||||
"second": {"ValueToMatch-1.2.3"},
|
||||
"third": {"ValueToMatch-2.1"},
|
||||
}
|
||||
operatorResult := operators.Result{Matches: matches}
|
||||
|
||||
expected :=
|
||||
"\nstart \x1b[32mV\x1b[0m\x1b[32ma\x1b[0m\x1b[32ml\x1b[0m\x1b[32mu\x1b[0m\x1b[32me\x1b[0m\x1b[32mT\x1b[0m\x1b[32mo\x1b[0m\x1b[32mM\x1b[0m\x1b[32ma\x1b[0m\x1b[32mt\x1b[0m\x1b[32mc\x1b[0m\x1b[32mh\x1b[0m end\n" +
|
||||
"start \x1b[32mV\x1b[0m\x1b[32ma\x1b[0m\x1b[32ml\x1b[0m\x1b[32mu\x1b[0m\x1b[32me\x1b[0m\x1b[32mT\x1b[0m\x1b[32mo\x1b[0m\x1b[32mM\x1b[0m\x1b[32ma\x1b[0m\x1b[32mt\x1b[0m\x1b[32mc\x1b[0m\x1b[32mh\x1b[0m\x1b[32m-\x1b[0m\x1b[32m1\x1b[0m\x1b[32m.\x1b[0m\x1b[32m2\x1b[0m\x1b[32m.\x1b[0m\x1b[32m3\x1b[0m end\n" +
|
||||
"start \x1b[32mV\x1b[0m\x1b[32ma\x1b[0m\x1b[32ml\x1b[0m\x1b[32mu\x1b[0m\x1b[32me\x1b[0m\x1b[32mT\x1b[0m\x1b[32mo\x1b[0m\x1b[32mM\x1b[0m\x1b[32ma\x1b[0m\x1b[32mt\x1b[0m\x1b[32mc\x1b[0m\x1b[32mh\x1b[0m\x1b[32m-\x1b[0m\x1b[32m2\x1b[0m\x1b[32m.\x1b[0m\x1b[32m1\x1b[0m end \n"
|
||||
result := Highlight(&operatorResult, input, false, false)
|
||||
assert.Equal(t, expected, result)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue