pretty-console
A fluent, zero-cost API for styling terminal text with colors and attributes. Write beautifully formatted console output with an intuitive, chainable interface.
Features
- 🎨 Rich Color Support: 16 named colors, 256 colors, and true color RGB
- ⚡ Zero-Cost Abstractions: Builder pattern compiles to efficient code
- 🔧 Fluent API: Chainable methods for intuitive styling
- 📝 Text Attributes: Bold, italic, underline, blink, reverse, and more
- 🌍 Cross-Platform: ANSI escape codes with Windows 10+ support
- 🎯 Flexible Output: Print immediately or get formatted strings
- 🔄 Reusable Styles: Create style templates and reuse them
- 🚫 No-Color Support: Optional feature to disable all coloring
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic usage:
use Console;
Examples
Basic Styling
use Console;
// Simple color and attribute combinations
new.red.bold.println;
new.yellow.italic.println;
new.blue.underline.println;
// Background colors
new.white.on_red.println;
RGB Colors
new.fg_rgb.println;
new.bg_rgb.println;
Multiple Attributes
new
.bright_red
.on_bright_yellow
.bold
.italic
.underline
.blink
.println;
Reusable Styles
use Console;
// Create a style template
let error_style = new.red.bold;
let warning_style = new.yellow.italic;
let success_style = new.green.bold;
// Reuse with different text
error_style.with_text.println;
warning_style.with_text.println;
success_style.with_text.println;
Using with Format Strings
use Console;
let username = "Alice";
let status = new.green.bold;
println!;
// Output: User Alice is online (with "online" in green and bold)
Advanced Style Management
use ;
// Create predefined styles
let heading_style = new
.fg
.bold
.underline;
let note_style = new
.fg
.italic;
// Use the styles
new_with_style.println;
new_with_style.println;
API Reference
Color Constants
// Basic colors
BLACK, RED, GREEN, YELLOW
BLUE, MAGENTA, CYAN, WHITE
// Bright colors
BRIGHT_BLACK, BRIGHT_RED, BRIGHT_GREEN
BRIGHT_YELLOW, BRIGHT_BLUE, BRIGHT_MAGENTA
BRIGHT_CYAN, BRIGHT_WHITE
Console Methods
Color Setters
fg(color),bg(color)- Set foreground/background colorfg_rgb(r, g, b),bg_rgb(r, g, b)- Set RGB colorsred(),green(),blue(), etc. - Foreground color shortcutson_red(),on_green(),on_blue(), etc. - Background color shortcuts
Attribute Setters
bold(),italic(),underline(),blink()dim(),reverse(),hidden(),strikethrough()
Output Methods
print()- Print without newlineprintln()- Print with newlinewrite_to(writer)- Write to anystd::io::Writeto_string()- Get formatted string
No-Color Support
For environments where terminal colors aren't supported or desired:
[]
= { = "0.1.0", = ["no-color"] }
With the no-color feature, all ANSI escape codes are omitted, and text is printed as-is.
Performance
The builder pattern uses zero-cost abstractions - all styling is computed at compile time where possible. The ANSI code generation is optimized and only occurs when actually printing.
Platform Support
- Unix-like systems: Full support via ANSI escape codes
- Windows 10+: Full support via virtual terminal sequences
- Older Windows: Limited support (requires enabling virtual terminal)
For best Windows compatibility, ensure your terminal supports ANSI escape codes or use Windows 10+.
Alternatives Comparison
| Feature | pretty_console | termcolor | colored |
|---|---|---|---|
| Fluent API | ✅ | ❌ | ✅ |
| True Color | ✅ | ✅ | ✅ |
| Zero-Cost | ✅ | ✅ | ❌ |
| Reusable Styles | ✅ | ❌ | ❌ |
| Attribute Combos | ✅ | ✅ | ✅ |
| No Dependencies | ✅ | ❌ | ✅ |
License
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.
Happy styling! 🎨