Skip to content

Commit 785e560

Browse files
committed
Support `NO_COLOR' environment variable
Suppress color if the `NO_COLOR' environment variable is is present. For more information, visit `https://no-color.org/'.
1 parent a8aed64 commit 785e560

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

alogview.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,21 @@ func main() {
8686
if len(*s) > 0 {
8787
adbargs = append(adbargs, "-s", *s)
8888
}
89+
_, suppresscolor := os.LookupEnv("NO_COLOR")
8990
if len(flag.Args()) == 0 {
90-
processLogs(func(line *logLine) bool { return true })
91+
processLogs(func(line *logLine) bool { return true }, suppresscolor)
9192
} else {
9293
packages := make(map[string]bool)
9394

9495
for _, pkg := range os.Args {
9596
packages[pkg] = true
9697
}
9798
pids := getProcs(packages)
98-
processLogs(func(line *logLine) bool { return filterByPackages(line, packages, pids) })
99+
processLogs(func(line *logLine) bool { return filterByPackages(line, packages, pids) }, suppresscolor)
99100
}
100101
}
101102

102-
func processLogs(filter filter) {
103+
func processLogs(filter filter, suppresscolor bool) {
103104
r, w := io.Pipe()
104105

105106
go runADB(w, "logcat")
@@ -119,7 +120,11 @@ func processLogs(filter filter) {
119120
continue
120121
}
121122
if filter(msg) {
122-
fmt.Printf("%s%s%s\n", colorForLevel(msg.level), line, reset)
123+
if suppresscolor {
124+
fmt.Printf("%s\n", line)
125+
} else {
126+
fmt.Printf("%s%s%s\n", colorForLevel(msg.level), line, reset)
127+
}
123128
}
124129
}
125130
}

0 commit comments

Comments
 (0)