]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | import textwrap |
2 | import typing as t | |
3 | from contextlib import contextmanager | |
4 | ||
5 | ||
6 | class TextWrapper(textwrap.TextWrapper): | |
7 | def _handle_long_word( | |
8 | self, | |
9 | reversed_chunks: t.List[str], | |
10 | cur_line: t.List[str], | |
11 | cur_len: int, | |
12 | width: int, | |
13 | ) -> None: | |
14 | space_left = max(width - cur_len, 1) | |
15 | ||
16 | if self.break_long_words: | |
17 | last = reversed_chunks[-1] | |
18 | cut = last[:space_left] | |
19 | res = last[space_left:] | |
20 | cur_line.append(cut) | |
21 | reversed_chunks[-1] = res | |
22 | elif not cur_line: | |
23 | cur_line.append(reversed_chunks.pop()) | |
24 | ||
25 | @contextmanager | |
26 | def extra_indent(self, indent: str) -> t.Iterator[None]: | |
27 | old_initial_indent = self.initial_indent | |
28 | old_subsequent_indent = self.subsequent_indent | |
29 | self.initial_indent += indent | |
30 | self.subsequent_indent += indent | |
31 | ||
32 | try: | |
33 | yield | |
34 | finally: | |
35 | self.initial_indent = old_initial_indent | |
36 | self.subsequent_indent = old_subsequent_indent | |
37 | ||
38 | def indent_only(self, text: str) -> str: | |
39 | rv = [] | |
40 | ||
41 | for idx, line in enumerate(text.splitlines()): | |
42 | indent = self.initial_indent | |
43 | ||
44 | if idx > 0: | |
45 | indent = self.subsequent_indent | |
46 | ||
47 | rv.append(f"{indent}{line}") | |
48 | ||
49 | return "\n".join(rv) |