feat(06-01): add Formatter interface, Registry, and TTY color detection
- pkg/output/formatter.go: Formatter interface, Options, Registry with Register/Get/Names, ErrUnknownFormat sentinel - pkg/output/colors.go: IsTTY + ColorsEnabled honoring NO_COLOR - Promote github.com/mattn/go-isatty to direct dependency - Unit tests cover registry round-trip, unknown lookup, sorted Names, non-TTY buffer, NO_COLOR override Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
34
pkg/output/colors.go
Normal file
34
pkg/output/colors.go
Normal file
@@ -0,0 +1,34 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user