2020-08-06 20:58:47 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
isatty "github.com/mattn/go-isatty"
|
2022-12-18 03:51:33 +03:00
|
|
|
"golang.org/x/term"
|
2020-08-06 20:58:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// isTerminal returns true if w is a terminal.
|
|
|
|
func isTerminal(w io.Writer) bool {
|
|
|
|
switch v := w.(type) {
|
|
|
|
case *os.File:
|
2022-12-18 03:51:33 +03:00
|
|
|
return term.IsTerminal(int(v.Fd()))
|
2020-08-06 20:58:47 +03:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isColorTerminal returns true if w is a colorable terminal.
|
|
|
|
func isColorTerminal(w io.Writer) bool {
|
|
|
|
if w == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isTerminal(w) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("TERM") == "dumb" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
f, ok := w.(*os.File)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if isatty.IsCygwinTerminal(f.Fd()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|