Trait Colorize

Source
pub trait Colorize: Display {
    // Provided methods
    fn color(&self, color: Color) -> String { ... }
    fn background(&self, color: Color) -> String { ... }
}
Expand description

Trait for applying colors to strings.

This trait provides the ability to color text using ANSI escape codes.

Provided Methods§

Source

fn color(&self, color: Color) -> String

Applies a color to the string.

§Arguments
  • color - The color to apply
§Returns

A new string with the color applied via ANSI escape codes

Examples found in repository?
examples/strings.rs (line 7)
3fn main() {
4    println!(
5        "{}",
6        "This is yellow text on a cyan background!"
7            .color(Color::Yellow)
8            .background(Color::Cyan)
9    );
10
11    println!("{}", "This is bold text!".bold().color(Color::Red));
12    println!("{}", "This is dim text!".dim());
13    println!("{}", "This is italic text!".italic());
14    println!("{}", "This is underlined text!".underline());
15    println!(
16        "{}",
17        "This is a styled message!".bold().italic().underline()
18    );
19    println!("{}", "Blinking text!".blink());
20    println!("{}", "Reversed background!".reverse());
21    println!("{}", "Hidden text!".hidden());
22    println!("{}", "Something wrong.".striketrough());
23
24    println!("{}", "I like cyan".color(Color::BrightCyan));
25}
Source

fn background(&self, color: Color) -> String

Applies a background color to the string.

§Arguments
  • color - The color to apply
§Returns

A new string with the background color applied via ANSI escape codes

Examples found in repository?
examples/strings.rs (line 8)
3fn main() {
4    println!(
5        "{}",
6        "This is yellow text on a cyan background!"
7            .color(Color::Yellow)
8            .background(Color::Cyan)
9    );
10
11    println!("{}", "This is bold text!".bold().color(Color::Red));
12    println!("{}", "This is dim text!".dim());
13    println!("{}", "This is italic text!".italic());
14    println!("{}", "This is underlined text!".underline());
15    println!(
16        "{}",
17        "This is a styled message!".bold().italic().underline()
18    );
19    println!("{}", "Blinking text!".blink());
20    println!("{}", "Reversed background!".reverse());
21    println!("{}", "Hidden text!".hidden());
22    println!("{}", "Something wrong.".striketrough());
23
24    println!("{}", "I like cyan".color(Color::BrightCyan));
25}

Implementations on Foreign Types§

Source§

impl Colorize for str

Source§

impl Colorize for String

Implementors§