anstream println field witdth is not aware of color codes
Open
Nobody has claimed this yet.
A-stream
C-bug
- Dominant language
- HTML
- Stars
- 171
- Forks
- 44
- Avg merge
- 2h 34m
- Merged PRs (30d)
- 6
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Rust example from the issue and trace how anstream's println formatting handles width when a Display implementation emits ANSI color codes. Identify the relevant formatting path and add coverage for the example; done means colored values align with their uncolored equivalents.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100