colored-rs / colored

(Rust) Coloring terminal so simple you already know how to do it !
Mozilla Public License 2.0
1.68k stars 82 forks source link

Coloring for write! #117

Closed shusheaan closed 1 year ago

shusheaan commented 1 year ago

Hi there,

This crate is awesome!!! Thanks for the work.

I'm trying to apply color in write!() instead of println!(), since I'm writing fmt::Display for my own enum and it will be println! later with other variables. I would like to define different colors for different enum value so that it's visually good. How can I do that?

To be specific, here's the code

enum Type {
    A,
    B,
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
             Type::A => {write!(f, "A")}, // can I add color here? like red for A and green for B?
             Type::B => {write!(f, "B")},
        }
    }
}

Thanks

shusheaan commented 1 year ago

directly writing "A".red() shows a compiler error: "argument never used" pointing ".red()"