-
Notifications
You must be signed in to change notification settings - Fork 1
/
crror.go
89 lines (78 loc) · 2.13 KB
/
crror.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
84
85
86
87
88
89
package main
import (
"bufio"
"flag"
"fmt"
"github.com/andrew-d/go-termutil"
"log"
"os"
"regexp"
"strings"
)
var (
input *bufio.Reader
re1 = regexp.MustCompile("make(\\[[0-9]+\\])?:")
)
const (
bold = "\033[1m"
reset = "\033[0m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
magenta = "\033[35m"
cyan = "\033[36m"
)
func first(args ...interface{}) interface{} {
return args[0]
}
func Colorize(line string) string {
tokens := strings.Split(line, " ")
if cap(tokens) < 2 {
return line
}
cline := line
switch {
case (cap(tokens) > 2) && (tokens[1] == "undefined"):
cline = bold + tokens[0] + " " + red + strings.Join(tokens[1:], " ") + reset
case (cap(tokens) > 2) && (tokens[1] == "error:"):
cline = bold + strings.Join(tokens[0:2], " ") + " " + red + strings.Join(tokens[2:], " ") + reset
case (cap(tokens) > 3) && (tokens[1] == "fatal") && (tokens[1] == "error:"):
cline = bold + strings.Join(tokens[0:3], " ") + " " + red + strings.Join(tokens[3:], " ") + reset
case (cap(tokens) > 2) && (tokens[1] == "warning:"):
cline = bold + strings.Join(tokens[0:2], " ") + " " + yellow + strings.Join(tokens[2:], " ") + reset
case (cap(tokens) > 3) && (tokens[1] == "In") && (tokens[2] == "function"):
cline = strings.Join(tokens[0:2], " ") + " " + magenta + strings.Join(tokens[3:], " ") + reset
case (cap(tokens) > 4) && (tokens[1] == "In") && (tokens[2] == "member") && (tokens[3] == "function"):
cline = strings.Join(tokens[0:3], " ") + " " + magenta + strings.Join(tokens[4:], " ") + reset
case (cap(tokens) > 2) && re1.MatchString(tokens[0]) && (tokens[1] == "***"):
cline = bold + red + strings.Join(tokens[:cap(tokens)], " ") + reset
}
return cline
}
func main() {
flag.Parse()
fname := flag.Arg(0)
if fname == "" {
input = bufio.NewReader(os.Stdin)
} else {
f, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
defer f.Close()
input = bufio.NewReader(f)
}
tty := termutil.Isatty(os.Stdout.Fd())
for {
line, err := input.ReadString('\n')
if tty {
fmt.Print(Colorize(line))
} else {
fmt.Print(line)
}
if err != nil {
break
}
}
}