Crate colored_text

Source
Expand description

A library for adding colors and styles to terminal text output.

This library provides a simple and intuitive way to add colors and styles to text in terminal applications. It works with both string literals and owned strings, and supports various text colors, background colors, and text styles.

§Examples

use colored_text::Colorize;

// Basic color usage
println!("{}", "Red text".red());
println!("{}", "Blue background".on_blue());

// Combining styles
println!("{}", "Bold green text".green().bold());

// Using with format! macro
let name = "World";
println!("{}", format!("Hello, {}!", name.blue().bold()));

// RGB and Hex colors
println!("{}", "RGB color".rgb(255, 128, 0));
println!("{}", "Hex color".hex("#ff8000"));

§Features

  • Basic colors (red, green, blue, yellow, etc.)
  • Background colors
  • Bright color variants
  • Text styles (bold, dim, italic, underline)
  • RGB and Hex color support
  • Style chaining
  • Works with format! macro

§Input Handling

  • RGB values must be in range 0-255 (enforced at compile time via u8 type)
  • Attempting to use RGB values > 255 will result in a compile error
  • Hex color codes can be provided with or without the ‘#’ prefix
  • Invalid hex codes (wrong length, invalid characters) will result in uncolored text
  • All color methods are guaranteed to return a valid string, never panicking
use colored_text::Colorize;

// Valid hex codes (with or without #)
println!("{}", "Valid hex".hex("#ff8000"));
println!("{}", "Also valid".hex("ff8000"));

// Invalid hex codes return uncolored text
println!("{}", "Invalid hex".hex("xyz")); // Returns uncolored text
println!("{}", "Too short".hex("#f8")); // Returns uncolored text

§Note

Colors and styles are implemented using ANSI escape codes, which are supported by most modern terminals. If your terminal doesn’t support ANSI escape codes, the text will be displayed without styling.

Structs§

ColorizeConfig
Configuration for controlling terminal detection behavior.

Traits§

Colorize
Trait for adding color and style methods to strings.