| 1 | #![allow(clippy::comparison_to_empty)] // no_indent != "", instead of !no_indent.is_empty() |
| 2 | |
| 3 | use crate::ansi::measure_text_width; |
| 4 | |
| 5 | /// Wrap `text` at spaces ('` `') to fit into `width`. If `indent_with` is non-empty, indent |
| 6 | /// each line with this string. If a line from `text` starts with `no_indent`, do not indent. |
| 7 | /// If a line starts with `no_wrap`, do not wrap (empty `no_indent`/`no_wrap` have no effect). |
| 8 | /// If both "magic prefix" markers are used, `no_indent` must be first. |
| 9 | /// Takes unicode and ANSI into account when calculating width, but won't wrap ANSI correctly. |
| 10 | /// Removes trailing spaces. Leading spaces or enumerations with '- ' continue the indentation on |
| 11 | /// the wrapped line. |
| 12 | /// Example: |
| 13 | /// ``` |
| 14 | /// let wrapped = wrap("ab cd ef\n!NI!123\n|AB CD EF GH\n!NI!|123 456 789", 7, "_", "!NI!", "|"); |
| 15 | /// assert_eq!(wrapped, "\ |
| 16 | /// _ab cd\n\ |
| 17 | /// _ef\n\ |
| 18 | /// 123\n\ |
| 19 | /// _AB CD EF GH\n\ |
| 20 | /// 123 456 789\n\ |
| 21 | /// "); |
| 22 | /// ``` |
| 23 | pub fn wrap(text: &str, width: usize, indent_with: &str, no_indent: &str, no_wrap: &str) -> String { |
| 24 | let mut result = String::with_capacity(text.len()); |
| 25 | let indent_len = measure_text_width(indent_with); |
| 26 | |
| 27 | for line in text.lines() { |
| 28 | let line = line.trim_end_matches(' '); |
| 29 | |
| 30 | let (line, indent) = |
| 31 | if let (Some(line), true) = (line.strip_prefix(no_indent), no_indent != "") { |
| 32 | (line, "") |
| 33 | } else { |
| 34 | result.push_str(indent_with); |
| 35 | (line, indent_with) |
| 36 | }; |
| 37 | |
| 38 | if let (Some(line), true) = (line.strip_prefix(no_wrap), no_wrap != "") { |
| 39 | result.push_str(line); |
| 40 | } else { |
| 41 | // `"foo bar end".split_inclusive(' ')` => `["foo ", "bar ", " ", " ", "end"]` |
| 42 | let mut wordit = line.split_inclusive(' '); |
| 43 | let mut curr_len = indent_len; |
| 44 | |
| 45 | if let Some(word) = wordit.next() { |
| 46 | result.push_str(word); |
| 47 | curr_len += measure_text_width(word); |
| 48 | } |
| 49 | |
| 50 | while let Some(mut word) = wordit.next() { |
| 51 | let word_len = measure_text_width(word); |
| 52 | if curr_len + word_len == width + 1 && word.ends_with(' ') { |
| 53 | // If just ' ' is over the limit, let the next word trigger the overflow. |
| 54 | } else if curr_len + word_len > width { |
| 55 | // Remove any trailing whitespace: |
| 56 | let pos = result.trim_end_matches(' ').len(); |
| 57 | result.truncate(pos); |
| 58 | |
| 59 | result.push('\n'); |
| 60 | |
| 61 | // Do not count spaces, skip until next proper word is found. |
| 62 | if word == " " { |
| 63 | for nextword in wordit.by_ref() { |
| 64 | word = nextword; |
| 65 | if word != " " { |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Re-calculates indent for each wrapped line. Could be done only once, maybe |
| 72 | // after an early return which just uses .len() (works for fullwidth chars). |
| 73 | |
| 74 | // If line started with spaces, indent by that much again. |
| 75 | let (indent, space_pos) = |
| 76 | if let Some(space_prefix_len) = line.find(|c: char| c != ' ') { |
| 77 | ( |
| 78 | format!("{}{}", indent, " ".repeat(space_prefix_len)), |
| 79 | space_prefix_len, |
| 80 | ) |
| 81 | } else { |
| 82 | debug_assert!(false, "line.trim_end_matches() missing?"); |
| 83 | (indent.to_string(), 0) |
| 84 | }; |
| 85 | |
| 86 | // If line started with '- ', treat it as a bullet point and increase indentation |
| 87 | let indent = if line[space_pos..].starts_with("- ") { |
| 88 | format!("{}{}", indent, " ") |
| 89 | } else { |
| 90 | indent |
| 91 | }; |
| 92 | |
| 93 | result.push_str(&indent); |
| 94 | curr_len = measure_text_width(&indent); |
| 95 | } |
| 96 | curr_len += word_len; |
| 97 | result.push_str(word); |
| 98 | } |
| 99 | } |
| 100 | let pos = result.trim_end_matches(' ').len(); |
| 101 | result.truncate(pos); |
| 102 | result.push('\n'); |
| 103 | } |
| 104 | |
| 105 | #[cfg(test)] |
| 106 | if !result.contains("no-sanity") { |
| 107 | // sanity check |
| 108 | let stripped_input = text |
| 109 | .replace(" ", "") |
| 110 | .replace("\n", "") |
| 111 | .replace(no_wrap, "") |
| 112 | .replace(no_indent, ""); |
| 113 | let stripped_output = result |
| 114 | .replace(" ", "") |
| 115 | .replace("\n", "") |
| 116 | .replace(indent_with, ""); |
| 117 | assert_eq!(stripped_input, stripped_output); |
| 118 | } |
| 119 | |
| 120 | result |
| 121 | } |
| 122 | |
| 123 | #[cfg(test)] |
| 124 | mod test { |
| 125 | use super::*; |
| 126 | use insta::assert_snapshot; |
| 127 | |
| 128 | #[test] |
| 129 | fn simple_ascii_can_not_split() { |
| 130 | let input = "000 123456789 abcdefghijklmnopqrstuvwxyz ok"; |
| 131 | let result = wrap(input, 5, "", "", ""); |
| 132 | assert_snapshot!(result, @r###" |
| 133 | 000 |
| 134 | 123456789 |
| 135 | abcdefghijklmnopqrstuvwxyz |
| 136 | ok |
| 137 | "###); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn simple_ascii_just_whitespace() { |
| 142 | let input = " \n \n \n \n \n \n"; |
| 143 | let result = wrap(input, 3, "__", "", ""); |
| 144 | assert_snapshot!(result, @r###" |
| 145 | __ |
| 146 | __ |
| 147 | __ |
| 148 | __ |
| 149 | __ |
| 150 | __ |
| 151 | "###); |
| 152 | let result = wrap(input, 3, "", "", ""); |
| 153 | assert_eq!(result, "\n\n\n\n\n\n"); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn simple_ascii_can_not_split_plus_whitespace() { |
| 158 | let input = "000 123456789 abcdefghijklmnopqrstuvwxyz ok"; |
| 159 | let result = wrap(input, 5, "", "", ""); |
| 160 | assert_snapshot!(result, @r###" |
| 161 | 000 |
| 162 | 123456789 |
| 163 | abcdefghijklmnopqrstuvwxyz |
| 164 | ok |
| 165 | "###); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn simple_ascii_keep_leading_input_indent() { |
| 170 | let input = "abc\n Def ghi jkl mno pqr stuv xyz\n Abc def ghijklm\nok"; |
| 171 | let result = wrap(input, 10, "_", "", ""); |
| 172 | assert_snapshot!(result, @r###" |
| 173 | _abc |
| 174 | _ Def ghi |
| 175 | _ jkl mno |
| 176 | _ pqr |
| 177 | _ stuv |
| 178 | _ xyz |
| 179 | _ Abc |
| 180 | _ def |
| 181 | _ ghijklm |
| 182 | _ok |
| 183 | "###); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn simple_ascii_indent_and_bullet_points() { |
| 188 | let input = "- ABC ABC abc\n def ghi - jkl\n - 1 22 3 4 55 6 7 8 9\n - 1 22 3 4 55 6 7 8 9\n!- 0 0 0 0 0 0 0 \n"; |
| 189 | let result = wrap(input, 10, "", "!", ""); |
| 190 | assert_snapshot!(result, @r###" |
| 191 | - ABC ABC |
| 192 | abc |
| 193 | def ghi |
| 194 | - jkl |
| 195 | - 1 22 3 |
| 196 | 4 55 6 |
| 197 | 7 8 9 |
| 198 | - 1 22 |
| 199 | 3 4 |
| 200 | 55 6 |
| 201 | 7 8 |
| 202 | 9 |
| 203 | - 0 0 0 0 |
| 204 | 0 0 0 |
| 205 | "###); |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn simple_ascii_all_overlong_after_indent() { |
| 210 | let input = "0000 1111 2222"; |
| 211 | let result = wrap(input, 5, "__", "", ""); |
| 212 | assert_snapshot!(result, @r###" |
| 213 | __0000 |
| 214 | __1111 |
| 215 | __2222 |
| 216 | "###); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn simple_ascii_one_line() { |
| 221 | let input = "123 456 789 abc def ghi jkl mno pqr stu vwx yz"; |
| 222 | let result = wrap(input, 10, "__", "", ""); |
| 223 | assert_snapshot!(result, @r###" |
| 224 | __123 456 |
| 225 | __789 abc |
| 226 | __def ghi |
| 227 | __jkl mno |
| 228 | __pqr stu |
| 229 | __vwx yz |
| 230 | "###); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn simple_ascii_trailing_space() { |
| 235 | let input = "123 \n\n \n 456 \n a b \n\n"; |
| 236 | let result = wrap(input, 10, " ", "", ""); |
| 237 | assert_eq!(result, " 123\n\n\n 456\n a\n b\n\n"); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn simple_ascii_two_lines() { |
| 242 | let input = "123 456 789 abc def\nghi jkl mno pqr stu vwx yz\n1234 567 89 876 54321\n"; |
| 243 | let result = wrap(input, 10, "__", "", ""); |
| 244 | assert_snapshot!(result, @r###" |
| 245 | __123 456 |
| 246 | __789 abc |
| 247 | __def |
| 248 | __ghi jkl |
| 249 | __mno pqr |
| 250 | __stu vwx |
| 251 | __yz |
| 252 | __1234 567 |
| 253 | __89 876 |
| 254 | __54321 |
| 255 | "###); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn simple_ascii_no_indent() { |
| 260 | let input = "123 456 789\n!!abc def ghi jkl mno pqr\nstu vwx yz\n\n"; |
| 261 | let result = wrap(input, 10, "__", "!!", ""); |
| 262 | assert_snapshot!(result, @r###" |
| 263 | __123 456 |
| 264 | __789 |
| 265 | abc def |
| 266 | ghi jkl |
| 267 | mno pqr |
| 268 | __stu vwx |
| 269 | __yz |
| 270 | __ |
| 271 | "###); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn simple_ascii_no_wrap() { |
| 276 | let input = "123 456 789\n|abc def ghi jkl mno pqr\nstu vwx yz\n|W\nA B C D E F G H I\n"; |
| 277 | let result = wrap(input, 10, "__", "!!", "|"); |
| 278 | assert_snapshot!(result, @r###" |
| 279 | __123 456 |
| 280 | __789 |
| 281 | __abc def ghi jkl mno pqr |
| 282 | __stu vwx |
| 283 | __yz |
| 284 | __W |
| 285 | __A B C D |
| 286 | __E F G H |
| 287 | __I |
| 288 | "###); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn simple_ascii_no_both() { |
| 293 | let input = "123 456 789\n!!|abc def ghi jkl mno pqr\nstu vwx yz\n|W\nA B C D E F G H I\n"; |
| 294 | let result = wrap(input, 10, "__", "!!", "|"); |
| 295 | assert_snapshot!(result, @r###" |
| 296 | __123 456 |
| 297 | __789 |
| 298 | abc def ghi jkl mno pqr |
| 299 | __stu vwx |
| 300 | __yz |
| 301 | __W |
| 302 | __A B C D |
| 303 | __E F G H |
| 304 | __I |
| 305 | "###); |
| 306 | } |
| 307 | |
| 308 | #[test] |
| 309 | fn simple_ascii_no_both_wrong_order() { |
| 310 | let input = "!!|abc def ghi jkl\n|!!ABC DEF GHI JKL + no-sanity\n"; |
| 311 | let result = wrap(input, 7, "__", "!!", "|"); |
| 312 | assert_snapshot!(result, @r###" |
| 313 | abc def ghi jkl |
| 314 | __!!ABC DEF GHI JKL + no-sanity |
| 315 | "###); |
| 316 | let wrapped = wrap( |
| 317 | "ab cd ef\n!NI!123\n|AB CD EF GH\n!NI!|123 456 789", |
| 318 | 6, |
| 319 | "_", |
| 320 | "!NI!", |
| 321 | "|", |
| 322 | ); |
| 323 | assert_snapshot!(wrapped, @r###" |
| 324 | _ab cd |
| 325 | _ef |
| 326 | 123 |
| 327 | _AB CD EF GH |
| 328 | 123 456 789 |
| 329 | "###); |
| 330 | } |
| 331 | |
| 332 | #[test] |
| 333 | fn simple_ascii_much_whitespace() { |
| 334 | let input = "123 456 789\nabc def ghi jkl mno pqr \nstu vwx yz"; |
| 335 | let result = wrap(input, 10, "__", "!!", "|"); |
| 336 | assert_snapshot!(result, @r###" |
| 337 | __123 |
| 338 | __456 |
| 339 | __789 |
| 340 | __abc |
| 341 | __def ghi |
| 342 | __jkl mno |
| 343 | __pqr |
| 344 | __stu |
| 345 | __vwx yz |
| 346 | "###); |
| 347 | } |
| 348 | } |