Text that was not handled by format! does not get painted
ayrapetovai opened this issue · 3 comments
ayrapetovai commented
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
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
That is correct. I also believe ColoredString.to_string()
would work, but I have not tested this.
ayrapetovai commented
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...