From 95c638842a317043d2d2945650130544c5fac39c Mon Sep 17 00:00:00 2001 From: Harald Gutmann Date: Sat, 8 Oct 2022 18:55:46 +0200 Subject: [PATCH 1/2] 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) --- src/output/textwrap/core.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/output/textwrap/core.rs b/src/output/textwrap/core.rs index 793ae69e12e..f271ca0825a 100644 --- a/src/output/textwrap/core.rs +++ b/src/output/textwrap/core.rs @@ -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 } From 505f760df925ce175f760b595e6ce08ec62368cb Mon Sep 17 00:00:00 2001 From: Harald Gutmann Date: Sat, 8 Oct 2022 19:55:52 +0200 Subject: [PATCH 2/2] 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) --- src/output/textwrap/core.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/output/textwrap/core.rs b/src/output/textwrap/core.rs index f271ca0825a..b890c6c7bca 100644 --- a/src/output/textwrap/core.rs +++ b/src/output/textwrap/core.rs @@ -63,6 +63,7 @@ pub(crate) fn display_width(text: &str) -> usize { control_sequence = true; } else if control_sequence && ch == control_terminate { control_sequence = false; + continue; } if !control_sequence {