-
Notifications
You must be signed in to change notification settings - Fork 0
/
textwrap.py
40 lines (32 loc) · 1023 Bytes
/
textwrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import re
import textwrap
import typing as t
def dedent(text: str, lstrip: bool = True, join_sep: str = None) -> str:
"""
params:
join_sep: suggest: '-', '|' or '\\'.
"""
out = textwrap.dedent(text).rstrip()
if join_sep:
if '\\' in join_sep:
# escape for regular expression
join_sep = join_sep.replace('\\', '\\\\')
out = re.sub(rf' {join_sep} *\n *', ' ', out)
return out.lstrip() if lstrip else out
def indent(text: str, spaces: int = 4, rstrip: bool = True) -> str:
out = textwrap.indent(text, ' ' * spaces)
return out.rstrip() if rstrip else out
def reindent(text: str, spaces: int = 4, **kwargs) -> str:
return indent(dedent(text, **kwargs), spaces)
def join(
parts: t.Iterable[str],
indent_: int = 0,
sep: str = '\n',
lstrip: bool = True
) -> str:
if indent_:
out = indent(sep.join(parts), indent_)
if lstrip:
return out.lstrip()
return out
return sep.join(parts)