fix(clap): Early line wrap ascii control chars

counting ascii control sequences lead to unpredictable and early
line breaks on colorized inputs (e.g. syntax highlighted strings)
This commit is contained in:
Harald Gutmann 2022-10-08 18:55:46 +02:00
parent 93648df153
commit 95c638842a

View file

@ -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
}