TTY (terminal) detection utilities for stdout and stderr streams.
This package exposes two simple functions for checking whether the standard 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.
| Function | Signature | Description |
|---|---|---|
IsStdoutTerminal |
func() bool |
Returns true if stdout (os.Stdout) is connected to a terminal |
IsStderrTerminal |
func() bool |
Returns true if stderr (os.Stderr) is connected to a terminal |
import "github.com/github/gh-aw/pkg/tty"
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.
This specification is automatically maintained by the spec-extractor workflow.