-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.py
230 lines (187 loc) · 6.65 KB
/
printer.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
219
220
221
222
223
224
225
226
227
228
229
hor_line = u"\u2500"
left_corner_top = u"\u250C"
right_corner_top = u"\u2510"
left_side = u"\u251C"
right_side = u"\u2524"
top_connector = u"\u252C"
bottom_connector = u"\u2534"
cross = u"\u253C"
left_corner_bottom = u"\u2514"
right_corner_bottom = u"\u2518"
vertical_line = u"\u2502"
def get_headers_and_sizes_from_data(data, headers, console_width):
headers_and_sizes = []
print(headers)
for header in headers:
size = max(map(lambda x: len(x[header]), data))
if len(header) > size:
size = len(header)
headers_and_sizes.append({"size": size, "name": header})
headers_and_sizes_adjusted = adjust_column_width_to_console(headers_and_sizes, console_width)
return headers_and_sizes_adjusted
def adjust_column_width_to_console(headers, console_width):
sum_of_sizes = 0
num_of_items = 0
for item in headers:
sum_of_sizes += int(item["size"])
num_of_items += 1
console_width -= num_of_items + 1
for item in headers:
item["size"] = int(round(console_width * (float(item["size"]) / sum_of_sizes)))
total_width = sum([item["size"] for item in headers])
while total_width > console_width:
headers[-1]["size"] -= 1
total_width -= 1
return headers
def format_line(headers, line_position):
line = []
for header in headers:
column_width = header["size"]
line.append(hor_line * int(column_width))
if line_position == "top":
line = left_corner_top + top_connector.join(line) + right_corner_top
if line_position == "separator":
line = left_side + cross.join(line) + right_side
if line_position == "bottom":
line = left_corner_bottom + bottom_connector.join(line) + right_corner_bottom
return line
def split_strings(data_row):
substrings = []
for string in data_row:
substrings.append(string.split("\n"))
return substrings
def create_line_content(substrings):
lines = []
line = []
max_length = max([len(item) for item in substrings])
i = 0
while max_length > 0:
for item in substrings:
try:
line.append(item[i])
except IndexError:
line.append(" ")
continue
lines.append(line)
line = []
max_length -= 1
i += 1
return lines
def format_data_line(row, headers):
data_row = []
for header in headers:
column_width = header["size"]
data_row.append(adjust_text(row[header["name"]], column_width))
substrings = split_strings(data_row)
data_line = format_text(headers, substrings)
return data_line
def format_headers_line(headers):
headers_row = []
for header in headers:
column_width = header["size"]
headers_row.append(adjust_text(header["name"], column_width))
substrings = split_strings(headers_row)
headers_line = format_text(headers, substrings)
return headers_line
def format_text(headers, substrings):
headers_sizes = [header["size"] for header in headers]
row_to_print = []
final_data = []
line = create_line_content(substrings)
for item in line:
i = 0
for string in item:
column_width = headers_sizes[i]
row_to_print.append(string.ljust(column_width - 1).rjust(column_width))
i += 1
final_data.append(
vertical_line + vertical_line.join(row_to_print) + vertical_line
)
row_to_print = []
return final_data
def _data_to_string(data):
new_data = []
for item in data:
new_data.append({pair: str(item[pair]) for pair in item})
return new_data
def adjust_text(string, column_width):
string_width = column_width - 2
char_num_so_far = 0
line = []
line_to_print = []
for word in string.split():
if ((char_num_so_far + len(word) + 1) <= string_width):
line.append(word)
char_num_so_far += len(word) + 1
else:
char_left = string_width - char_num_so_far
if char_left == 1:
line.append(word[char_left - 1])
if char_left > 1:
line.append(word[:char_left])
line_to_print.append(" ".join(line))
char_num_so_far = 0
line = []
new_word = word[int(char_left):]
length = len(new_word)
start = 0
end = string_width
while length > 0:
if length < string_width:
line.append(new_word[int(start):int(start + length)])
char_num_so_far = length + 1
break
line_to_print.append(new_word[int(start):int(end)])
length -= string_width
start += string_width
end = start + string_width
if line:
line_to_print.append(" ".join(line))
return "\n".join(line_to_print)
def get_default_console_width():
try:
from shutil import get_terminal_size
console_width, rows = get_terminal_size()
return console_width
except ImportError:
import termios, fcntl, struct, sys
s = struct.pack('hh', 0, 0)
try:
x = fcntl.ioctl(sys.__stdout__.fileno(), termios.TIOCGWINSZ, s)
print(x)
rows, console_width = struct.unpack("hh", x)
return console_width
except IOError:
return 80
def format_lines_adjusted_to_console(data, headers, options={}):
if not data:
return []
console_width = (
options["console_width"]
if "console_width" in options
else get_default_console_width()
)
print(console_width)
headers = options["headers"] if "headers" in options else headers
print(headers)
data = _data_to_string(data)
headers_and_sizes = get_headers_and_sizes_from_data(data, headers, console_width)
lines_to_print = []
lines_to_print.append(format_line(headers_and_sizes, "top"))
headers_line = format_headers_line(headers_and_sizes)
for string in headers_line:
lines_to_print.append(string)
lines_to_print.append(format_line(headers_and_sizes, "separator"))
for row in data[:-1]:
data_line = format_data_line(row, headers_and_sizes)
for string in data_line:
lines_to_print.append(string)
lines_to_print.append(format_line(headers_and_sizes, "separator"))
data_last_row = format_data_line(data[-1], headers_and_sizes)
for string in data_last_row:
lines_to_print.append(string)
lines_to_print.append(format_line(headers_and_sizes, "bottom"))
return lines_to_print
def printer(lines):
for line in lines:
print(line)