40ROW_TYPES: list[int] = [HEADER, SEPARATOR, DATA]
51 ansi_escape = re.compile(
r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
52 result = ansi_escape.sub(
'', cell_content)
73 headline_color: str=
'\x1b[37m',
74 header_color: str=
'\x1b[104m'
82 if len(self.
data) < 3:
84 log.info(
'To fulfill the table design, it must have header row, separator row and a final separator row.')
86 if not isinstance(self.
data[0][0], int):
87 log.warning(
'The first column of first row is not as "int".')
88 log.info(
'To fulfill the table design, each row must start with a row type integer constant.')
89 if self.
data[0][0] != HEADER:
90 log.warning(
'First data row is not the table header.')
91 log.info(
'To fulfil the table design, the table header must be sorted to the first index position.')
93 row_lengths: list[int] = []
94 for i
in range(len(self.
data)):
96 if self.
data[i][0]
not in ROW_TYPES:
97 log.warning(
'Found not implemented value "{}" in row type column.'.format(self.
data[i][0]))
98 log.info(
'The first value in each row must be in "{}" = "[tbl.HEADER, tbl.SEPARATOR, tbl.DATA]".'.format(ROW_TYPES))
99 raise NotImplementedError()
100 if self.
data[i][0]
is SEPARATOR:
104 [
None for _
in range(len(self.
data[0]) - 1)]
106 row_lengths.append(len(self.
data[i]))
107 if (row[0] != SEPARATOR)
and (row_lengths[-1] < 2):
109 log.info(
'Each data and header row must have a row type column and min. 1 data cell column.')
111 for j
in range(len(row)):
113 cell = str(data[i][j])
117 if not cell.startswith(
' '):
118 cell =
' ' + str(cell)
119 if not cell.endswith(
' '):
125 if not all(item == row_lengths[0]
for item
in row_lengths):
127 'Length of each row is not identical (see {}).'.format(row_lengths))
128 log.info(
'Check/repair the deviating rows.')
133 data: (list[list],
None) =
None
137 cw: (list[int],
None) =
None
140 headline: (str,
None) =
None
144 headline_color: str =
'\x1b[37m'
148 header_color: str =
'\x1b[104m'
153 self.
cw = [int(0)
for _
in range(len(self.
data[0]))]
155 for item
in self.
data:
156 if not isinstance(item[0], int):
157 log.warning(
'First item in this row "{}" is not as "int".'.format(item))
158 log.info(
'The first column in each row defines the row type as "int".')
160 if item[0]
in [1, 3]:
161 for i
in range(len(self.
cw)):
171 print(
'\n{}{}\x1b[0m\n'.format(
176 for row
in self.
data:
179 for i
in range(len(row)):
183 row_str +=
'{}{}{}\x1b[0m'.format(
186 ' ' * (self.
cw[i] - len(row[i])),
192 elif row[0] == SEPARATOR:
193 row_width = sum(self.
cw[1:]) + len(self.
cw) - 2
194 row_str =
'-' * row_width
198 for i
in range(len(row)):
202 row_str +=
'{}{}'.format(
run(self)
Display the data table.
str header_color
ANSI ESC-sequence for the header color.
list cw
Column width list.
__init__(self, list[list] data, str headline='', str headline_color='\x1b[37m', str header_color='\x1b[104m')
CONSTRUCTOR.
data
Two-dimensional data table.
headline
Headline, which is printed before the table.
str headline_color
ANSI ESC-sequence for the header color.
calculateColumnWidth(self)
Calculate width of each column except the row type column.
warning((str, tuple) msg)
Log warning message colored to console only.
info((str, tuple) msg)
Log info message colored to console only.
int readableLen(str cell_content)
Calculate the length of cell content skipping ANSI ESC sequences (e.g.