|
|
|
@ -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 { |
|
|
|
|
|
|
|
|
|
if string(bytes[i]) == "\n" { |
|
|
|
|
line = word |
|
|
|
|
if strings.Contains(line, search) { |
|
|
|
|
found("%s", word) |
|
|
|
|
word = "" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
line = "" |
|
|
|
|
word = "" //reset line and word
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|