colorSupported decides whether ANSI color should be emitted, given a way to look up environment variables and whether stderr is an interactive terminal. It is kept pure so the decision logic can be unit tested. Precedence: 1. NO_COLOR set, any value -> no color (https://no-color.org/). Universal op
(lookupEnv func(string) (string, bool), isTerminal bool)
| 63 | // render ANSI (see colorCapableCISignals); otherwise no color. This keeps |
| 64 | // piped output and ANSI-incapable consoles (e.g. Jenkins) clean. |
| 65 | func colorSupported(lookupEnv func(string) (string, bool), isTerminal bool) bool { |
| 66 | if _, ok := lookupEnv("NO_COLOR"); ok { |
| 67 | return false |
| 68 | } |
| 69 | |
| 70 | for _, key := range []string{"CLICOLOR_FORCE", "FORCE_COLOR"} { |
| 71 | if v, ok := lookupEnv(key); ok && v != "" && v != "0" { |
| 72 | return true |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if v, ok := lookupEnv("TERM"); ok && v == "dumb" { |
| 77 | return false |
| 78 | } |
| 79 | |
| 80 | if isTerminal { |
| 81 | return true |
| 82 | } |
| 83 | |
| 84 | for _, key := range colorCapableCISignals { |
| 85 | if _, ok := lookupEnv(key); ok { |
| 86 | return true |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return false |
| 91 | } |
no outgoing calls