-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathwrap.py
54 lines (42 loc) · 1.27 KB
/
wrap.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# SPDX-License-Identifier: GPL-2.0-only
from __future__ import annotations
from typing import List
def _width(s: str) -> int:
return len(s) + s.count('\t') * 7
# Wrap to new line if long (without alignment)
def simple(items: List[str], wrap: int = 80) -> str:
s = ''
for i in items:
if _width(s) + len(i) + 1 > wrap:
s += '\n'
elif s:
s += ' '
s += i
return s
# Wrap long argument lists if necessary and align them properly.
# e.g. join('static int panel_get_modes(', ',', ')', ['struct drm_panel *panel', 'struct drm_connector *connector'])
# results in static int panel_get_modes(struct drm_panel *panel,
# struct drm_connector *connector)
def join(prefix: str, sep: str, end: str, items: List[str], force: int = -1, wrap: int = 80) -> str:
s = prefix + (sep + ' ').join(items) + end
if _width(s) <= wrap:
return s
align = _width(prefix)
wrap -= align
indent = '\t' * (align // 8) + ' ' * (align % 8)
s = ''
line = ''
last = len(items) - 1
for i, item in enumerate(items):
if i == last:
sep = end
if line:
if force == i or _width(line) + len(item) + len(sep) > wrap:
s += prefix + line + '\n'
prefix = indent
line = ''
else:
line += ' '
line += item + sep
s += prefix + line
return s