Refills a collection of paragraphs, prefixing them with an optional `indent`.
(
paragraphs: P,
indent: &str,
width: usize,
)
| 63 | |
| 64 | /// Refills a collection of paragraphs, prefixing them with an optional `indent`. |
| 65 | fn refill_many<S: AsRef<str>, P: IntoIterator<Item = S>>( |
| 66 | paragraphs: P, |
| 67 | indent: &str, |
| 68 | width: usize, |
| 69 | ) -> Vec<String> { |
| 70 | let mut formatted = vec![]; |
| 71 | |
| 72 | let mut first = true; |
| 73 | for paragraph in paragraphs { |
| 74 | let paragraph = paragraph.as_ref(); |
| 75 | |
| 76 | if !first { |
| 77 | formatted.push(String::new()); |
| 78 | } |
| 79 | first = false; |
| 80 | |
| 81 | let mut extra_indent = String::new(); |
| 82 | for ch in paragraph.chars() { |
| 83 | // TODO(jmmv): It'd be nice to recognize '*' prefixes so that continuation lines in |
| 84 | // lists look nicer. |
| 85 | if ch.is_whitespace() { |
| 86 | extra_indent.push(' '); |
| 87 | } else { |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | let lines = refill(paragraph, width - 4 - indent.len() - extra_indent.len()); |
| 93 | for line in lines { |
| 94 | if line.is_empty() { |
| 95 | formatted.push(String::new()); |
| 96 | } else { |
| 97 | formatted.push(format!("{}{}{}", indent, extra_indent, line)); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | formatted |
| 103 | } |
| 104 | |
| 105 | /// Same as `refill` but prints the lines of each paragraph to the console instead of returning |
| 106 | /// them and prefixes them with an optional `indent`. |