colored-rs / colored

(Rust) Coloring terminal so simple you already know how to do it !

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Text that was not handled by format! does not get painted

ayrapetovai opened this issue · comments

Hi, I have a strange issue. First of those two ways does not work, the second does (paints text with green as expected).

use colored::{Colorize, Color};

fn main() {
    let mut buffer = String::with_capacity(400);
    buffer.push_str(&*"supposed to be green text".color(Color::BrightGreen));                            // does not work
    buffer.push_str(format!("{}", "supposed to be green text".color(Color::BrightGreen)).as_str());      // works
    println!("{}", buffer);
}

For the first way, ColoredString dereferences to the unstyled str, so I believe that's why the style is being removed when you use &*. When you use format!, then the ColoredString is properly formatted with the styles.

That is correct. I also believe ColoredString.to_string() would work, but I have not tested this.

Thanks, now I see, &"".color(Color::BrightGreen).to_string() works.
Still cant understand why Deref is implemented this way, returns standard String with no escape chars written to it...