diff --git a/go.mod b/go.mod index f05e692..af68781 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module go-linux-tools/go-grep go 1.20 -require github.com/fatih/color v1.15.0 +require ( + github.com/fatih/color v1.15.0 + golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 +) require ( github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index 2624c9d..3f8ef56 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 h1:/yRP+0AN7mf5DkD3BAI6TOFnd51gEoDEb8o35jIFtgw= +golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/gogrep b/gogrep index e07c589..57af82a 100755 Binary files a/gogrep and b/gogrep differ diff --git a/main.go b/main.go index 1622ec7..12c4c42 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,10 @@ package main import ( - "flag" "fmt" "io/ioutil" "os" + "strings" "github.com/fatih/color" ) @@ -21,16 +21,8 @@ func usage() { } func main() { - var flagNoColor = flag.Bool("no-colour", false, "Disable color output") - flag.Parse() - if *flagNoColor { - color.NoColor = true - } - - //set up colourful output found := color.New(color.FgRed).PrintfFunc() - //check no args passed (min. two args need to be passed) and no more than 3 if len(os.Args) <= 2 || len(os.Args) > 3 { usage() //print usage and then exit os.Exit(1) @@ -38,22 +30,23 @@ func main() { search := os.Args[1] filename := os.Args[2] - fmt.Printf("search: %s\nfilename: %s\n\n", search, filename) bytes, err := ioutil.ReadFile(filename) check(err) word := "" - //split []bytes into words by spaces + line := "" for i := 0; i < len(bytes); i++ { word += string(bytes[i]) - if string(bytes[i]) == " " { - fmt.Print(word) - word = "" - } - if word == search { - found("%s", word) - word = "" + + if string(bytes[i]) == "\n" { + line = word + if strings.Contains(line, search) { + found("%s", word) + } + + line = "" + word = "" //reset line and word } } }