-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
83 lines (75 loc) · 1.79 KB
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"bufio"
"flag"
"fmt"
"os"
"regexp"
"strings"
"github.com/fatih/color"
)
func main() {
// pull in cli values
Color := flag.String("color", "", "Color to highlight")
Background := flag.Bool("background", false, "Background highlighting [false]")
Pattern := flag.String("pattern", "", "Pattern to highlight")
Strip := flag.Bool("strip", false, "Strip all colors [false]")
flag.Parse()
strip := *Strip
// check color
highlight := color.FgWhite
if *Background {
switch strings.ToLower(*Color) {
case "red":
highlight = color.BgRed
case "green":
highlight = color.BgGreen
case "yellow":
highlight = color.BgYellow
case "blue":
highlight = color.BgBlue
case "magenta":
highlight = color.BgMagenta
case "cyan":
highlight = color.BgCyan
}
} else {
switch strings.ToLower(*Color) {
case "red":
highlight = color.FgRed
case "green":
highlight = color.FgGreen
case "yellow":
highlight = color.FgYellow
case "blue":
highlight = color.FgBlue
case "magenta":
highlight = color.FgMagenta
case "cyan":
highlight = color.FgCyan
}
}
if highlight == color.FgWhite && !strip {
fmt.Fprintln(os.Stderr, "ERROR: Not a valid color")
}
reColor := color.New(highlight).SprintFunc()
// check for pattern
pattern := *Pattern
if pattern == "" && !strip {
fmt.Fprintln(os.Stderr, "ERROR: No provided pattern")
}
// strip patterns
stripreg := regexp.MustCompile("(\\033|\\027)\\[[0-9]*m")
// setup standard input
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
if strip {
fmt.Println(stripreg.ReplaceAllString(scanner.Text(), ""))
} else {
fmt.Println(strings.Replace(scanner.Text(), pattern, reColor(pattern), -1))
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "ERROR: ", err)
}
}