Skip to content

Commit

Permalink
fix(clap): Early line wrap ascii control chars
Browse files Browse the repository at this point in the history
counting ascii control sequences lead to unpredictable and early
line breaks on colorized inputs (e.g. syntax highlighted strings)
  • Loading branch information
hargut committed Oct 8, 2022
1 parent 93648df commit 95c6388
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/output/textwrap/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,20 @@
#[inline(never)]
pub(crate) fn display_width(text: &str) -> usize {
let mut width = 0;

let mut control_sequence = false;
let control_terminate: char = 'm';

for ch in text.chars() {
width += ch_width(ch);
if ch.is_ascii_control() {
control_sequence = true;
} else if control_sequence && ch == control_terminate {
control_sequence = false;
}

if !control_sequence {
width += ch_width(ch);
}
}
width
}
Expand Down

0 comments on commit 95c6388

Please sign in to comment.