Wrap a long error message into few lines. Breaks will only happen between words, and never inside a quoted group (to avoid breaking types such as "Union[int, str]"). The 'first_offset' is the width before the start of first line. Pad every next line with 'num_indent' spaces. Every
(msg: str, max_len: int, first_offset: int, num_indent: int = 0)
| 534 | |
| 535 | |
| 536 | def soft_wrap(msg: str, max_len: int, first_offset: int, num_indent: int = 0) -> str: |
| 537 | """Wrap a long error message into few lines. |
| 538 | |
| 539 | Breaks will only happen between words, and never inside a quoted group |
| 540 | (to avoid breaking types such as "Union[int, str]"). The 'first_offset' is |
| 541 | the width before the start of first line. |
| 542 | |
| 543 | Pad every next line with 'num_indent' spaces. Every line will be at most 'max_len' |
| 544 | characters, except if it is a single word or quoted group. |
| 545 | |
| 546 | For example: |
| 547 | first_offset |
| 548 | ------------------------ |
| 549 | path/to/file: error: 58: Some very long error message |
| 550 | that needs to be split in separate lines. |
| 551 | "Long[Type, Names]" are never split. |
| 552 | ^^^^-------------------------------------------------- |
| 553 | num_indent max_len |
| 554 | """ |
| 555 | words = split_words(msg) |
| 556 | next_line = words.pop(0) |
| 557 | lines: list[str] = [] |
| 558 | while words: |
| 559 | next_word = words.pop(0) |
| 560 | max_line_len = max_len - num_indent if lines else max_len - first_offset |
| 561 | # Add 1 to account for space between words. |
| 562 | if len(next_line) + len(next_word) + 1 <= max_line_len: |
| 563 | next_line += " " + next_word |
| 564 | else: |
| 565 | lines.append(next_line) |
| 566 | next_line = next_word |
| 567 | lines.append(next_line) |
| 568 | padding = "\n" + " " * num_indent |
| 569 | return padding.join(lines) |
| 570 | |
| 571 | |
| 572 | def hash_digest(data: bytes) -> str: |
no test coverage detected
searching dependent graphs…