go-isatty
Cross-platform Go library that detects whether a file descriptor is connected to a terminal, including Cygwin and MSYS2 ptys on Windows.
Repository Health
Technical Analysis
go-isatty is a minimal, dependency-light Go package that answers one question reliably across platforms: is this file descriptor attached to an interactive terminal? It underpins color and TTY-detection logic in some of the Go ecosystem’s most widely used CLI tools, letting them decide when to emit ANSI colors, progress bars, or interactive prompts versus falling back to plain output for pipes, redirects, and CI logs.
The implementation is split by build tags into platform-specific files — BSD/Darwin using ioctl TIOCGETA via golang.org/x/sys/unix, Windows using GetConsoleMode plus an NtQueryObject-based Cygwin/MSYS2 pty name check, Plan9, Solaris, and a catch-all stub for sandboxed runtimes like js/wasm and App Engine. Each file compiles only for its target OS via Go build constraints, so the public IsTerminal and IsCygwinTerminal functions stay a small, stable two-function API regardless of platform.
What You Get
- A single IsTerminal(fd uintptr) bool function that works uniformly across Linux, BSD/Darwin, Windows, Plan9, Solaris, and sandboxed runtimes
- IsCygwinTerminal(fd uintptr) bool for detecting Cygwin and MSYS2 pseudo-terminals on Windows, where the native console API alone can’t tell
- Platform-specific implementations selected automatically at compile time via Go build tags, with no runtime detection overhead
- A dependency footprint limited to golang.org/x/sys, keeping it safe to vendor into CLI tools without dragging in a larger toolkit
Common Use Cases
- CLI tools deciding whether to emit ANSI color codes or plain text depending on whether stdout is a terminal or a redirected file/pipe
- Interactive prompts and spinners that should only render when attached to a real terminal, not when running in CI or as part of a script
- Logging libraries that switch between colorized human-readable output and structured plain output based on terminal detection
- Cross-platform Go tools that need consistent terminal detection on Windows including Cygwin/MSYS2 environments, not just native Windows consoles
Under The Hood
Architecture go-isatty is a flat, single-package library with no internal layering — its entire public surface is two functions, IsTerminal and IsCygwinTerminal, declared once conceptually and implemented separately per platform via Go build constraints (isatty_bsd.go, isatty_windows.go, isatty_others.go, and platform equivalents for Linux, Plan9, and Solaris). Each file satisfies the same signature so the compiler selects exactly one implementation per build target, meaning there’s no runtime branching, interface indirection, or dependency injection — callers pass a raw uintptr file descriptor directly to a syscall wrapper. If the core two-function signature ever changed, every downstream consumer that gates terminal-aware behavior — colorized CLIs, prompt libraries, loggers — would need updating, which is why the API has stayed stable for over a decade.
Tech Stack The stack is intentionally minimal: Go 1.20+ as declared in go.mod, with a single external dependency, golang.org/x/sys v0.28.0, used only on BSD/Darwin targets to call unix.IoctlGetTermios. The Windows implementation bypasses any external package and calls kernel32.dll (GetConsoleMode, GetFileInformationByHandleEx, GetFileType) and ntdll.dll (NtQueryObject) directly via syscall.NewLazyDLL, avoiding cgo entirely. There’s no build tooling beyond the standard go build/go test toolchain and a go.test.sh coverage script; the library has no deployment target of its own since it’s consumed as a vendored dependency inside other Go binaries.
Code Quality Test coverage is present but shallow — the non-Windows and Windows test files run largely smoke-level checks (one test’s own comment notes it verifies non-panic behavior rather than a specific return value, since terminal state depends on how the test runner is invoked). Error handling favors returning a plain bool over propagating errors, which fits a boolean-predicate API. What compensates for the thin unit assertions is a genuinely rigorous CI setup: GitHub Actions runs the full suite with the race detector across Linux, macOS, and Windows runners on multiple Go versions each, uploading coverage to Codecov on every push and pull request. Naming is idiomatic Go throughout, and there’s no dedicated linter configuration, but the surface area is small enough that this is a low-risk gap.
What Makes It Unique go-isatty doesn’t innovate technically — TTY detection via ioctl or GetConsoleMode is a well-understood, decades-old technique — but its practical contribution is being the de facto standard implementation the Go ecosystem converged on for exactly this problem, correctly handling an edge case most naive implementations miss: Cygwin and MSYS2 terminals on Windows report as pipes rather than consoles, so a plain GetConsoleMode check returns false even inside an interactive Cygwin shell. Its named-pipe pattern inspection to recover that information, plus a documented fallback to the undocumented NtQueryObject API for older Windows versions, reflects accumulated platform knowledge rather than a novel architecture.
Used by 16 apps in this directory
Authgear
Authentication
Open-source, self-hostable authentication platform with passkeys, biometric login, SSO, MFA, and GraphQL admin API — a full Auth0/Clerk/Firebase alternative for SaaS and mobile apps.
Coder
Devops · Developer Tools · Code Editors
Self-hosted cloud development environments and AI coding agents — defined in Terraform, connected via WireGuard, automatically shut down when idle.
Cog
AI Development · Devops · Developer Tools
An open-source CLI that packages machine learning models into standard, production-ready Docker containers — no Dockerfile wrangling, no CUDA version hell.
Crush
Developer Tools · AI Code Assistants · AI Assistants
Your terminal coding companion — wire up any LLM with LSP intelligence, MCP extensibility, and a skills system that learns your workflow.
Dokku
Devops · Hosting Control Panel
The smallest PaaS implementation you've ever seen — deploy apps via git push using Docker and Heroku buildpacks on your own server.
Dolt
Databases · Data Engineering · Developer Tools
The SQL database you can branch, merge, diff, and clone — Git for your data, MySQL-compatible and ready for multi-agent AI workflows.
Flipt
Devops · Developer Tools
Git-native feature flag platform that stores, versions, and deploys feature toggles directly in your own Git repositories with no external database required.
Gitea
Devops · Developer Tools · Project Management
Self-hosted DevOps in a single Go binary — Git hosting, GitHub Actions-compatible CI/CD, and 30+ package registries without any SaaS dependency.
Gotify
Monitoring · Developer Tools
A lightweight, self-hosted push notification server that sends and receives messages in real time over WebSocket, with a sleek web UI and a native Go plugin system.