/
term.go
107 lines (88 loc) · 2.86 KB
/
term.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package env
import (
"io"
"os"
"runtime"
isatty "github.com/mattn/go-isatty"
)
// a fake TTY type for testing that can only be implemented within this package
type isTestFakeTTY interface {
isTestFakeTTY()
}
// IsTerminal returns true if the writer w is a terminal
func IsTerminal(w io.Writer) bool {
// check for internal fake type we can use for testing.
if _, ok := (w).(isTestFakeTTY); ok {
return true
}
// check for real terminals
if v, ok := (w).(*os.File); ok {
return isatty.IsTerminal(v.Fd())
}
return false
}
// IsSmartTerminal returns true if the writer w is a terminal AND
// we think that the terminal is smart enough to use VT escape codes etc.
func IsSmartTerminal(w io.Writer) bool {
return isSmartTerminal(w, runtime.GOOS, os.LookupEnv)
}
func isSmartTerminal(w io.Writer, GOOS string, lookupEnv func(string) (string, bool)) bool {
// Not smart if it's not a tty
if !IsTerminal(w) {
return false
}
// getenv helper for when we only care about the value
getenv := func(e string) string {
v, _ := lookupEnv(e)
return v
}
// Explicit request for no ANSI escape codes
// https://no-color.org/
if _, set := lookupEnv("NO_COLOR"); set {
return false
}
// Explicitly dumb terminals are not smart
// https://en.wikipedia.org/wiki/Computer_terminal#Dumb_terminals
term := getenv("TERM")
if term == "dumb" {
return false
}
// st has some bug 🤷♂️
// https://github.com/kubernetes-sigs/kind/issues/1892
if term == "st-256color" {
return false
}
// On Windows WT_SESSION is set by the modern terminal component.
// Older terminals have poor support for UTF-8, VT escape codes, etc.
if GOOS == "windows" && getenv("WT_SESSION") == "" {
return false
}
/* CI Systems with bad Fake TTYs */
// Travis CI
// https://github.com/kubernetes-sigs/kind/issues/1478
// We can detect it with documented magical environment variables
// https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
if getenv("HAS_JOSH_K_SEAL_OF_APPROVAL") == "true" && getenv("TRAVIS") == "true" {
return false
}
// OK, we'll assume it's smart now, given no evidence otherwise.
return true
}
// trivial fake TTY writer for testing
type testFakeTTY struct{}
func (t *testFakeTTY) Write(p []byte) (int, error) {
return len(p), nil
}
func (t *testFakeTTY) isTestFakeTTY() {}