diff options
Diffstat (limited to 'vendor/clap_builder/src/output/textwrap')
4 files changed, 33 insertions, 13 deletions
diff --git a/vendor/clap_builder/src/output/textwrap/core.rs b/vendor/clap_builder/src/output/textwrap/core.rs index 25c9eb6b0..2f6004c71 100644 --- a/vendor/clap_builder/src/output/textwrap/core.rs +++ b/vendor/clap_builder/src/output/textwrap/core.rs @@ -102,7 +102,7 @@ mod tests { let desc = format!("{:?} U+{:04X}", ch, ch as u32); #[cfg(feature = "unicode")] - assert_eq!(ch.width().unwrap(), 1, "char: {}", desc); + assert_eq!(ch.width().unwrap(), 1, "char: {desc}"); #[cfg(not(feature = "unicode"))] assert_eq!(ch_width(ch), 1, "char: {desc}"); @@ -120,7 +120,7 @@ mod tests { let desc = format!("{:?} U+{:04X}", ch, ch as u32); #[cfg(feature = "unicode")] - assert!(ch.width().unwrap() <= 2, "char: {}", desc); + assert!(ch.width().unwrap() <= 2, "char: {desc}"); #[cfg(not(feature = "unicode"))] assert_eq!(ch_width(ch), 1, "char: {desc}"); diff --git a/vendor/clap_builder/src/output/textwrap/mod.rs b/vendor/clap_builder/src/output/textwrap/mod.rs index d14d3fe7f..fe8139f1a 100644 --- a/vendor/clap_builder/src/output/textwrap/mod.rs +++ b/vendor/clap_builder/src/output/textwrap/mod.rs @@ -83,7 +83,7 @@ mod test { #[test] fn leading_whitespace() { - assert_eq!(wrap(" foo bar", 6), vec![" foo", "bar"]); + assert_eq!(wrap(" foo bar", 6), vec![" foo", " bar"]); } #[test] @@ -92,7 +92,7 @@ mod test { // will be empty. This is because the string is split into // words like [" ", "foobar ", "baz"], which puts "foobar " on // the second line. We never output trailing whitespace - assert_eq!(wrap(" foobar baz", 6), vec!["", "foobar", "baz"]); + assert_eq!(wrap(" foobar baz", 6), vec!["", " foobar", " baz"]); } #[test] diff --git a/vendor/clap_builder/src/output/textwrap/word_separators.rs b/vendor/clap_builder/src/output/textwrap/word_separators.rs index ac09231d5..cb8250b60 100644 --- a/vendor/clap_builder/src/output/textwrap/word_separators.rs +++ b/vendor/clap_builder/src/output/textwrap/word_separators.rs @@ -5,14 +5,15 @@ pub(crate) fn find_words_ascii_space(line: &str) -> impl Iterator<Item = &'_ str std::iter::from_fn(move || { for (idx, ch) in char_indices.by_ref() { - if in_whitespace && ch != ' ' { + let next_whitespace = ch == ' '; + if in_whitespace && !next_whitespace { let word = &line[start..idx]; start = idx; - in_whitespace = ch == ' '; + in_whitespace = next_whitespace; return Some(word); } - in_whitespace = ch == ' '; + in_whitespace = next_whitespace; } if start < line.len() { diff --git a/vendor/clap_builder/src/output/textwrap/wrap_algorithms.rs b/vendor/clap_builder/src/output/textwrap/wrap_algorithms.rs index 019cc04ff..34b4fd2b7 100644 --- a/vendor/clap_builder/src/output/textwrap/wrap_algorithms.rs +++ b/vendor/clap_builder/src/output/textwrap/wrap_algorithms.rs @@ -1,24 +1,37 @@ use super::core::display_width; #[derive(Debug)] -pub(crate) struct LineWrapper { - line_width: usize, +pub(crate) struct LineWrapper<'w> { hard_width: usize, + line_width: usize, + carryover: Option<&'w str>, } -impl LineWrapper { +impl<'w> LineWrapper<'w> { pub(crate) fn new(hard_width: usize) -> Self { Self { - line_width: 0, hard_width, + line_width: 0, + carryover: None, } } pub(crate) fn reset(&mut self) { self.line_width = 0; + self.carryover = None; } - pub(crate) fn wrap<'w>(&mut self, mut words: Vec<&'w str>) -> Vec<&'w str> { + pub(crate) fn wrap(&mut self, mut words: Vec<&'w str>) -> Vec<&'w str> { + if self.carryover.is_none() { + if let Some(word) = words.first() { + if word.trim().is_empty() { + self.carryover = Some(*word); + } else { + self.carryover = Some(""); + } + } + } + let mut i = 0; while i < words.len() { let word = &words[i]; @@ -31,9 +44,15 @@ impl LineWrapper { let trimmed = words[last].trim_end(); words[last] = trimmed; } + + self.line_width = 0; words.insert(i, "\n"); i += 1; - self.reset(); + if let Some(carryover) = self.carryover { + words.insert(i, carryover); + self.line_width += carryover.len(); + i += 1; + } } self.line_width += word_width + trimmed_delta; |