spraypaint
Terminal string styling for Rust.
cargo add spraypaint
Quick Start
use ;
The paint! Macro
paint! is the primary API. It prints to stdout with a newline, just like println!,
but with inline styled spans.
Syntax
use ;
// {style.style text content}
paint!;
// Multiple styles with dots
paint!;
// Nested spans
paint!;
// Embed Rust expressions with {expr}
let name = "Ferris";
let count = 42_u32;
paint!;
// No newline (like print!)
paint!;
// To stderr
paint!;
// styled! returns a String instead of printing
let msg = styled!;
error!;
Recognized style tokens
Colors: black red green yellow blue magenta cyan white
and their bright_ variants.
Backgrounds: prefix any color with on_ -- on_red, on_bright_blue, etc.
Attributes: bold dim italic underline blink blink_fast reverse hidden strikethrough
Invalid tokens produce a compile error:
error: unknown style `blod` in paint! template
hint: valid attributes are bold, dim, italic, underline, strikethrough, reverse
Extension Trait
Import Colorize to style any Display value:
use Colorize;
// Colors
"red text".red.paint;
"blue text".blue.paint;
"custom".rgb.paint;
"hex".hex.paint;
"xterm index".xterm.paint;
// Backgrounds
"warning".black.on_yellow.paint;
"danger".white.on_red.paint;
// Attributes
"bold".bold.paint;
"italic".italic.paint;
"underline".underline.paint;
"dim".dim.paint;
"strikethrough".strikethrough.paint;
// Chaining (flat -- no nesting penalty)
"error".red.bold.underline.paint;
// Without newline
"loading".yellow.paint_inline;
// To stderr
"fatal".red.bold.paint_err;
Works on any Display type:
42_i32.green.bold.paint;
3.14_f64.cyan.paint;
true.yellow.paint;
Reusable Styles
use Style;
let error = new.red.bold.underline;
let warn = new.yellow.bold;
let info = new.cyan;
error.apply.paint;
warn.apply.paint;
info.apply.paint;
// Merge styles
let loud_error = error.merge;
loud_error.apply.paint;
Gradients
use ;
// Two-stop gradient
"Red to Cyan".gradient.paint;
// Multi-stop (full rainbow)
let stops = vec!;
"The quick brown fox".gradient_multi.paint;
// Hex stops
let from = from_hex.unwrap;
let to = from_hex.unwrap;
"Purple to Pink".gradient.paint;
Color Detection
spraypaint auto-detects terminal color support on first use.
| Environment Variable | Effect |
|---|---|
NO_COLOR=1 |
Disable all color (level 0) |
FORCE_COLOR=1 |
Force basic 16-color |
FORCE_COLOR=2 |
Force 256-color |
FORCE_COLOR=3 |
Force truecolor |
COLORTERM=truecolor |
Enable truecolor |
TERM=xterm-256color |
Enable 256-color |
Override programmatically:
use ;
set_color_level;
// or
set_color_level; // disable color
Colors automatically downgrade when the terminal doesn't support them:
- TrueColor → Xterm256 → Basic16 → no color
Color Reference
use Color;
RED // named constant (ANSI)
rgb // 24-bit truecolor
xterm // xterm 256-color index
from_hex // parse CSS hex
from_hex // 3-digit shorthand
Examples
cargo run --example basic # colors and attributes
cargo run --example paint_macro # paint! macro features
cargo run --example gradient # gradient text
cargo run --example detect # color level detection