Print matched content in color

This commit is contained in:
2024-10-22 21:02:09 -04:00
parent bc11777ad5
commit cc0098b558
3 changed files with 34 additions and 7 deletions

21
main.go
View File

@@ -4,6 +4,8 @@ import (
"fmt"
"os"
"slices"
"github.com/fatih/color"
)
const CONCAT rune = '~'
@@ -44,7 +46,7 @@ func shuntingYard(re string) string {
}
}
fmt.Println(string(re_postfix))
// fmt.Println(string(re_postfix))
opStack := make([]rune, 0) // Operator stack
outQueue := make([]rune, 0) // Output queue
@@ -165,12 +167,19 @@ func main() {
var re string
re = os.Args[1]
re_postfix := shuntingYard(re)
fmt.Println(re_postfix)
start := thompson(re_postfix)
s, e, matched := match(start, os.Args[2])
// fmt.Println(re_postfix)
startState := thompson(re_postfix)
start, end, matched := match(startState, os.Args[2])
if matched {
fmt.Printf("Matched from %d to %d\n", s, e)
for i, c := range os.Args[2] {
if i >= start && i < end {
color.New(color.FgRed).Printf("%c", c)
} else {
fmt.Printf("%c", c)
}
}
fmt.Printf("\n")
} else {
fmt.Printf("No match found.\n")
fmt.Println(os.Args[2])
}
}