package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strings"

	"github.com/fatih/color"
)

func check(err error) {
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
}

func usage() {
	fmt.Printf("Usage:\n\t %s [text] [file]", os.Args[0])
}

func main() {
	found := color.New(color.FgRed).PrintfFunc()

	if len(os.Args) <= 2 || len(os.Args) > 3 {
		usage() //print usage and then exit
		os.Exit(1)
	}

	search := os.Args[1]
	filename := os.Args[2]

	bytes, err := ioutil.ReadFile(filename)
	check(err)

	word := ""
	line := ""
	for i := 0; i < len(bytes); i++ {
		word += string(bytes[i])

		if string(bytes[i]) == "\n" {
			line = word
			if strings.Contains(line, search) {
				found("%s", word)
			}

			line = ""
			word = "" //reset line and word
		}
	}
}
