### Basic Usage of Colored Crate in Rust Source: https://github.com/colored-rs/colored/blob/master/README.md Illustrates the fundamental setup for using the `colored` crate in a Rust project. It includes the necessary `use` statement and a simple `println!` macro to demonstrate colored output. ```rust extern crate colored; // not needed in Rust 2018+ use colored::Colorize; // test the example with `cargo run --example most_simple` fn main() { // TADAA! println!("{} {} !", "it".green(), "works".blue().bold()); } ``` -------------------------------- ### Dynamic Color Parsing from String in Rust Source: https://github.com/colored-rs/colored/blob/master/README.md Shows how to dynamically set colors for strings by parsing color names from strings. Includes examples of the easy way using `.color()` and the safer way using `Result` and `parse()`. ```rust // the easy way "blue string yo".color("blue"); // this will default to white "white string".color("zorglub"); // the safer way via a Result let color_res : Result = "zorglub".parse(); "red string".color(color_res.unwrap_or(Color::Red)); ``` -------------------------------- ### Configure `no-color` feature in Cargo.toml Source: https://context7.com/colored-rs/colored/llms.txt To enable the `no-color` feature, add it to your `Cargo.toml` file. This example shows how to define a `dumb_terminal` feature that depends on `colored/no-color`. ```toml # Cargo.toml [features] dumb_terminal = ["colored/no-color"] [dev-dependencies] colored = { version = "3", features = [] } ``` -------------------------------- ### Basic and Advanced Terminal Coloring in Rust Source: https://github.com/colored-rs/colored/blob/master/README.md Demonstrates various ways to apply colors and styles to strings using the `colored` crate. Includes basic colors, background colors, truecolor, styles like bold and italic, and chaining multiple effects. Also shows string formatting with colors and clearing styles. ```rust "this is blue".blue(); "this is red".red(); "this is red on blue".red().on_blue(); "this is also red on blue".on_blue().red(); "you can use truecolor values too!".truecolor(0, 255, 136); "background truecolor also works :)".on_truecolor(135, 28, 167); "truecolor from tuple".custom_color((0, 255, 136)); "background truecolor from tuple".on_custom_color((0, 255, 136)); "bright colors are welcome as well".on_bright_blue().bright_red(); "you can also make bold text".bold(); println!("{} {} {}", "or use".cyan(), "any".italic().yellow(), "string type".cyan()); "or change advice. This is red".yellow().blue().red(); "or clear things up. This is default color and style".red().bold().clear(); "purple and magenta are the same".purple().magenta(); "and so are normal and clear".normal().clear(); "you can specify color by string".color("blue").on_color("red"); "you can also use hex colors".color("#0057B7").on_color("#fd0"); String::from("this also works!").green().bold(); format!("{:30}", "format works as expected. This will be padded".blue()); format!("{:.3}", "and this will be green but truncated to 3 chars".green()); ``` -------------------------------- ### Colorize Trait - Background Colors Source: https://context7.com/colored-rs/colored/llms.txt Illustrates applying background colors using the `on_` prefix, which can be combined with foreground colors and other styles. ```APIDOC ## Colorize Trait - Background Colors ### Description Applies background colors to strings, optionally combined with foreground colors. ### Usage ```rust use colored::Colorize; println!("{}", "red text on blue background".red().on_blue()); println!("{}", "yellow text on magenta background".yellow().on_magenta()); println!("{}", "green on bright yellow".green().on_bright_yellow()); println!("{}", "bright red on bright white".bright_red().on_bright_white()); // Order of chaining does not affect the final result println!("{}", "same result".on_blue().red()); println!("{}", "same result".red().on_blue()); ``` ### Methods - `on_black()` - `on_red()` - `on_green()` - `on_yellow()` - `on_blue()` - `on_magenta()` - `on_purple()` - `on_cyan()` - `on_white()` - `on_bright_black()` - `on_bright_red()` - `on_bright_green()` - `on_bright_yellow()` - `on_bright_blue()` - `on_bright_magenta()` - `on_bright_cyan()` - `on_bright_white()` ``` -------------------------------- ### Adding Colored Crate Dependency to Cargo.toml Source: https://github.com/colored-rs/colored/blob/master/README.md Shows how to add the `colored` crate as a dependency in your project's `Cargo.toml` file. This is the first step to using the crate in your Rust project. ```toml [dependencies] colored = "3" ``` -------------------------------- ### Colorize Trait - Bright Foreground Colors Source: https://context7.com/colored-rs/colored/llms.txt Shows how to apply bright variants of foreground colors using the `bright_` prefix with the Colorize trait methods. ```APIDOC ## Colorize Trait - Bright Foreground Colors ### Description Applies bright variants of foreground colors to strings. ### Usage ```rust use colored::Colorize; println!("{}", "bright black".bright_black()); println!("{}", "bright red".bright_red()); println!("{}", "bright green".bright_green()); println!("{}", "bright yellow".bright_yellow()); println!("{}", "bright blue".bright_blue()); println!("{}", "bright magenta".bright_magenta()); println!("{}", "bright cyan".bright_cyan()); println!("{}", "bright white".bright_white()); ``` ### Methods - `bright_black()` - `bright_red()` - `bright_green()` - `bright_yellow()` - `bright_blue()` - `bright_magenta()` - `bright_cyan()` - `bright_white()` ``` -------------------------------- ### Programmatic Style Composition with Style and Styles Source: https://context7.com/colored-rs/colored/llms.txt Combine, manipulate, and check styles using bitwise operators (`|`, `&`, `!`) and methods like `add` and `remove`. Useful for dynamic style construction. ```Rust use colored::{Style, Styles}; fn main() { // Combine styles with | operator let combined = Styles::Bold | Styles::Underline | Styles::Italic; assert!(combined.contains(Styles::Bold)); assert!(combined.contains(Styles::Underline)); // Remove specific styles with & and ! let mut loud = Style::default() .bold() .underline() .italic() .strikethrough() .hidden(); // Remove hidden and strikethrough let mask = !Style::from_iter([Styles::Hidden, Styles::Strikethrough]); loud &= mask; assert!(!loud.contains(Styles::Hidden)); assert!(!loud.contains(Styles::Strikethrough)); assert!(loud.contains(Styles::Bold)); // Style::add and Style::remove for imperative mutation let mut s = Style::default(); s.add(Styles::Bold); s.add(Styles::Italic); assert!(s.contains(Styles::Italic)); s.remove(Styles::Italic); assert!(!s.contains(Styles::Italic)); } ``` -------------------------------- ### Colorize Trait - Text Styles Source: https://context7.com/colored-rs/colored/llms.txt Details how to apply various text styles like bold, italic, underline, etc., and combine them with colors. ```APIDOC ## Colorize Trait - Text Styles ### Description Applies text styles such as bold, italic, and underline, and allows for clearing styles. ### Usage ```rust use colored::Colorize; println!("{}", "bold text".bold()); println!("{}", "dimmed text".dimmed()); println!("{}", "italic text".italic()); println!("{}", "underlined text".underline()); println!("{}", "blinking text".blink()); println!("{}", "reversed colors".reversed()); println!("{}", "hidden text".hidden()); println!("{}", "strikethrough text".strikethrough()); // Combine colors and styles freely println!("{}", "bold red underlined".red().bold().underline()); println!("{}", "italic cyan on blue".cyan().italic().on_blue()); // Clear all color and style println!("{}", "no color or style".red().bold().clear()); println!("{}", "same as clear".red().bold().normal()); ``` ### Methods - `bold()` - `dimmed()` - `italic()` - `underline()` - `blink()` - `reversed()` - `hidden()` - `strikethrough()` - `clear()` - `normal()` ``` -------------------------------- ### Colorize Trait - Foreground Colors Source: https://context7.com/colored-rs/colored/llms.txt Demonstrates the usage of foreground color methods provided by the Colorize trait for &str and String types. These methods return a ColoredString that can be further styled or printed. ```APIDOC ## Colorize Trait - Foreground Colors ### Description Applies standard foreground colors to strings. ### Usage ```rust use colored::Colorize; println!("{}", "black".black()); println!("{}", "red".red()); println!("{}", "green".green()); println!("{}", "yellow".yellow()); println!("{}", "blue".blue()); println!("{}", "magenta".magenta()); println!("{}", "purple".purple()); // alias for magenta println!("{}", "cyan".cyan()); println!("{}", "white".white()); // Works on owned Strings too println!("{}", String::from("owned string").green().bold()); // Formatting directives are respected println!("{:30}", "padded to 30 chars".blue()); println!("{:.5}", "truncated to 5".green()); ``` ### Methods - `black()` - `red()` - `green()` - `yellow()` - `blue()` - `magenta()` - `purple()` - `cyan()` - `white()` ### Example Output Each word printed in its respective terminal color. ``` -------------------------------- ### Programmatic Style Composition with Style and Styles Source: https://context7.com/colored-rs/colored/llms.txt Enables rich programmatic style construction and manipulation using bitwise operators on the `Style` bitfield and `Styles` enum. ```APIDOC ## `Style` and `Styles` — programmatic style composition `Style` is a bitfield wrapper around `u8`. `Styles` is an enum of individual style switches. Bitwise operators (`|`, `&`, `^`, `!`) are implemented for both types and their combinations, enabling rich programmatic style construction and manipulation. ### Example Usage: ```rust use colored::{Style, Styles}; // Combine styles with | operator let combined = Styles::Bold | Styles::Underline | Styles::Italic; assert!(combined.contains(Styles::Bold)); assert!(combined.contains(Styles::Underline)); // Remove specific styles with & and ! let mut loud = Style::default() .bold() .underline() .italic() .strikethrough() .hidden(); // Remove hidden and strikethrough let mask = !Style::from_iter([Styles::Hidden, Styles::Strikethrough]); loud &= mask; assert!(!loud.contains(Styles::Hidden)); assert!(!loud.contains(Styles::Strikethrough)); assert!(loud.contains(Styles::Bold)); // Style::add and Style::remove for imperative mutation let mut s = Style::default(); s.add(Styles::Bold); s.add(Styles::Italic); assert!(s.contains(Styles::Italic)); s.remove(Styles::Italic); assert!(!s.contains(Styles::Italic)); ``` ``` -------------------------------- ### Dynamic color from string Source: https://context7.com/colored-rs/colored/llms.txt Use `color` and `on_color` to apply colors specified by name strings or hex codes. Handles unknown names by falling back to white. ```APIDOC ## Dynamic color from string — color / on_color ### Description Accepts types that implement `Into`, including `&str` and `String`. Unknown color names default to white. Hex color strings (`#RGB` or `#RRGGBB`) are also supported. For safe parsing, use `str::parse::()`. ### Usage - `color(color_spec: impl Into)`: Sets the foreground color. - `on_color(color_spec: impl Into)`: Sets the background color. ### Supported Formats - Named colors (e.g., "blue", "red") - Hex colors (e.g., "#0057B7", "#fd0") - `Color` enum variants ### Example ```rust use colored::{Color, Colorize}; println!("{}", "blue string".color("blue")); println!("{}", "red background".on_color("red")); println!("{}", "Ukrainian blue".color("#0057B7")); // Safe parsing with fallback let parsed: Result = "zorglub".parse(); println!("{}", "fallback to red".color(parsed.unwrap_or(Color::Red))); ``` ``` -------------------------------- ### Truecolor (24-bit RGB) Source: https://context7.com/colored-rs/colored/llms.txt Apply truecolor (24-bit RGB) to text using `truecolor(r, g, b)` for foreground and `on_truecolor(r, g, b)` for background. Falls back to the closest ANSI color if truecolor is not supported. ```APIDOC ## truecolor / on_truecolor ### Description Accepts three `u8` values for R, G, and B to define a truecolor color. Falls back to the closest standard ANSI color if the terminal does not support truecolor. ### Usage - `truecolor(r: u8, g: u8, b: u8)`: Sets the foreground color. - `on_truecolor(r: u8, g: u8, b: u8)`: Sets the background color. ### Example ```rust use colored::Colorize; println!("{}", "mint green text".truecolor(0, 255, 136)); println!("{}", "purple background".on_truecolor(135, 28, 167)); ``` ``` -------------------------------- ### Use Colorize trait for background colors Source: https://context7.com/colored-rs/colored/llms.txt Apply background colors using the `on_` prefix, which can be chained with foreground colors. The order of chaining does not affect the final output. ```rust use colored::Colorize; fn main() { println!("{}", "red text on blue background".red().on_blue()); println!("{}", "yellow text on magenta background".yellow().on_magenta()); println!("{}", "green on bright yellow".green().on_bright_yellow()); println!("{}", "bright red on bright white".bright_red().on_bright_white()); // Order of chaining does not affect the final result println!("{}", "same result".on_blue().red()); println!("{}", "same result".red().on_blue()); } ``` -------------------------------- ### Nested colored strings Source: https://context7.com/colored-rs/colored/llms.txt Demonstrates how `colored-rs` handles nested `ColoredString` instances, ensuring that inner styles are preserved and outer styles resume correctly after inner resets. ```APIDOC ## Nested colored strings ### Description When a `ColoredString` is embedded within another formatted string and then re-colored, `colored-rs` automatically manages style resets and re-applications to maintain styling integrity throughout the line. ### Behavior Inner styles (like bold or italic) are preserved. The outer style correctly resumes after any inner style reset sequences. ### Example ```rust use colored::Colorize; let world = "world".bold(); let hello = format!("Hello, {world}!lalalala").red(); println!("{{hello}}"); let a = "alpha".italic(); let b = "beta".italic(); let sentence = format!("start {a} middle {b} end").blue(); println!("{{sentence}}"); ``` ``` -------------------------------- ### Truecolor (24-bit RGB) Formatting Source: https://context7.com/colored-rs/colored/llms.txt Use `truecolor(r, g, b)` and `on_truecolor(r, g, b)` for 24-bit RGB colors. The library falls back to the closest standard ANSI color if truecolor is not supported by the terminal. ```rust use colored::Colorize; fn main() { // Foreground truecolor println!("{}", "mint green text".truecolor(0, 255, 136)); // Background truecolor println!("{}", "purple background".on_truecolor(135, 28, 167)); // Combined println!("{}", "custom fg and bg".truecolor(255, 200, 0).on_truecolor(30, 30, 30)); // Ukrainian flag colors println!("{}", "Slava Ukraini!".truecolor(255, 215, 0).on_truecolor(0, 87, 183)); } ``` -------------------------------- ### Disabling Color Output with Cargo Features Source: https://github.com/colored-rs/colored/blob/master/README.md Demonstrates how to disable color output at compile time by using the `no-color` feature of the `colored` crate. This is useful for testing or in environments where color is not supported or desired. ```toml [features] # this effectively enable the feature `no-color` of colored when testing with # `cargo test --features dumb_terminal` dumb_terminal = ["colored/no-color"] ``` -------------------------------- ### ANSI 256-color Source: https://context7.com/colored-rs/colored/llms.txt Apply colors from the ANSI 256-color palette by specifying an index using `ansi_color` and `on_ansi_color`. ```APIDOC ## ANSI 256-color — ansi_color / on_ansi_color ### Description Select a color from the 256-color ANSI palette by its index (`u8`). ### Usage - `ansi_color(index: u8)`: Sets the foreground color using an ANSI 256-color index. - `on_ansi_color(index: u8)`: Sets the background color using an ANSI 256-color index. ### Example ```rust use colored::Colorize; println!("{}", "ansi color 196".ansi_color(196u8)); println!("{}", "ansi color 46 background".on_ansi_color(46u8)); ``` ``` -------------------------------- ### ANSI 256-Color Formatting Source: https://context7.com/colored-rs/colored/llms.txt Apply colors from the 256-color ANSI palette by specifying their index (`u8`). Styles like bold can be combined with ANSI colors. ```rust use colored::Colorize; fn main() { // ANSI color index 196 is bright red, 46 is bright green println!("{}", "ansi color 196".ansi_color(196u8)); println!("{}", "ansi color 46 background".on_ansi_color(46u8)); // Combine with styles println!("{}", "bold ansi 208".ansi_color(208u8).bold()); } ``` -------------------------------- ### Return ColoredString as Box Source: https://context7.com/colored-rs/colored/llms.txt Implement `From for Box` to easily return colored error messages using the `?` operator. Simplifies error handling in functions returning `Result`. ```Rust use colored::Colorize; use std::error::Error; fn validate_input(input: &str) -> Result<(), Box> { if input.is_empty() { // The red ColoredString is converted into a Box automatically return Err("Input must not be empty".red().into()); } Ok(()) } fn main() -> Result<(), Box> { // Using ? operator — ColoredString converts to Box via From impl validate_input("")?; Ok(()) } // Output on error: "Input must not be empty" printed in red to stderr ``` -------------------------------- ### ColoredString as Box Source: https://context7.com/colored-rs/colored/llms.txt ColoredString implements `From for Box`, simplifying the return of colored error messages from functions using the `?` operator. ```APIDOC ## `ColoredString` as `Box` `ColoredString` implements `From for Box`, making it easy to return colored error messages directly from functions using the `?` operator. ### Example Usage: ```rust use colored::Colorize; use std::error::Error; fn validate_input(input: &str) -> Result<(), Box> { if input.is_empty() { // The red ColoredString is converted into a Box automatically return Err("Input must not be empty".red().into()); } Ok(()) } fn main() -> Result<(), Box> { // Using ? operator — ColoredString converts to Box via From impl validate_input("")?; Ok(()) } // Output on error: "Input must not be empty" printed in red to stderr ``` ``` -------------------------------- ### Control Colorization Output with set_override and unset_override Source: https://context7.com/colored-rs/colored/llms.txt Force colorization on or off unconditionally, or restore environment-based detection. Useful for testing or specific output requirements. ```Rust use colored::Colorize; use colored::control; fn main() { // Respects terminal/environment detection (default) println!("{}", "maybe colored".yellow()); // Force color on regardless of environment or TTY control::set_override(true); println!("{}", "always yellow".yellow()); // Force color off control::set_override(false); println!("{}", "never yellow (plain text)".yellow()); // Restore environment-driven behavior control::unset_override(); println!("{}", "environment decides again".yellow()); } ``` -------------------------------- ### Use Colorize trait for bright foreground colors Source: https://context7.com/colored-rs/colored/llms.txt Apply bright variants of foreground colors by prefixing color names with `bright_`. These can be chained with other styles and colors. ```rust use colored::Colorize; fn main() { println!("{}", "bright black".bright_black()); println!("{}", "bright red".bright_red()); println!("{}", "bright green".bright_green()); println!("{}", "bright yellow".bright_yellow()); println!("{}", "bright blue".bright_blue()); println!("{}", "bright magenta".bright_magenta()); println!("{}", "bright cyan".bright_cyan()); println!("{}", "bright white".bright_white()); } ``` -------------------------------- ### Use Colorize trait for text styles Source: https://context7.com/colored-rs/colored/llms.txt Apply text styles like bold, italic, underline, blink, reversed, hidden, and strikethrough by calling their respective methods. Styles and colors can be freely combined. Use `.clear()` or `.normal()` to remove all applied styles and colors. ```rust use colored::Colorize; fn main() { println!("{}", "bold text".bold()); println!("{}", "dimmed text".dimmed()); println!("{}", "italic text".italic()); println!("{}", "underlined text".underline()); println!("{}", "blinking text".blink()); println!("{}", "reversed colors".reversed()); println!("{}", "hidden text".hidden()); println!("{}", "strikethrough text".strikethrough()); // Combine colors and styles freely println!("{}", "bold red underlined".red().bold().underline()); println!("{}", "italic cyan on blue".cyan().italic().on_blue()); // Clear all color and style println!("{}", "no color or style".red().bold().clear()); println!("{}", "same as clear".red().bold().normal()); } ``` -------------------------------- ### Use Colorize trait for foreground colors Source: https://context7.com/colored-rs/colored/llms.txt Apply standard foreground colors to string literals and owned strings using methods from the Colorize trait. Formatting directives are respected. ```rust use colored::Colorize; fn main() { println!("{}", "black".black()); println!("{}", "red".red()); println!("{}", "green".green()); println!("{}", "yellow".yellow()); println!("{}", "blue".blue()); println!("{}", "magenta".magenta()); println!("{}", "purple".purple()); // alias for magenta println!("{}", "cyan".cyan()); println!("{}", "white".white()); // Works on owned Strings too println!("{}", String::from("owned string").green().bold()); // Formatting directives are respected println!("{:30}", "padded to 30 chars".blue()); println!("{:.5}", "truncated to 5".green()); } ``` -------------------------------- ### Enable Virtual Terminal Processing on Windows Source: https://context7.com/colored-rs/colored/llms.txt On Windows 10+, this function enables or disables `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on the console handle, allowing ANSI escape codes to render correctly. ```APIDOC ## `control::set_virtual_terminal` (Windows only) On Windows 10+, enables or disables `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on the console handle so ANSI escape codes render correctly in PowerShell and CMD. ### Example Usage: ```rust // Only compiles and has effect on Windows targets #[cfg(windows)] fn main() { use colored::Colorize; use colored::control; control::set_virtual_terminal(true).unwrap(); println!("{}", "rendered correctly in green".bright_green()); control::set_virtual_terminal(false).unwrap(); // ANSI escape chars will appear as raw text on Windows without VT println!("{}", "escape codes visible as text".bright_red()); control::set_virtual_terminal(true).unwrap(); } ``` ``` -------------------------------- ### Dynamic Color from String Parsing Source: https://context7.com/colored-rs/colored/llms.txt Use `color` and `on_color` with string inputs for named colors or hex codes (`#RGB`, `#RRGGBB`). Unknown names default to white. Safe parsing with fallback is available via `str::parse::()`. ```rust use colored::{Color, Colorize}; fn main() { // Named color string println!("{}", "blue string".color("blue")); println!("{}", "red background".on_color("red")); // Hex colors (3-digit and 6-digit) println!("{}", "Ukrainian blue".color("#0057B7")); println!("{}", "Ukrainian yellow bg".on_color("#fd0")); // Unknown name defaults to white println!("{}", "defaults to white".color("zorglub")); // Safe parsing with fallback let parsed: Result = "zorglub".parse(); println!("{}", "fallback to red".color(parsed.unwrap_or(Color::Red))); // Case-insensitive println!("{}", "BLUE works".color("BLUE")); println!("{}", "mixed case".color("bLuE")); } ``` -------------------------------- ### Control Colorization Behavior Source: https://context7.com/colored-rs/colored/llms.txt Functions to force colorization on or off, or restore environment-based detection for the entire library. ```APIDOC ## `control::set_override` / `control::unset_override` `set_override(true)` forces colorization on unconditionally. `set_override(false)` forces it off. `unset_override()` restores environment-based detection. These functions delegate to the global `SHOULD_COLORIZE` lazy static. ### Example Usage: ```rust use colored::Colorize; use colored::control; // Respects terminal/environment detection (default) println!("{}", "maybe colored".yellow()); // Force color on regardless of environment or TTY control::set_override(true); println!("{}", "always yellow".yellow()); // Force color off control::set_override(false); println!("{}", "never yellow (plain text)".yellow()); // Restore environment-driven behavior control::unset_override(); println!("{}", "environment decides again".yellow()); ``` ``` -------------------------------- ### Enable Virtual Terminal Processing on Windows Source: https://context7.com/colored-rs/colored/llms.txt Enable or disable `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on Windows 10+ to ensure correct rendering of ANSI escape codes. This function is Windows-specific. ```Rust // Only compiles and has effect on Windows targets #[cfg(windows)] fn main() { use colored::Colorize; use colored::control; control::set_virtual_terminal(true).unwrap(); println!("{}", "rendered correctly in green".bright_green()); control::set_virtual_terminal(false).unwrap(); // ANSI escape chars will appear as raw text on Windows without VT println!("{}", "escape codes visible as text".bright_red()); control::set_virtual_terminal(true).unwrap(); } ``` -------------------------------- ### CustomColor Struct for Reusable Colors Source: https://context7.com/colored-rs/colored/llms.txt Define reusable named RGB colors using the `CustomColor` struct. It implements `From<(u8, u8, u8)>`, allowing tuples to be used directly. ```rust use colored::{Colorize, CustomColor}; fn main() { // Create a reusable named color let teal = CustomColor::new(0, 120, 120); println!("{}", "Greetings from Ukraine".custom_color(teal)); println!("{}", "Slava Ukraini!".on_custom_color(teal)); // Inline tuple — no explicit CustomColor needed println!("{}", "Hello World!".on_custom_color((0, 120, 120))); println!("{}", "fg from tuple".custom_color((255, 128, 0))); } ``` -------------------------------- ### CustomColor struct Source: https://context7.com/colored-rs/colored/llms.txt Define and reuse custom RGB colors using the `CustomColor` struct or by passing tuples directly to `custom_color` and `on_custom_color`. ```APIDOC ## CustomColor struct — custom_color / on_custom_color ### Description `CustomColor` allows you to define a named RGB color that can be stored and reused. It implements `From<(u8, u8, u8)>`, enabling the use of plain tuples. ### Usage - `CustomColor::new(r: u8, g: u8, b: u8)`: Creates a new `CustomColor` instance. - `custom_color(color: CustomColor | (u8, u8, u8))`: Sets the foreground color. - `on_custom_color(color: CustomColor | (u8, u8, u8))`: Sets the background color. ### Example ```rust use colored::{Colorize, CustomColor}; let teal = CustomColor::new(0, 120, 120); println!("{}", "Greetings from Ukraine".custom_color(teal)); println!("{}", "Slava Ukraini!".on_custom_color(teal)); println!("{}", "Hello World!".on_custom_color((0, 120, 120))); ``` ``` -------------------------------- ### Direct Field Manipulation of ColoredString Source: https://context7.com/colored-rs/colored/llms.txt Allows for in-place modification of a ColoredString's text, foreground color, background color, and style without re-allocating via the builder API. ```APIDOC ## `ColoredString` struct — direct field manipulation `ColoredString` exposes `input: String`, `fgcolor: Option`, `bgcolor: Option`, and `style: Style` as public fields for direct in-place manipulation without re-allocating via the builder API. ### Example Usage: ```rust use colored::{Color, ColoredString, Colorize, Style, Styles}; let mut cs = "Hello".red().bold(); cs.fgcolor = Some(Color::Blue); // change fg color in place cs.input = "World".to_string(); // change text in place println!("{cs}"); // prints "World" in bold blue let mut cs2 = "styled".green().italic().on_yellow(); cs2.clear_fgcolor(); // removes green cs2.clear_bgcolor(); // removes yellow background cs2.clear_style(); // removes italic assert!(cs2.is_plain()); let s1 = "bold".bold(); let s2 = "italic".italic(); let mut combined = ColoredString::from("bold and italic"); combined.style = s1.style | s2.style; println!("{combined}"); let style = Style::from_iter([Styles::Bold, Styles::Italic, Styles::Underline]); let mut cs3 = ColoredString::from("multi-style"); cs3.style = style; println!("{cs3}"); ``` ``` -------------------------------- ### Rust code with `no-color` feature Source: https://context7.com/colored-rs/colored/llms.txt When the `no-color` feature is enabled (e.g., via `cargo test --features dumb_terminal`), this Rust code will print plain text. Without the feature, it prints colored text. ```rust // With `cargo test --features dumb_terminal`, all colored output is plain text use colored::Colorize; fn main() { // With no-color feature: prints "hello" without any escape codes // Without no-color feature: prints "hello" in red println!("{}", "hello".red()); } ``` -------------------------------- ### Handling Nested Colored Strings Source: https://context7.com/colored-rs/colored/llms.txt When a `ColoredString` is nested within another formatted string and re-colored, the library automatically re-inserts the outer style after inner reset sequences to maintain styling. ```rust use colored::Colorize; fn main() { // Inner bold word inside an outer red string let world = "world".bold(); let hello = format!("Hello, {world}!lalalala").red(); println!("{hello}"); // Output: "Hello, " is red, "world" is bold (inner style preserved), // "!lalalala" resumes red correctly. // Multiple nested strings let a = "alpha".italic(); let b = "beta".italic(); let sentence = format!("start {a} middle {b} end").blue(); println!("{sentence}"); // Each italic segment resets and the outer blue resumes afterward } ``` -------------------------------- ### Mutate ColoredString Fields Directly Source: https://context7.com/colored-rs/colored/llms.txt Modify `input`, `fgcolor`, `bgcolor`, and `style` fields of a `ColoredString` in place without re-allocation. Useful for quick updates after initial creation. ```Rust use colored::{Color, ColoredString, Colorize, Style, Styles}; fn main() { // Build via Colorize, then mutate fields let mut cs = "Hello".red().bold(); cs.fgcolor = Some(Color::Blue); // change fg color in place cs.input = "World".to_string(); // change text in place println!("{cs}"); // prints "World" in bold blue // Clear individual aspects let mut cs2 = "styled".green().italic().on_yellow(); cs2.clear_fgcolor(); // removes green cs2.clear_bgcolor(); // removes yellow background cs2.clear_style(); // removes italic assert!(cs2.is_plain()); // Combine styles from two ColoredStrings let s1 = "bold".bold(); let s2 = "italic".italic(); let mut combined = ColoredString::from("bold and italic"); combined.style = s1.style | s2.style; println!("{combined}"); // Build a Style from an iterator let style = Style::from_iter([Styles::Bold, Styles::Italic, Styles::Underline]); let mut cs3 = ColoredString::from("multi-style"); cs3.style = style; println!("{cs3}"); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.