* Wrap the text, if necessary. The variable indent is the indent for the * first line, indent2 is the indent for all other lines. * If indent is negative, assume that already -indent columns have been * consumed (and no extra indent is necessary for the first line). */
| 275 | * consumed (and no extra indent is necessary for the first line). |
| 276 | */ |
| 277 | void strbuf_add_wrapped_text(struct strbuf *buf, |
| 278 | const char *text, int indent1, int indent2, int width) |
| 279 | { |
| 280 | int indent, w, assume_utf8 = 1; |
| 281 | const char *bol, *space, *start = text; |
| 282 | size_t orig_len = buf->len; |
| 283 | |
| 284 | if (width <= 0) { |
| 285 | strbuf_add_indented_text(buf, text, indent1, indent2); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | retry: |
| 290 | bol = text; |
| 291 | w = indent = indent1; |
| 292 | space = NULL; |
| 293 | if (indent < 0) { |
| 294 | w = -indent; |
| 295 | space = text; |
| 296 | } |
| 297 | |
| 298 | for (;;) { |
| 299 | char c; |
| 300 | size_t skip; |
| 301 | |
| 302 | while ((skip = display_mode_esc_sequence_len(text))) |
| 303 | text += skip; |
| 304 | |
| 305 | c = *text; |
| 306 | if (!c || isspace(c)) { |
| 307 | if (w <= width || !space) { |
| 308 | const char *start = bol; |
| 309 | if (!c && text == start) |
| 310 | return; |
| 311 | if (space) |
| 312 | start = space; |
| 313 | else |
| 314 | strbuf_addchars(buf, ' ', indent); |
| 315 | strbuf_add(buf, start, text - start); |
| 316 | if (!c) |
| 317 | return; |
| 318 | space = text; |
| 319 | if (c == '\t') |
| 320 | w |= 0x07; |
| 321 | else if (c == '\n') { |
| 322 | space++; |
| 323 | if (*space == '\n') { |
| 324 | strbuf_addch(buf, '\n'); |
| 325 | goto new_line; |
| 326 | } |
| 327 | else if (!isalnum(*space)) |
| 328 | goto new_line; |
| 329 | else |
| 330 | strbuf_addch(buf, ' '); |
| 331 | } |
| 332 | w++; |
| 333 | text++; |
| 334 | } |
no test coverage detected