terminal_size
A tiny cross-platform Rust crate for getting the width and height of the controlling terminal.
Repository Health
Technical Analysis
terminal_size is a minimal, dependency-light Rust library that reports the dimensions (columns and rows) of the current terminal. It works consistently across Linux, macOS, Windows, and illumos by wrapping each platform’s native mechanism behind a single, ergonomic function.
With over 177 million downloads on crates.io, it is a foundational building block underneath countless CLI tools and TUI applications that need to lay out output to fit the available screen space.
What You Get
- A single
terminal_size()call that checks stdout, stderr, and stdin in order and returns the first TTY’s dimensions terminal_size_of()to query the size of a specific file descriptor or handle you already hold- Strongly-typed
Width(u16)andHeight(u16)newtypes so you never mix up rows and columns - Cross-platform support for Linux, macOS, Windows, and illumos from one unchanged API
- A graceful
Optionreturn that yieldsNonewhen output is redirected or piped rather than a hard error
Common Use Cases
- Wrapping or truncating CLI output to fit the current terminal width
- Laying out progress bars, tables, and TUI widgets that must fill the available screen
- Detecting piped or redirected output (a
Noneresult) to switch to a non-interactive rendering mode - Centering or padding text based on live terminal dimensions
Under The Hood
Architecture - The crate is split by platform behind cfg attributes: src/lib.rs defines the shared Width/Height newtypes and re-exports the platform module (unix.rs, windows.rs, or a no-op fallback). terminal_size() walks stdout, stderr, then stdin, returning the first stream that is a TTY. Tech Stack - Pure Rust (edition 2021, MSRV 1.71) with rustix (termios feature) on Unix for the winsize ioctl and windows-sys for the console screen buffer API on Windows; there are no other runtime dependencies. Code Quality - The code is compact and idiomatic, guards every path with isatty/validity checks, and includes an integration test that compares its output against stty size. Deprecated raw-fd entry points are retained with clear migration notes. API Design - The public surface is deliberately minimal: one primary function plus typed dimension wrappers, so getting the terminal size is a single expression, and the Option return communicates the non-TTY case without forcing error handling.