-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_printer.py
87 lines (73 loc) · 2.37 KB
/
table_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
from utils import stylize, max_col_length, format_cells
from arg_parser import IS_BOLD, COLOR
from table_styles import SELECTED_TABLE_STYLE, ANSI_CODES
def table_header(header_row):
"""Returns a string containing the header of the table
Args:
header_row (dict): dictionary containing header row and weights (cell lengths)
"""
row = header_row['row']
weights = header_row['weights']
str = f"{SELECTED_TABLE_STYLE['top_left']}"
# First line
for i in range(len(row)):
str += f"{SELECTED_TABLE_STYLE['horizontal'] * (weights[i] + 2)}"
if i < len(row) - 1:
str += f"{SELECTED_TABLE_STYLE['cross_top']}"
else:
str += f"{SELECTED_TABLE_STYLE['top_right']}\n"
# Second line
str += f"{SELECTED_TABLE_STYLE['vertical']}"
for i in range(len(row)):
str += f" {stylize(row[i], 'bold' if IS_BOLD else 'reset')} "
if i < len(row):
str += f"{SELECTED_TABLE_STYLE['vertical']}"
# Third line
str += f"\n{SELECTED_TABLE_STYLE['cross_left']}"
for i in range(len(row)):
str += f"{SELECTED_TABLE_STYLE['horizontal'] * (weights[i] + 2)}"
if i < len(row) - 1:
str += f"{SELECTED_TABLE_STYLE['cross']}"
else:
str += f"{SELECTED_TABLE_STYLE['cross_right']}"
return str
def table_row(row):
"""Returns a string containing a single row of the table
Args:
row (list): row to print
"""
str = f"{SELECTED_TABLE_STYLE['vertical']}"
for cell in row:
str += f" {stylize(cell, COLOR)} {SELECTED_TABLE_STYLE['vertical']}"
return str
def last_row(header_weights):
"""Returns a string containing the last line of the table (bottom border)
Args:
header_weights (list): list of header cell lengths
"""
str = f"{SELECTED_TABLE_STYLE['bottom_left']}"
for i in range(len(header_weights)):
str += f"{SELECTED_TABLE_STYLE['horizontal'] * (header_weights[i] + 2)}"
if i < len(header_weights) - 1:
str += f"{SELECTED_TABLE_STYLE['cross_bottom']}"
else:
str += f"{SELECTED_TABLE_STYLE['bottom_right']}"
return str
def print_table(table):
"""Prints a csv with table format to console
Args:
table (list): table containing csv contents to print
"""
# Initialize cells
format_cells(table)
# Print table
for i in range(len(table)):
if i == 0:
header_row = {
"row": table[i],
"weights": [max_col_length(table, j) for j in range(len(table[i]))]
}
print(table_header(header_row))
else:
print(table_row(table[i]))
print(last_row(header_row['weights']))