Skip to content

Fix wide_bar width computation with a multiline message#738

Merged
djc merged 1 commit intoconsole-rs:mainfrom
glehmann:gln/major-updates-and-fixes-nlun
Oct 21, 2025
Merged

Fix wide_bar width computation with a multiline message#738
djc merged 1 commit intoconsole-rs:mainfrom
glehmann:gln/major-updates-and-fixes-nlun

Conversation

@glehmann
Copy link
Contributor

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.

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think spelling it like this would be easier to understand:

&match cur.split_once('\n') {
     Some((first, _)) => first,
     None => &cur,
}.replace('\x00', "")

@glehmann
Copy link
Contributor Author

Is it always the first line, or is it the line in which the bar appears?

I suppose it can be anywhere, indeed.

So maybe rather split the text in lines, find the line with \x00 and measure its width?

@glehmann glehmann force-pushed the gln/major-updates-and-fixes-nlun branch from c259ce1 to 55cb070 Compare October 20, 2025 12:45
@glehmann glehmann force-pushed the gln/major-updates-and-fixes-nlun branch from 55cb070 to f64c6d1 Compare October 20, 2025 12:55
@glehmann glehmann changed the title Fix wide_bar width computation with multiline a message Fix wide_bar width computation with a multiline message Oct 20, 2025
@glehmann glehmann force-pushed the gln/major-updates-and-fixes-nlun branch from f64c6d1 to f9f6432 Compare October 20, 2025 15:56
src/style.rs Outdated
Comment on lines +467 to +470
match &cur.lines().find(|line| line.contains('\x00')) {
Some(line) => measure_text_width(&line.replace('\x00', "")),
None => measure_text_width(&cur),
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
            },
        ));

@glehmann glehmann force-pushed the gln/major-updates-and-fixes-nlun branch from f9f6432 to ff4fe42 Compare October 20, 2025 17:15
@djc djc merged commit e69d621 into console-rs:main Oct 21, 2025
10 checks passed
@glehmann glehmann deleted the gln/major-updates-and-fixes-nlun branch October 21, 2025 08:53
@glehmann
Copy link
Contributor Author

thanks!

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

Successfully merging this pull request may close these issues.

2 participants