Skip to content

Commit

Permalink
support for reading from file rather than stdin
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Couture-Beil <alex@mofo.ca>
  • Loading branch information
alexcb committed Nov 23, 2022
1 parent 781e771 commit fa9bd85
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
run:
timeout: 10m
130 changes: 92 additions & 38 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"
"regexp"
"sort"
"strings"
)

type pattern struct {
Expand All @@ -17,6 +19,55 @@ func die(msg string, args ...interface{}) {
os.Exit(1)
}

const nc = "\033[0m"

var (
colors map[string]string
defaultColors []string
colorOptions string
)

func init() {
colors = map[string]string{
"red": "\033[0;31m",
"green": "\033[0;32m",
"orange": "\033[0;33m",
"blue": "\033[0;34m",
"purple": "\033[0;35m",
"cyan": "\033[0;36m",
"lightgray": "\033[0;37m",
"darkgray": "\033[1;30m",
"lightred": "\033[1;31m",
"lightgreen": "\033[1;32m",
"yellow": "\033[1;33m",
"lightblue": "\033[1;34m",
"lightpurple": "\033[1;35m",
"lightcyan": "\033[1;36m",
}
l := []string{}
for c := range colors {
l = append(l, c)
}
sort.Strings(l)
colorOptions = strings.Join(l, ", ")

defaultColors = []string{
colors["red"],
colors["green"],
colors["orange"],
colors["blue"],
colors["purple"],
colors["cyan"],
colors["yellow"],
colors["lightred"],
colors["lightgreen"],
colors["lightblue"],
colors["lightpurple"],
colors["lightcyan"],
}

}

func main() {
progName := "colorgrep"
if len(os.Args) > 0 {
Expand All @@ -29,6 +80,9 @@ func main() {
wordBoundary := false
showHelp := false
colorNext := false
fileNext := false
file := ""
fileGiven := false
var color string
for _, arg := range os.Args[1:] {
if len(arg) == 0 {
Expand All @@ -39,7 +93,17 @@ func main() {
colorNext = false
continue
}
if fileGiven {
die("only a single file can be specified\n")
}
if fileNext {
file = arg
fileGiven = true
fileNext = false
continue
}
if arg[0] == '-' && !ignoreDashes {
shortFor:
for _, short := range arg[1:] {
switch short {
case 'h':
Expand All @@ -58,6 +122,16 @@ func main() {
wordBoundary = true
case 'e':
ignoreDashes = true
case '-':
switch arg[2:] {
case "help":
showHelp = true
case "":
fileNext = true
default:
die("Error: %s not recognized\n", arg)
}
break shortFor
default:
die("Error: -%c not recognized\n", short)
}
Expand Down Expand Up @@ -86,54 +160,34 @@ func main() {

if showHelp {
fmt.Printf(
"%s [options] <pattern> [[-i] [-w] [-c <color>] <pattern> [...]]\n"+
"usage: %s [options] <pattern> [[-i] [-w] [-c <color>] <pattern> [...]] [-- <file>]\n"+
"\n"+
"Reads from stdin (or <file> when specified), any regex patterns which match will cause the text to be highlighted.\n"+
"\n"+
" -i case insensitive matching\n"+
" -w word boundary matching\n"+
" -c <color> color to highlight match\n"+
" -c <color> color to highlight match: %s\n"+
" -e <pattern> use pattern (useful for patterns starting with a hyphen)\n"+
" -h, --help display this help\n", progName)
" -h, --help display this help\n", progName, colorOptions)
os.Exit(0)
}

colors := map[string]string{
"red": "\033[0;31m",
"green": "\033[0;32m",
"orange": "\033[0;33m",
"blue": "\033[0;34m",
"purple": "\033[0;35m",
"cyan": "\033[0;36m",
"lightgray": "\033[0;37m",
"darkgray": "\033[1;30m",
"lightred": "\033[1;31m",
"lightgreen": "\033[1;32m",
"yellow": "\033[1;33m",
"lightblue": "\033[1;34m",
"lightpurple": "\033[1;35m",
"lightcyan": "\033[1;36m",
}
nc := "\033[0m"

defaultColors := []string{
colors["red"],
colors["green"],
colors["orange"],
colors["blue"],
colors["purple"],
colors["cyan"],
colors["yellow"],
colors["lightred"],
colors["lightgreen"],
colors["lightblue"],
colors["lightpurple"],
colors["lightcyan"],
}

getColor := func(i int) string {
n := len(defaultColors)
return defaultColors[i%n]
}

scanner := bufio.NewScanner(os.Stdin)
var scanner *bufio.Scanner
if fileGiven {
fileHandle, err := os.Open(file)
if err != nil {
die("failed to open %s: %s\n", file, err.Error())
}
defer fileHandle.Close()
scanner = bufio.NewScanner(bufio.NewReader(fileHandle))
} else {
scanner = bufio.NewScanner(os.Stdin)
}
for scanner.Scan() {
l := scanner.Text()
for i, pat := range patterns {
Expand All @@ -142,7 +196,7 @@ func main() {
var ok bool
col, ok = colors[pat.color]
if !ok {
die("color %s not found", pat.color)
die("color %s not found\n", pat.color)
}
}
l = pat.re.ReplaceAllString(l, col+"$0"+nc)
Expand Down

0 comments on commit fa9bd85

Please sign in to comment.