tty.go 404 B

12345678910111213141516171819
  1. package cli
  2. import (
  3. "io"
  4. "os"
  5. "golang.org/x/term"
  6. )
  7. // isTTY reports whether r is attached to a terminal. In production the app
  8. // passes os.Stdin, which might be a tty. In tests a bytes.Buffer is supplied
  9. // and the type assertion fails, so we correctly treat it as non-tty.
  10. func isTTY(r io.Reader) bool {
  11. f, ok := r.(*os.File)
  12. if !ok {
  13. return false
  14. }
  15. return term.IsTerminal(int(f.Fd()))
  16. }