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

anstream println field witdth is not aware of color codes #167

Open
VorpalBlade opened this issue Jan 28, 2024 · 4 comments
Open

anstream println field witdth is not aware of color codes #167

VorpalBlade opened this issue Jan 28, 2024 · 4 comments
Labels
A-stream Area: anstream C-bug Category: Things not working as expected

Comments

@VorpalBlade
Copy link

VorpalBlade commented Jan 28, 2024

I would expect the following (rather silly minimal case) to align columns properly:

use anstream::println;
use anstyle::AnsiColor;
use anstyle::Reset;

enum MyLogLevel {
    Ok,
    Warn,
    Error,
}

impl std::fmt::Display for MyLogLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MyLogLevel::Ok => write!(f, "{}Ok{}", AnsiColor::Green.render_fg(), Reset.render()),
            MyLogLevel::Warn => write!(
                f,
                "{}Warning{}",
                AnsiColor::Yellow.render_fg(),
                Reset.render()
            ),
            MyLogLevel::Error => write!(f, "{}Error{}", AnsiColor::Red.render_fg(), Reset.render()),
        }
    }
}

fn main() {
    println!("{:<10} Everything is fine", "Ok");
    println!("{:<10} Be careful", "Warning");
    println!("{:<10} BAD BAD BAD", "Error");

    println!("{:<10} Everything is fine", MyLogLevel::Ok);
    println!("{:<10} Be careful", MyLogLevel::Warn);
    println!("{:<10} BAD BAD BAD", MyLogLevel::Error);
}

It prints:

Ok         Everything is fine
Warning    Be careful
Error      BAD BAD BAD
Ok Everything is fine
Warning Be careful
Error BAD BAD BAD

This is unexpected. So it seems the anstream println is not properly aware of how wide colour codes are.

@epage epage added A-stream Area: anstream C-bug Category: Things not working as expected labels Jan 29, 2024
@epage
Copy link
Collaborator

epage commented Jan 29, 2024

I ran into something sort-of similar in env_logger. The way I worked around it is controlling what i used write! on vs what I called .fmt(f) on, see rust-cli/env_logger@303a9c0

From that experience, I think the problem is that write! doesn't handle any of the format specifiers itself and does not forward them on. I reproduced this with std at https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0409fd1ae8ddd4d6b6e98ec9903dce7e

So to get his to work, you'd need to do something similar to what I did in env_logger, use write! for the styling and .fmt(f) on the text.

@VorpalBlade
Copy link
Author

VorpalBlade commented Feb 3, 2024

@epage Thank you, this does indeed work! Perhaps it should be documented in anstyle somewhere? It seems like a bit of a gotcha.

@epage
Copy link
Collaborator

epage commented Feb 5, 2024

I'm not sure how to fit this in. This is a problem specifically when you are creating a wrapper type for Display and is a problem independent of whether anstyle is in use or not.

@VorpalBlade
Copy link
Author

That is a fair point. I would assume it is somewhat common to abstract away and use traits though. The code ends up a lot nicer that way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stream Area: anstream C-bug Category: Things not working as expected
Projects
None yet
Development

No branches or pull requests

2 participants