schema: make wrap_text less awkward

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2024-06-27 11:44:55 +02:00
parent dc7273e888
commit f02ce77ad6

View File

@ -37,6 +37,23 @@ pub fn wrap_text(
text: &str, text: &str,
columns: usize, columns: usize,
) -> String { ) -> String {
// first we condense paragraphs by normalizing whitespace:
let paragraphs = text
.split("\n\n")
.map(|paragraph| {
paragraph
.split_ascii_whitespace()
.fold(String::new(), |mut acc, word| {
if !acc.is_empty() {
acc.push(' ');
}
acc.push_str(word);
acc
})
})
.collect::<Vec<_>>();
// Then we wrap each paragraph with textwrap.
let wrap_options1 = textwrap::Options::new(columns) let wrap_options1 = textwrap::Options::new(columns)
.initial_indent(initial_indent) .initial_indent(initial_indent)
.subsequent_indent(subsequent_indent); .subsequent_indent(subsequent_indent);
@ -45,15 +62,15 @@ pub fn wrap_text(
.initial_indent(subsequent_indent) .initial_indent(subsequent_indent)
.subsequent_indent(subsequent_indent); .subsequent_indent(subsequent_indent);
text.split("\n\n") paragraphs
.map(|p| p.trim()) .into_iter()
.filter(|p| !p.is_empty()) .filter(|p| !p.is_empty())
.fold(String::new(), |mut acc, p| { .fold(String::new(), |mut acc, p| {
if acc.is_empty() { if acc.is_empty() {
acc.push_str(&textwrap::wrap(p, &wrap_options1).join("\n")); acc.push_str(&textwrap::wrap(&p, &wrap_options1).join("\n"));
} else { } else {
acc.push_str("\n\n"); acc.push_str("\n\n");
acc.push_str(&textwrap::wrap(p, &wrap_options2).join("\n")); acc.push_str(&textwrap::wrap(&p, &wrap_options2).join("\n"));
} }
acc acc
}) })
@ -61,11 +78,29 @@ pub fn wrap_text(
#[test] #[test]
fn test_wrap_text() { fn test_wrap_text() {
let text = "Command. This may be a list in order to spefify nested sub-commands."; let text = "\
let expect = " Command. This may be a list in order to spefify nested sub-\n commands."; Command. This may be a list in order to specify nested subcommands.
let indent = " "; A
let wrapped = wrap_text(indent, indent, text, 80); second
paragraph which will be formatted differently, consisting of both lines
which are too long and ones which are
too
short.";
let expect = " \
Command. This may be a list in order to specify nested
subcommands.
A second paragraph which will be formatted
differently, consisting of both lines which are too
long and ones which are too short.\
";
let wrapped = wrap_text(" ", " ", text, 60);
eprintln!("[[[[\n{expect}]]]]");
eprintln!("[[[[\n{wrapped}]]]]");
assert_eq!(wrapped, expect); assert_eq!(wrapped, expect);
} }