terminal_size

A tiny cross-platform Rust crate for getting the width and height of the controlling terminal.

Library
Cargo
v0.4.4
119stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
51/100Fair
Development Activity36
Maintenance24
Community72
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
87/100Excellent
Architecture80
Code Quality85
Innovation88
Learning Curve95

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) and Height(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 Option return that yields None when 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 None result) 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search