Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

tty Package

TTY (terminal) detection utilities for stdout and stderr streams.

Overview

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.

Public API

Functions

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

Usage Examples

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
}

Dependencies

Internal:

  • None

External:

  • golang.org/x/term — TTY detection via term.IsTerminal

Design Notes

  • 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 returns false so that components built for the browser never attempt to use ANSI escape codes.
  • Prefer this package over calling term.IsTerminal directly 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 checking os.Stderr directly.

This specification is automatically maintained by the spec-extractor workflow.