TTY (terminal) detection utilities for standard input, output, and error streams.
This package exposes functions for checking whether standard input, output, or error streams are connected to a real terminal. The detection uses golang.org/x/term, which is the same library used by the spinner and progress-bar components in this codebase.
On WebAssembly targets (js/wasm) the package provides stub implementations that always return false, since WASM environments do not have real TTY file descriptors.
import "github.com/github/gh-aw/pkg/tty"
if tty.IsStdinTerminal() {
// Safe to request interactive input from stdin
}
if tty.IsStdoutTerminal() {
// Safe to emit colored or animated output to stdout
fmt.Println(coloredOutput)
} else {
// Plain output for pipes/redirects
fmt.Println(plainOutput)
}
if tty.IsStderrTerminal() {
// Safe to emit spinner or progress animation to stderr
}Internal:
- None
External:
golang.org/x/term— TTY detection viaterm.IsTerminal
- Terminal detection is evaluated at call time, not cached. This is intentional: the streams could be redirected between calls in some testing scenarios.
- The WASM stub (
tty_wasm.go) always returnsfalseso that components built for the browser never attempt to use ANSI escape codes. - Prefer this package over calling
term.IsTerminaldirectly to keep the TTY detection logic centralized and easily testable. - Components that need to adapt output for terminals (spinners, progress bars, colored messages) should call
IsStderrTerminal()rather than checkingos.Stderrdirectly. - Components that request interactive input should call
IsStdinTerminal()before reading fromos.Stdin.
This specification is automatically maintained by the spec-extractor workflow.
