From cc0098b558b30831b1c5f54edc1ac4570eb4e758 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 22 Oct 2024 21:02:09 -0400 Subject: [PATCH] Print matched content in color --- go.mod | 9 ++++++++- go.sum | 11 +++++++++++ main.go | 21 +++++++++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index a9f775e..b422ea0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ -module re +module re go 1.23.1 + +require ( + github.com/fatih/color v1.18.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + golang.org/x/sys v0.25.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..33148a4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= +github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/main.go b/main.go index b802b15..6dd9a9e 100644 --- a/main.go +++ b/main.go @@ -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]) } }