-
Notifications
You must be signed in to change notification settings - Fork 1
/
twocols.py
148 lines (126 loc) · 4.89 KB
/
twocols.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import textwrap
import html
from collections import defaultdict
def array(*args): return defaultdict(array)
def array_keys(a): return range(len(a)) if isinstance(a, list) else a.keys()
def array_shift(a): return a.pop(0)
def foreach(a): return enumerate(a) if isinstance(a, list) else a.items()
def implode(by, a): return by.join(a)
def count(a): return len(a)
def strlen(a): return len(a)
def ahex(item): return hex(item) if isinstance(item, int) and (item > 9 or item < 9) else str(item)
def A(o): return o if isinstance(o, list) else [o]
def shorten(s, width, placeholder='[...]'):
return s[:width] if len(s) <= width else s[:width-len(placeholder)] + placeholder
def indent(n1, n2, s, stripEmpty=True):
if isinstance(s, str):
s = s.replace('\r', '').split('\n')
result = []
count = -1
for line in s:
if isinstance(line, list):
print("[indent] line: {}".format(line))
if not stripEmpty or line.rstrip():
count += 1
if count == 0:
n = n1
else:
n = n2
if isinstance(n, str):
result.append(n + line)
elif isinstance(n, int):
result.append(" " * n + line)
return "\n".join(result)
def array_pad(a, length, value):
len_a = len(a)
if abs(length) <= len_a: return a
if length < 0:
amt = 0 - (len_a + length)
return [value] * amt + a
else:
amt = length - len_a
return a + [value] * amt
class MakeRows(object):
def __init__(self, width=80):
self.clear()
self.width = width
def clear(self):
self.data = dict()
self.numrows = dict()
self.nameWidths = dict()
self.valueWidths = dict()
self.headerWidths = dict()
def add(self, _name, _value):
_value = str(_value)
if _name in self.data:
_name = _name + ' '
self.data[_name] = _value;
self.nameWidths[_name] = strlen(_name)
self.valueWidths[_name] = strlen(_value)
def addHeading(self, _value):
_name = '#{}'.format(len(self.data.keys()))
self.data[_name] = _value;
self.headerWidths[_name] = strlen(_value)
self.valueWidths[_name] = 0
self.nameWidths[_name] = 0
def addBreak(self):
_name = '#{}'.format(len(self.data.keys()))
_value = '-'
self.data[_name] = _value;
self.valueWidths[_name] = 0
self.nameWidths[_name] = 0
def __str__(self):
# import shutil
# size = shutil.get_terminal_size((80, 20)) # pass fallback
# columns = size.columns
# print("size: {}".format(size))
# os.terminal_size(columns=87, lines=23) # returns a named-tuple
columns = self.width
headerWidth = max(self.headerWidths.values())
nameWidth = max(self.nameWidths.values())
valueWidth = max(self.valueWidths.values())
if headerWidth + 4 > columns:
# columns = headerWidth + 4
headerWidth = columns - 4
if nameWidth + valueWidth + 7 > columns:
valueWidth = columns - nameWidth - 7
columns = valueWidth + nameWidth + 7
headerLine = '+-' + '-' * nameWidth + '-+-' + '-' * valueWidth + '-+'
_output = []
_output.append(headerLine)
for k, v in self.data.items():
if k[0] == '#':
if v != '-':
_output.append('| {} |'.format(shorten(v, width=headerWidth, placeholder='...').center(columns - 4)))
_output.append(headerLine)
else:
_output.append(indent(
'| {} | '.format(k.ljust(nameWidth)),
'| {} | '.format(''.ljust(nameWidth)),
[x.ljust(valueWidth) + ' |' for x in textwrap.wrap(v, valueWidth)]
))
_output.append(headerLine)
return implode("\n", _output) + "\n"
def html_escape(self, s):
return html.escape(s, True)
def asDotTable(self):
columns = self.width
headerWidth = max(self.headerWidths.values())
nameWidth = max(self.nameWidths.values())
valueWidth = max(self.valueWidths.values())
_output = ['']
_output.append('<TABLE>')
for k, v in self.data.items():
_output.append(' <TR>')
if k[0] == '#':
if v != '-':
_output.append(' <TD COLSPAN="2">{}</TD>'.format(self.html_escape(v)))
# _output.append(headerLine)
else:
_output.append(' <TD>{}</TD>'.format(self.html_escape(k)))
_output.append(' <TD>{}</TD>'.format(self.html_escape(v)))
_output.append(' </TR>')
# _output.append(headerLine)
_output.append('</TABLE>')
return implode("\n", _output)
# vim: set ts=4 sts=4 sw=4 et: