|
|
@ -1,10 +1,10 @@ |
|
|
|
package main |
|
|
|
package main |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"flag" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/fatih/color" |
|
|
|
"github.com/fatih/color" |
|
|
|
) |
|
|
|
) |
|
|
@ -21,16 +21,8 @@ func usage() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
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() |
|
|
|
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 { |
|
|
|
if len(os.Args) <= 2 || len(os.Args) > 3 { |
|
|
|
usage() //print usage and then exit
|
|
|
|
usage() //print usage and then exit
|
|
|
|
os.Exit(1) |
|
|
|
os.Exit(1) |
|
|
@ -38,22 +30,23 @@ func main() { |
|
|
|
|
|
|
|
|
|
|
|
search := os.Args[1] |
|
|
|
search := os.Args[1] |
|
|
|
filename := os.Args[2] |
|
|
|
filename := os.Args[2] |
|
|
|
fmt.Printf("search: %s\nfilename: %s\n\n", search, filename) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(filename) |
|
|
|
bytes, err := ioutil.ReadFile(filename) |
|
|
|
check(err) |
|
|
|
check(err) |
|
|
|
|
|
|
|
|
|
|
|
word := "" |
|
|
|
word := "" |
|
|
|
//split []bytes into words by spaces
|
|
|
|
line := "" |
|
|
|
for i := 0; i < len(bytes); i++ { |
|
|
|
for i := 0; i < len(bytes); i++ { |
|
|
|
word += string(bytes[i]) |
|
|
|
word += string(bytes[i]) |
|
|
|
if string(bytes[i]) == " " { |
|
|
|
|
|
|
|
fmt.Print(word) |
|
|
|
if string(bytes[i]) == "\n" { |
|
|
|
word = "" |
|
|
|
line = word |
|
|
|
} |
|
|
|
if strings.Contains(line, search) { |
|
|
|
if word == search { |
|
|
|
found("%s", word) |
|
|
|
found("%s", word) |
|
|
|
} |
|
|
|
word = "" |
|
|
|
|
|
|
|
|
|
|
|
line = "" |
|
|
|
|
|
|
|
word = "" //reset line and word
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|