Skip to content

Fix height and filler when wrapped at the middle of wide characters#799

Open
kojiishi wants to merge 1 commit into
console-rs:mainfrom
kojiishi:cjk
Open

Fix height and filler when wrapped at the middle of wide characters#799
kojiishi wants to merge 1 commit into
console-rs:mainfrom
kojiishi:cjk

Conversation

@kojiishi
Copy link
Copy Markdown
Contributor

@kojiishi kojiishi commented Apr 29, 2026

When a wide character such as CJK appears at the end of wrap with only 1 column available, the line wraps before the character, leaving an empty column.

This patch fixes wrapped_height to take them into account, by computing each wrap instead of dividing by the width.

Fixes #798.

@kojiishi kojiishi marked this pull request as ready for review April 29, 2026 08:24
@kojiishi kojiishi force-pushed the cjk branch 9 times, most recently from 713282e to a48771a Compare May 5, 2026 16:20
@kojiishi
Copy link
Copy Markdown
Contributor Author

kojiishi commented May 5, 2026

Updated the commit message to be more descriptive.

Comment thread src/draw_target.rs Outdated
}
}
}
let effective_width = (num_lines - 1) * width + column;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure effective_width is a good name for this? At least it seems confusing that we're multiplying num_lines with a width and also calling the result a width.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Renamed to width_sum. How about this?

Copy link
Copy Markdown
Contributor Author

@kojiishi kojiishi May 6, 2026

Choose a reason for hiding this comment

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

I got another idea; I made it last_line_width, and modified draw_to_term accordingly. This saves some CPU cycles with unicode-width, adds some cycles when only height is needed without unicode-width, but subtle. Does this look clearer?

Comment thread src/draw_target.rs Outdated
continue;
}
for ch in substr.chars() {
let ch_width = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please import UnicodeWidthChar at the top of the module. I think the unwrap_or(0) here is a little weird. I think we can do let Some(ch_width) = .. else { continue }; instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, and thank you for the good suggestion again, done.

Comment thread src/draw_target.rs
Comment thread src/draw_target.rs Outdated
}

#[cfg(not(feature = "unicode-width"))]
fn wrapped_height_and_effective_width(&self, width: usize) -> (VisualLines, usize) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find this name a little verbose. Maybe something like metrics? Could even introduce a Metrics struct for the return type, maybe.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestion, I like that. Introduced a Metrics struct.

@djc
Copy link
Copy Markdown
Member

djc commented May 6, 2026

What are your uses for? Is indicatif being used in Chromium?

@kojiishi
Copy link
Copy Markdown
Contributor Author

kojiishi commented May 6, 2026

What are your uses for? Is indicatif being used in Chromium?

No, this is for my personal project https://github.com/kojiishi/compare-dir, half for own use and half for learning Rust.

@kojiishi kojiishi force-pushed the cjk branch 2 times, most recently from 35f7856 to 37599fb Compare May 7, 2026 05:17
When a wide character such as CJK appears at the end of wrap with only 1 column available, the line wraps before the character, leaving an empty column.

This patch fixes `wrapped_height` to take them into account, by computing each wrap instead of dividing by the width.

Fixes console-rs#798.
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.

Rendering failure when wrapped at wide characters such as CJK

2 participants