prettyt
A lightweight, environment-aware terminal text styling library with automatic color downsampling.
Features
- ANSI16, ANSI256, and TrueColor support
- Automatic terminal color capability detection
- NO_COLOR support
- Fluent builder-style API
- Declarative styling macros
- Zero dependencies
- Lightweight and fast
Quick Start
Inline Printing (Using Macros)
Use the sprintln! macro to cleanly format and print styled text. It seamlessly handles native format! interpolation arguments directly, eliminating the need to manually build separate formatted strings beforehand:
use ;
Builder Style Formatting
For finer control, you can build styles dynamically using the fluent builder API. The .apply() method accepts any type implementing std::fmt::Display (such as strings, integers, or floats) and returns an environment-aware styled string:
use ;
Available Styles
| Style | Macro Syntax | ANSI Code |
|---|---|---|
| Bold | bold |
\x1b[1m |
| Dim | dim |
\x1b[2m |
| Italic | italic |
\x1b[3m |
| Underline | underline |
\x1b[4m |
| Invert | invert |
\x1b[7m |
| Strikethrough | strikethrough |
\x1b[9m |
| Foreground Color | fg(Color::RED) |
\x1b[3Xm / extended |
| Background Color | bg(Color::BLUE) |
\x1b[4Xm / extended |
Colors
ANSI16
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White
BrightBlack
BrightRed
BrightGreen
BrightYellow
BrightBlue
BrightMagenta
BrightCyan
BrightWhite
ANSI256
Ansi256
TrueColor RGB
Rgb
Combining Styles
use ;
Environment Configuration & Overrides
prettyt automatically detects terminal color support based on the current
environment and output stream. This behavior can be customized using standard
environment variables:
| Environment Variable | Allowed Values | Description |
|---|---|---|
NO_COLOR |
Any value | Disables all styling and color output. Presence alone is enough to disable colors, following the no-color.org convention. |
FORCE_COLOR |
0, 1, 2, 3, true, yes |
Explicitly forces a color level, even when output is redirected or piped. 0 disables color, 1 enables basic ANSI colors, 2 enables 256-color support, and any other value enables TrueColor. |
COLORTERM |
truecolor, 24bit |
Indicates native 24-bit RGB color support in the active terminal emulator. |
TERM |
dumb, *256color* |
Fallback terminal capability detection. dumb disables styling, while values containing 256color enable 256-color support. |
Detection Precedence
Environment detection follows this priority order:
NO_COLORFORCE_COLOR- TTY detection (
stdout.is_terminal()) COLORTERMTERM- Fallback to basic ANSI colors
Piping & Redirection
If stdout is redirected or piped to another process, prettyt automatically
disables styling to avoid leaking ANSI escape sequences into logs or plain-text
outputs. This behavior can still be overridden with FORCE_COLOR.