Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't apply multiple formatters in if_supports_color #39

Open
sunshowers opened this issue Dec 6, 2021 · 2 comments
Open

Can't apply multiple formatters in if_supports_color #39

sunshowers opened this issue Dec 6, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@sunshowers
Copy link
Contributor

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
Copy link
Contributor Author

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

@jam1garner
Copy link
Owner

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!

@jam1garner jam1garner added the bug Something isn't working label Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants