-
Notifications
You must be signed in to change notification settings - Fork 23
/
table.py
218 lines (187 loc) · 6.98 KB
/
table.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import csv
from io import StringIO
from os import environ
from .text import ansi_clean
ROW_SEPARATOR = 1
if environ.get("BW_TABLE_STYLE") == 'ascii':
FRAME_TOP_LEFT = "+-"
FRAME_TOP_COLUMN_SEPARATOR = "-+-"
FRAME_TOP_RIGHT = "-+"
FRAME_BOTTOM_LEFT = "+-"
FRAME_BOTTOM_COLUMN_SEPARATOR = "-+-"
FRAME_BOTTOM_RIGHT = "-+"
FRAME_CENTER_LEFT = "+-"
FRAME_CENTER_COLUMN_SEPARATOR = "-+-"
FRAME_CENTER_RIGHT = "-+"
FRAME_COLUMN_FILLER = "-"
FRAME_COLUMN_WHITESPACE = " "
FRAME_ROW_COLUMN_SEPARATOR_LEFT = "-| "
FRAME_ROW_COLUMN_SEPARATOR_NONE = " | "
FRAME_ROW_COLUMN_SEPARATOR_BOTH = "-+-"
FRAME_ROW_COLUMN_SEPARATOR_RIGHT = " |-"
elif environ.get("BW_TABLE_STYLE") == 'grep':
FRAME_TOP_LEFT = ""
FRAME_TOP_COLUMN_SEPARATOR = ""
FRAME_TOP_RIGHT = ""
FRAME_BOTTOM_LEFT = ""
FRAME_BOTTOM_COLUMN_SEPARATOR = ""
FRAME_BOTTOM_RIGHT = ""
FRAME_CENTER_LEFT = ""
FRAME_CENTER_COLUMN_SEPARATOR = ""
FRAME_CENTER_RIGHT = ""
FRAME_COLUMN_FILLER = ""
FRAME_COLUMN_WHITESPACE = ""
FRAME_ROW_COLUMN_SEPARATOR_LEFT = "\t"
FRAME_ROW_COLUMN_SEPARATOR_NONE = "\t"
FRAME_ROW_COLUMN_SEPARATOR_BOTH = "\t"
FRAME_ROW_COLUMN_SEPARATOR_RIGHT = "\t"
else:
FRAME_TOP_LEFT = "╭─"
FRAME_TOP_COLUMN_SEPARATOR = "─┬─"
FRAME_TOP_RIGHT = "─╮"
FRAME_BOTTOM_LEFT = "╰─"
FRAME_BOTTOM_COLUMN_SEPARATOR = "─┴─"
FRAME_BOTTOM_RIGHT = "─╯"
FRAME_CENTER_LEFT = "├─"
FRAME_CENTER_COLUMN_SEPARATOR = "─┼─"
FRAME_CENTER_RIGHT = "─┤"
FRAME_COLUMN_FILLER = "─"
FRAME_COLUMN_WHITESPACE = " "
FRAME_ROW_COLUMN_SEPARATOR_LEFT = "─┤ "
FRAME_ROW_COLUMN_SEPARATOR_NONE = " │ "
FRAME_ROW_COLUMN_SEPARATOR_BOTH = "─┼─"
FRAME_ROW_COLUMN_SEPARATOR_RIGHT = " ├─"
def _column_widths_for_rows(rows):
column_widths = [0 for column in rows[0]]
for row in rows:
if not isinstance(row, list) and not isinstance(row, tuple):
continue
for i, column in enumerate(row):
if column == ROW_SEPARATOR:
continue
column_widths[i] = max(column_widths[i], len(ansi_clean(column)))
return column_widths
def _border_top(column_widths):
result = FRAME_TOP_LEFT
result += FRAME_TOP_COLUMN_SEPARATOR.join(
[FRAME_COLUMN_FILLER * width for width in column_widths]
)
result += FRAME_TOP_RIGHT
return result
def _border_center(column_widths): # FIXME unused?
result = FRAME_CENTER_LEFT
result += FRAME_CENTER_COLUMN_SEPARATOR.join(
[FRAME_COLUMN_FILLER * width for width in column_widths]
)
result += FRAME_CENTER_RIGHT
return result
def _border_bottom(column_widths):
result = FRAME_BOTTOM_LEFT
result += FRAME_BOTTOM_COLUMN_SEPARATOR.join(
[FRAME_COLUMN_FILLER * width for width in column_widths]
)
result += FRAME_BOTTOM_RIGHT
return result
def _empty_row(row):
for column_value in row:
if column_value != ROW_SEPARATOR and column_value.strip():
return False
return True
def _row(row, column_widths, alignments):
result = ""
columns = []
for i, column_value in enumerate(row):
alignment = alignments.get(i, 'left')
if column_value == ROW_SEPARATOR:
columns.append(ROW_SEPARATOR)
elif alignment == 'right':
columns.append(
FRAME_COLUMN_WHITESPACE * (column_widths[i] - len(ansi_clean(column_value))) +
column_value
)
elif alignment == 'left':
columns.append(
column_value +
FRAME_COLUMN_WHITESPACE * (column_widths[i] - len(ansi_clean(column_value)))
)
elif alignment == 'center':
prefix = int((column_widths[i] - len(ansi_clean(column_value))) / 2)
suffix = (column_widths[i] - len(ansi_clean(column_value)) - prefix)
columns.append(
FRAME_COLUMN_WHITESPACE * prefix +
column_value +
FRAME_COLUMN_WHITESPACE * suffix
)
else:
raise NotImplementedError("no such alignment: {}".format(alignment))
for i, column_value in enumerate(columns):
if i == 0:
fill_previous_column = False
else:
fill_previous_column = columns[i - 1] == ROW_SEPARATOR
fill_this_column = column_value == ROW_SEPARATOR
if fill_previous_column and fill_this_column:
result += FRAME_ROW_COLUMN_SEPARATOR_BOTH
elif fill_previous_column and not fill_this_column:
result += FRAME_ROW_COLUMN_SEPARATOR_LEFT
elif not fill_previous_column and fill_this_column:
result += FRAME_ROW_COLUMN_SEPARATOR_RIGHT
else:
result += FRAME_ROW_COLUMN_SEPARATOR_NONE
if fill_this_column:
result += FRAME_COLUMN_FILLER * column_widths[i]
else:
result += column_value
if fill_this_column:
result += FRAME_ROW_COLUMN_SEPARATOR_LEFT
else:
result += FRAME_ROW_COLUMN_SEPARATOR_NONE
return result[1:-1] # strip exactly one whitespace character at each end
def inline_list(cell):
if isinstance(cell, set):
return ",".join(sorted(set))
elif isinstance(cell, (list, tuple)):
return ",".join(cell)
else:
return cell
def render_table(rows, alignments=None):
"""
Yields lines for a table.
rows must be a list of lists of values, with the first row being
considered the heading row. Alternatively, an entire row or
individual cells can be set to ROW_SEPARATOR to turn it into a
separator:
rows = [
["heading1", "heading2"],
ROW_SEPARATOR,
["value1", "value2"],
["value3", ROW_SEPARATOR],
]
alignments is a dict mapping column indexes to 'left' or 'right'.
"""
if environ.get("BW_TABLE_STYLE") == 'csv':
output = StringIO()
writer = csv.writer(output)
for row in rows:
if row == ROW_SEPARATOR:
continue
writer.writerow([ansi_clean(inline_list(cell)) for cell in row])
for line in output.getvalue().splitlines():
yield line
else:
if alignments is None:
alignments = {}
column_widths = _column_widths_for_rows(rows)
if environ.get("BW_TABLE_STYLE") != 'grep':
yield _border_top(column_widths)
for row_index, row in enumerate(rows):
if row == ROW_SEPARATOR:
if environ.get("BW_TABLE_STYLE") != 'grep':
yield _row([ROW_SEPARATOR] * len(column_widths), column_widths, {})
elif row_index == 0:
# heading row ignores alignments
yield _row(row, column_widths, {})
elif environ.get("BW_TABLE_STYLE") != 'grep' or not _empty_row(row):
yield _row(row, column_widths, alignments)
if environ.get("BW_TABLE_STYLE") != 'grep':
yield _border_bottom(column_widths)