jam1garner / owo-colors

A zero-allocation no_std-compatible zero-cost way to add color to your Rust terminal
MIT License
639 stars 33 forks source link

Can't apply multiple formatters in if_supports_color #39

Open sunshowers opened 2 years ago

sunshowers commented 2 years ago

With input:

println!(
    "{}",
    "colored blue if a supported terminal"
        .if_supports_color(Stdout, |text| text.bold().bright_blue())
);

I get the output:

  --> src/lib.rs:46:43
   |
10 |         .if_supports_color(Stdout, |text| text.bold().bright_blue())
   |                                           -----------^^^^^^^^^^^^^^
   |                                           |
   |                                           returns a reference to data owned by the current function
   |                                           temporary value created here

It looks like each formatter creates a temporary, causing issues if multiple formatters are applied.

I suspect this may need to be a macro rather than a function call...

sunshowers commented 2 years ago

Looks like I can work around this with a Style but just wanted to flag this.

jam1garner commented 2 years ago

I will note that this issue only occurs with non-color styles. For example .bright_blue().on_red() will work (outside of being an eye sore). It's definitely possible to support this without a macro, but with my current setup that'd suck. I think possibly specializing the method to auto-convert it to a Style once you've applied both a color and a modifier (bold, italics, etc) would fix this though. I'm currently in the middle of moving but hopefully once I'm less busy I'll have time

Thank you for the issue!