Fix wide_bar width computation with a multiline message#738
Fix wide_bar width computation with a multiline message#738djc merged 1 commit intoconsole-rs:mainfrom
Conversation
djc
left a comment
There was a problem hiding this comment.
Nice catch!
Is it always the first line, or is it the line in which the bar appears?
src/style.rs
Outdated
| ) -> String { | ||
| let left = (width as usize).saturating_sub(measure_text_width(&cur.replace('\x00', ""))); | ||
| let left = (width as usize).saturating_sub(measure_text_width( | ||
| &cur.split_once('\n') |
There was a problem hiding this comment.
I think spelling it like this would be easier to understand:
&match cur.split_once('\n') {
Some((first, _)) => first,
None => &cur,
}.replace('\x00', "")
I suppose it can be anywhere, indeed. So maybe rather split the text in lines, find the line with |
c259ce1 to
55cb070
Compare
55cb070 to
f64c6d1
Compare
f64c6d1 to
f9f6432
Compare
src/style.rs
Outdated
| match &cur.lines().find(|line| line.contains('\x00')) { | ||
| Some(line) => measure_text_width(&line.replace('\x00', "")), | ||
| None => measure_text_width(&cur), | ||
| }, |
There was a problem hiding this comment.
I think we should be able to hoist the measure_text_width() call out of the match? I also don't think it makes sense to take a & of the scrutinee.
There was a problem hiding this comment.
I think the & just after the match can be removed. I though clippy would have complained about that…
measure_text_width(s: &str) forces us to pass a reference.
line.replace() returns a String so we would have to cur.clone() in the other match arm if we want to put the measure_text_width() out of the match.
I suppose we could use a variable outside the match to hold the output of line.replace(), but that would be less compact code and would not be more efficient.
let line0;
let left = (width as usize).saturating_sub(measure_text_width(
match cur.lines().find(|line| line.contains('\x00')) {
Some(line) => {
line0 = line.replace('\x00', "");
&line0
}
None => &cur,
},
));f9f6432 to
ff4fe42
Compare
|
thanks! |
Before this PR, the whole width of the message was used to compute the bar width, making the bar very small or invisible.
In practice, only the first line of the message should have an impact on the bar width.