Crate consclr

Crate consclr 

Source
Expand description

A library to colorized your terminal This library provides traits that give extension function for &str, &String and String and also macro.

§1. Traits

There are two types of traits that available, the fixed 256 traits and traits with generic type to work with dynamic buffer size.

§256 examples

 
fn create_text() -> (&'static str, String, &'static str, &'static str) {
    let m1: &str = "Success!";
    let m2: String = "Information!".to_string();
    let m3: &str = "Failed!";
    let m4 = "Error!";
    (m1, m2, m3, m4)
}
 
fn regular(args: (&str, String, &str, &str)) {
    use consclr::Color256;
 
    println!("{}", args.0.green());
    println!("{}", args.1.blue());
    println!("{}", args.2.purple());
    println!("{}", args.3.red());
}

fn underline(args: (&str, String, &str, &str)) {
    use consclr::ColorUl256;
 
    println!("{}", args.0.ugreen());
    println!("{}", args.1.ublue());
    println!("{}", args.2.upurple());
    println!("{}", args.3.ured());
}
 
fn background(args: (&str, String, &str, &str)) {
    use consclr::ColorBg256;
 
    println!("{}", args.0.bggreen());
    println!("{}", args.1.bgblue());
    println!("{}", args.2.bgpurple());
    println!("{}", args.3.bgred());
}
 
fn bold(args: (&str, String, &str, &str)) {
    use consclr::ColorBold256;
 
    println!("{}", args.0.greenb());
    println!("{}", args.1.blueb());
    println!("{}", args.2.purpleb());
    println!("{}", args.3.redb());
}
 
fn main() {
    regular(create_text());
    underline(create_text());
    background(create_text());
    bold(create_text());
}

§dynamic generic type

fn create_text() -> (&'static str, String, &'static str, &'static str) {
    let m1: &str = "Success!";
    let m2: String = "Information!".to_string();
    let m3: &str = "Failed!";
    let m4 = "Error!";
    (m1, m2, m3, m4)
}
 
fn regular(args: (&str, String, &str, &str)) {
    use consclr::Color; 
 
    // use buffer with size 32 bytes
    println!("{}", args.0.dgreen::<32>());
    println!("{}", args.1.dblue::<32>());
    println!("{}", args.2.dpurple::<32>());
    println!("{}", args.3.dred::<32>());
}
 
fn main() {
    regular(create_text());
}

Modules§

colors
helper
macros
prelude

Macros§

ceprint
print with color on stderr
clr
Print without using buffer (Print with newline). Because this macro does not store text into the buffer, this macro can print very long text
clr2
Print without using buffer (Print without newline). Because this macro does not store text into the buffer, this macro can print very long text
colorized
helper macro for internal use
cprint
print with color on stdout
pchar
Print char without using buffer. Because this macro does not store text into the buffer, this macro can print very long text. You can specify an argument how many time you want to repeat printing the char.
to_color
convert a &str to color If the argument is invalid, return a default color instead (default color = white, default bg = black)
to_colorb
convert a &str to color bold If the argument is invalid, return a default color instead (default color = white, default bg = black)

Traits§

Color
This trait receive const generic to specify the buffer size A trait that contain methods to format self with regular color once this trait imported, &str, &String and String will have extension method to format it with color on terminal.
Color256
A trait that contain methods to format self with regular color once this trait imported, &str, &String and String will have extension method to format it with color on terminal
ColorBg
This trait receive const generic to specify the buffer size A trait that contain methods to format self with colorful background once this trait imported, &str, &String and String will have extension method to format it with colorful background on terminal.
ColorBg256
A trait that contain methods to format self with background color once this trait imported, &str, &String and String will have extension method to format it with bg color on terminal
ColorBold
This trait receive const generic to specify the buffer size A trait that contain methods to format self with color and bold once this trait imported, &str, &String and String will have extension method to format it with color+bold on terminal.
ColorBold256
A trait that contain methods to format self with color and Bold once this trait imported, &str, &String and String will have extension method to format it with color and Bold on terminal
ColorUl
A trait that contain methods to format self with background color once this trait imported, &str, &String and String will have extension method to format it with bg color on terminal
ColorUl256
A trait that contain methods to format self with background color once this trait imported, &str, &String and String will have extension method to format it with bg color on terminal