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

It's not obvious how to use different colors in if-else #120

Open
kriomant opened this issue Feb 1, 2024 · 0 comments
Open

It's not obvious how to use different colors in if-else #120

kriomant opened this issue Feb 1, 2024 · 0 comments

Comments

@kriomant
Copy link

kriomant commented Feb 1, 2024

I tried to print different words with different colours depending on condition:

println!("operation is {}",
    if op.success {
        "successful".bright_green()
    } else {
        "failed".bright_red()
    }
);

This doesn't work because color is encoded into type of returned wrapper.

I've managed to solve it using:

println!("operation is {}",
    if op.success {
        "successful".color(AnsiColors::BrightGreen)
    } else {
        "failed".color(AnsiColors::BrightRed)
    }
);

But it would be cool to be able to perform such conversion more conveniently, so you don't have to rewrite code that much (function name may be arbitrary):

println!("operation is {}",
    if op.success {
        "successful".bright_green().downgrade() // converts statically-known color to common type which stores color as value
    } else {
        "failed".bright_red().downgrade()
    }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant