Skip to content

Use ellipses when truncating progress #15330

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/cargo/util/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ impl<'gctx> Progress<'gctx> {
/// * `cur` should be how far along the progress is.
/// * `max` is the maximum value for the progress bar.
/// * `msg` is a small piece of text to display at the end of the progress
/// bar. It will be truncated with `...` if it does not fit on the
/// terminal.
/// bar. It will be truncated with `…` if it does not fit on the terminal.
///
/// This may not actually update the display if `tick` is being called too
/// quickly.
Expand Down Expand Up @@ -521,20 +520,20 @@ impl Format {
fn render(&self, string: &mut String, msg: &str) {
let mut avail_msg_len = self.max_width - string.len() - 15;
let mut ellipsis_pos = 0;
if avail_msg_len <= 3 {
if avail_msg_len <= 1 {
return;
}
for c in msg.chars() {
let display_width = c.width().unwrap_or(0);
if avail_msg_len >= display_width {
avail_msg_len -= display_width;
string.push(c);
if avail_msg_len >= 3 {
if avail_msg_len >= 1 {
ellipsis_pos = string.len();
}
} else {
string.truncate(ellipsis_pos);
string.push_str("...");
string.push_str("");
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not unconditionally output unicode characters but need to check either shell().out_unicode() or shell().err_unicode().

Copy link
Author

Choose a reason for hiding this comment

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

Unicode is a superset of ascii. I assume you mean non-ascii Unicode?

It actually slipped my mind that this is non-ascii. I'll update this and review the tests below.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unicode is a superset of ascii. I assume you mean non-ascii Unicode?

Yes

break;
}
}
Expand Down Expand Up @@ -610,7 +609,7 @@ fn test_progress_status() {
);
assert_eq!(
format.progress_status(3, 4, ": msg that's just fit"),
Some("[=============> ] 3/4: msg that's just...".to_string())
Some("[=============> ] 3/4: msg that's just f…".to_string())
);

// combining diacritics have width zero and thus can fit max_width.
Expand All @@ -623,16 +622,16 @@ fn test_progress_status() {
// some non-ASCII ellipsize test
assert_eq!(
format.progress_status(3, 4, "_123456789123456e\u{301}\u{301}8\u{301}90a"),
Some("[=============> ] 3/4_123456789123456e\u{301}\u{301}...".to_string())
Some("[=============> ] 3/4_123456789123456e\u{301}\u{301}8\u{301}9…".to_string())
);
assert_eq!(
format.progress_status(3, 4, ":每個漢字佔據了兩個字元"),
Some("[=============> ] 3/4:每個漢字佔據了...".to_string())
Some("[=============> ] 3/4:每個漢字佔據了兩…".to_string())
);
assert_eq!(
// handle breaking at middle of character
format.progress_status(3, 4, ":-每個漢字佔據了兩個字元"),
Some("[=============> ] 3/4:-每個漢字佔據了...".to_string())
Some("[=============> ] 3/4:-每個漢字佔據了兩…".to_string())
);
}

Expand Down