Skip to content

Commit faa4915

Browse files
authored
feat(cli): Use ellipses when truncating progress (#15955)
### What does this PR try to resolve? Use ellipses when truncating progress instead of three periods. While this allows an extra two characters to fit on screen, the main motivation it to reduce the visual quirkiness or the resulting output. ### How to test and review this PR? This applies the review feedback left in #15330 Closes #15330
2 parents 00c7c80 + d9891ed commit faa4915

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/cargo/util/progress.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct Format {
7575
max_width: usize,
7676
max_print: usize,
7777
term_integration: TerminalIntegration,
78+
unicode: bool,
7879
}
7980

8081
/// Controls terminal progress integration via OSC sequences.
@@ -229,6 +230,7 @@ impl<'gctx> Progress<'gctx> {
229230
// even on narrow (e.g. 80 char) terminals.
230231
max_print: 50,
231232
term_integration: TerminalIntegration::from_config(gctx),
233+
unicode: gctx.shell().err_unicode(),
232234
},
233235
name: name.to_string(),
234236
done: false,
@@ -261,8 +263,7 @@ impl<'gctx> Progress<'gctx> {
261263
/// * `cur` should be how far along the progress is.
262264
/// * `max` is the maximum value for the progress bar.
263265
/// * `msg` is a small piece of text to display at the end of the progress
264-
/// bar. It will be truncated with `...` if it does not fit on the
265-
/// terminal.
266+
/// bar. It will be truncated with `…` if it does not fit on the terminal.
266267
///
267268
/// This may not actually update the display if `tick` is being called too
268269
/// quickly.
@@ -521,20 +522,23 @@ impl Format {
521522
fn render(&self, string: &mut String, msg: &str) {
522523
let mut avail_msg_len = self.max_width - string.len() - 15;
523524
let mut ellipsis_pos = 0;
524-
if avail_msg_len <= 3 {
525+
526+
let (ellipsis, ellipsis_width) = if self.unicode { ("…", 1) } else { ("...", 3) };
527+
528+
if avail_msg_len <= ellipsis_width {
525529
return;
526530
}
527531
for c in msg.chars() {
528532
let display_width = c.width().unwrap_or(0);
529533
if avail_msg_len >= display_width {
530534
avail_msg_len -= display_width;
531535
string.push(c);
532-
if avail_msg_len >= 3 {
536+
if avail_msg_len >= ellipsis_width {
533537
ellipsis_pos = string.len();
534538
}
535539
} else {
536540
string.truncate(ellipsis_pos);
537-
string.push_str("...");
541+
string.push_str(ellipsis);
538542
break;
539543
}
540544
}
@@ -569,6 +573,7 @@ fn test_progress_status() {
569573
max_print: 40,
570574
max_width: 60,
571575
term_integration: TerminalIntegration::new(false),
576+
unicode: true,
572577
};
573578
assert_eq!(
574579
format.progress_status(0, 4, ""),
@@ -610,7 +615,7 @@ fn test_progress_status() {
610615
);
611616
assert_eq!(
612617
format.progress_status(3, 4, ": msg that's just fit"),
613-
Some("[=============> ] 3/4: msg that's just...".to_string())
618+
Some("[=============> ] 3/4: msg that's just f…".to_string())
614619
);
615620

616621
// combining diacritics have width zero and thus can fit max_width.
@@ -623,16 +628,16 @@ fn test_progress_status() {
623628
// some non-ASCII ellipsize test
624629
assert_eq!(
625630
format.progress_status(3, 4, "_123456789123456e\u{301}\u{301}8\u{301}90a"),
626-
Some("[=============> ] 3/4_123456789123456e\u{301}\u{301}...".to_string())
631+
Some("[=============> ] 3/4_123456789123456e\u{301}\u{301}8\u{301}9…".to_string())
627632
);
628633
assert_eq!(
629634
format.progress_status(3, 4, ":每個漢字佔據了兩個字元"),
630-
Some("[=============> ] 3/4:每個漢字佔據了...".to_string())
635+
Some("[=============> ] 3/4:每個漢字佔據了兩…".to_string())
631636
);
632637
assert_eq!(
633638
// handle breaking at middle of character
634639
format.progress_status(3, 4, ":-每個漢字佔據了兩個字元"),
635-
Some("[=============> ] 3/4:-每個漢字佔據了...".to_string())
640+
Some("[=============> ] 3/4:-每個漢字佔據了兩…".to_string())
636641
);
637642
}
638643

@@ -643,6 +648,7 @@ fn test_progress_status_percentage() {
643648
max_print: 40,
644649
max_width: 60,
645650
term_integration: TerminalIntegration::new(false),
651+
unicode: true,
646652
};
647653
assert_eq!(
648654
format.progress_status(0, 77, ""),
@@ -669,6 +675,7 @@ fn test_progress_status_too_short() {
669675
max_print: 25,
670676
max_width: 25,
671677
term_integration: TerminalIntegration::new(false),
678+
unicode: true,
672679
};
673680
assert_eq!(
674681
format.progress_status(1, 1, ""),
@@ -680,6 +687,7 @@ fn test_progress_status_too_short() {
680687
max_print: 24,
681688
max_width: 24,
682689
term_integration: TerminalIntegration::new(false),
690+
unicode: true,
683691
};
684692
assert_eq!(format.progress_status(1, 1, ""), None);
685693
}

0 commit comments

Comments
 (0)