terminal_style
A minimal Rust library for styling terminal text using ANSI escape codes. Supports 24-bit TrueColor (RGB) as its primary format, alongside 256-color (8-bit) ANSI quantization. Easily apply foreground/background colors from hex, RGB, or ANSI 8-bit values to strings, 1D vectors, and 2D vectors. Perfect for simple CLI tools.
Installation
terminal_style is published as a crate on crates.io.
Features
- 24-bit TrueColor (RGB) support for smooth gradients
- 8-bit ANSI quantization for legacy terminal support
- Convert RGB or Hex to ANSI 256-color
- Apply foreground/background color to strings, vectors, and 2D vectors
- Format text as bold, italic, faint, inverse, or underline
- Graceful handling of invalid color inputs
Usage
Formatting functions work with strings, vectors, and 2D vectors of strings seamlessly. It also supports references, so you can pass either owned or borrowed values.
Color Depth Options
terminal_style provides two sets of functions for color styling:
color/background: Aliases forcolor_rgb/background_rgb. Unconditionally generates 24-bit TrueColor sequences.color_ansi/background_ansi: Quantizes any color input to the nearest 8-bit ANSI index (256-color palette).
use ;
// 24-bit TrueColor: \x1b[38;2;255;20;147m
let rgb = color_rgb?;
// 8-bit ANSI: \x1b[38;5;198m
let ansi = color_ansi?;
Supported Input Types
| Input Type | Output Type | Description |
|---|---|---|
String |
String |
Single string formatting |
&str |
String |
Single string formatting |
&String |
String |
Reference forwarding to String |
Vec<String> |
Vec<String> |
Format each element individually |
&Vec<String> |
Vec<String> |
Reference forwarding to owned Vec<String> |
Vec<Vec<String>> |
Vec<Vec<String>> |
Format every element in each subvector |
&Vec<Vec<String>> |
Vec<Vec<String>> |
Reference forwarding to owned Vec<Vec<String>> |
Example
use ;
Color Conversion Examples
Utility functions enable converting between RGB, HEX, and ANSI 8-bit values.
use *;
Additional examples in the examples folder.
Tests
Structure
color/: Utility color conversions (hex, rgb, ansi)format/: Terminal text styling functionstests/: Test suiteexamples/: Usage examples
Authors
License
Coded in Rust with a little help from the LLMs. Based on the lovingly handcrafted colors.crumb.. Derived from work done on Impossible.js. Enjoy.
Fabriqué au Canada : Made in Canada 🇨🇦