Skip to content

Commit

Permalink
Stripped background color option
Browse files Browse the repository at this point in the history
Display alternating lines in different colors.
This has a known limitation that it doesn't fully color the complete
line.

Currently hardcoded to yellow; will have to be improved later.

Signed-off-by: Alex Couture-Beil <[email protected]>
  • Loading branch information
alexcb committed Mar 17, 2023
1 parent f06085d commit 057b314
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
ignoreDashes := false
wordBoundary := false
showHelp := false
stripes := false
colorNext := false
fileNext := false
file := ""
Expand Down Expand Up @@ -123,9 +124,12 @@ func main() {
case 'e':
ignoreDashes = true
case '-':
// long flags
switch arg[2:] {
case "help":
showHelp = true
case "stripes":
stripes = true
case "":
fileNext = true
default:
Expand Down Expand Up @@ -168,6 +172,7 @@ func main() {
" -w word boundary matching\n"+
" -c <color> color to highlight match: %s\n"+
" -e <pattern> use pattern (useful for patterns starting with a hyphen)\n"+
" --stripes color each line background with alternating colors (experimental)\n"+
" -h, --help display this help\n", progName, colorOptions)
os.Exit(0)
}
Expand All @@ -188,6 +193,7 @@ func main() {
} else {
scanner = bufio.NewScanner(os.Stdin)
}
lineNum := 0
for scanner.Scan() {
l := scanner.Text()
for i, pat := range patterns {
Expand All @@ -201,7 +207,18 @@ func main() {
}
l = pat.re.ReplaceAllString(l, col+"$0"+nc)
}
fmt.Println(l)

if stripes {
// tabs dont get colored, convert to spaces
l = strings.Replace(l, "\t", " ", -1)
}

if lineNum%2 == 1 && stripes {
fmt.Println("\033[43m" + l + nc)
} else {
fmt.Println(l)
}
lineNum++
}

if err := scanner.Err(); err != nil {
Expand Down

0 comments on commit 057b314

Please sign in to comment.