package output

import (
	"io"
	"os"

	"github.com/mattn/go-isatty"
)

// IsTTY reports whether f is an open terminal or Cygwin/MSYS pty.
// Returns false for a nil file.
func IsTTY(f *os.File) bool {
	if f == nil {
		return false
	}
	fd := f.Fd()
	return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
}

// ColorsEnabled reports whether ANSI color output should be emitted on w.
// Returns false when:
//   - the NO_COLOR environment variable is set (https://no-color.org/), or
//   - w is not an *os.File (e.g. bytes.Buffer, strings.Builder), or
//   - w is an *os.File but not a terminal.
func ColorsEnabled(w io.Writer) bool {
	if _, ok := os.LookupEnv("NO_COLOR"); ok {
		return false
	}
	f, ok := w.(*os.File)
	if !ok {
		return false
	}
	return IsTTY(f)
}
