colored-rs / colored

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

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

Closed ayrapetovai closed 3 years ago

ayrapetovai commented 3 years ago

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);
}
spenserblack commented 3 years ago

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.

kurtlawrence commented 3 years ago

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

ayrapetovai commented 3 years ago

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...