Trait kdam::term::Colorizer

source ·
pub trait Colorizer {
    // Required methods
    fn colorize(&self, code: &str) -> String;
    fn trim_ansi(&self) -> String;
    fn len_ansi(&self) -> usize;
}
Expand description

Add ANSI colour escape codes to the given text for printing coloured text in terminal.

Required Methods§

source

fn colorize(&self, code: &str) -> String

Add ANSI colour escape codes to the given text.

Example
use kdam::term::Colorizer;

// ANSI [8-16; 4-bit]

println!("{}", "hello world!".colorize("bold red"));
println!("{}", "hello world!".colorize("bright white on blue"));

// ANSI [256; 8-bit]

println!("{}", "hello world!".colorize("ansi(200)"));
println!("{}", "hello world!".colorize("ansi(0) on ansi(255)"));

// True Colours [(0-255, 0-255, 0-255); 24-bit]

println!("{}{}",
    "hello".colorize("#171717 on #00de6d"),
    " world!".colorize("#ffffff on #007272")
);
println!("{}{}",
    "hello".colorize("rgb(23,23,23) on rgb(0,255,109)"),
    " world!".colorize("rgb(255,255,255) on rgb(0,144,144)")
);
source

fn trim_ansi(&self) -> String

Inverse of colorize method. This method trims all ANSI escape codes from given string.

source

fn len_ansi(&self) -> usize

Returns display length of string by triming ANSI escape codes.

Implementations on Foreign Types§

source§

impl Colorizer for str

source§

fn colorize(&self, code: &str) -> String

source§

fn trim_ansi(&self) -> String

source§

fn len_ansi(&self) -> usize

Implementors§