summaryrefslogtreecommitdiff
path: root/cmd2/table_creator.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2021-10-13 15:44:40 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2021-10-13 15:44:40 -0400
commit29bf2a991ef63fc49c576d77c9c1ee6386a51c9c (patch)
tree513c362c9ae5e218cb7fc1fb1dc9967d7246eda7 /cmd2/table_creator.py
parentc5aaee59dbd2a749b99a33437f9628bc7a9d55fc (diff)
downloadcmd2-git-nested_table.tar.gz
Added settings to Column class which prevent a table from overriding existing styles in headernested_table
and/or data text. These were added to support nesting an AlternatingTable within an AlternatingTable, but other custom table classes can also use these settings.
Diffstat (limited to 'cmd2/table_creator.py')
-rw-r--r--cmd2/table_creator.py41
1 files changed, 32 insertions, 9 deletions
diff --git a/cmd2/table_creator.py b/cmd2/table_creator.py
index 5420ebec..29f7ad62 100644
--- a/cmd2/table_creator.py
+++ b/cmd2/table_creator.py
@@ -17,6 +17,7 @@ from enum import (
from typing import (
Any,
Deque,
+ List,
Optional,
Sequence,
Tuple,
@@ -64,8 +65,10 @@ class Column:
width: Optional[int] = None,
header_horiz_align: HorizontalAlignment = HorizontalAlignment.LEFT,
header_vert_align: VerticalAlignment = VerticalAlignment.BOTTOM,
+ override_header_style: bool = True,
data_horiz_align: HorizontalAlignment = HorizontalAlignment.LEFT,
data_vert_align: VerticalAlignment = VerticalAlignment.TOP,
+ override_data_style: bool = True,
max_data_lines: Union[int, float] = constants.INFINITY,
) -> None:
"""
@@ -77,8 +80,15 @@ class Column:
this width using word-based wrapping (defaults to actual width of header or 1 if header is blank)
:param header_horiz_align: horizontal alignment of header cells (defaults to left)
:param header_vert_align: vertical alignment of header cells (defaults to bottom)
+ :param override_header_style: if True, then the table is allowed to apply text styles to the header, which may
+ interfere with any styles the header already has. If False, the header is printed as is.
+ Table classes which apply style to headers must respect this flag. (defaults to True)
:param data_horiz_align: horizontal alignment of data cells (defaults to left)
:param data_vert_align: vertical alignment of data cells (defaults to top)
+ :param override_data_style: if True, then the table is allowed to apply text styles to the data, which may
+ interfere with any styles the data already has. If False, the data is printed as is.
+ Table classes which apply style to data must respect this flag. See the AlternatingTable
+ class for an example of this. (defaults to True)
:param max_data_lines: maximum lines allowed in a data cell. If line count exceeds this, then the final
line displayed will be truncated with an ellipsis. (defaults to INFINITY)
:raises: ValueError if width is less than 1
@@ -93,8 +103,11 @@ class Column:
self.header_horiz_align = header_horiz_align
self.header_vert_align = header_vert_align
+ self.override_header_style = override_header_style
+
self.data_horiz_align = data_horiz_align
self.data_vert_align = data_vert_align
+ self.override_data_style = override_data_style
if max_data_lines < 1:
raise ValueError("Max data lines cannot be less than 1")
@@ -856,6 +869,11 @@ class AlternatingTable(BorderedTable):
"""
Implementation of BorderedTable which uses background colors to distinguish between rows instead of row border
lines. This class can be used to create the whole table at once or one row at a time.
+
+ AlternatingTable will not apply background color to data whose Columns set override_data_style to False.
+ Background color will still be applied to those Columns's padding and fill characters. For example, to nest
+ an AlternatingTable within an AlternatingTable, set override_data_style to False on the Column which contains
+ the nested table.
"""
def __init__(
@@ -908,22 +926,27 @@ class AlternatingTable(BorderedTable):
:param row_data: data with an entry for each column in the row
:return: data row string
"""
- pre_line = '║' + self.padding * SPACE
+ # Just color the padding, not the border characters
+ colored_padding = self._apply_bg_color(self.padding * SPACE)
- inter_cell = self.padding * SPACE
+ pre_line = '║' + colored_padding
+
+ inter_cell = colored_padding
if self.column_borders:
inter_cell += '│'
- inter_cell += self.padding * SPACE
+ inter_cell += colored_padding
- post_line = self.padding * SPACE + '║'
+ post_line = colored_padding + '║'
fill_char = self._apply_bg_color(SPACE)
- pre_line = self._apply_bg_color(pre_line)
- inter_cell = self._apply_bg_color(inter_cell)
- post_line = self._apply_bg_color(post_line)
- # Apply appropriate background color to data, but don't change the original
- to_display = [self._apply_bg_color(col) for col in row_data]
+ # Apply background colors to data whose Columns allow it
+ to_display: List[Any] = []
+ for index, col in enumerate(self.cols):
+ if col.override_data_style:
+ to_display.append(self._apply_bg_color(row_data[index]))
+ else:
+ to_display.append(row_data[index])
row = self.generate_row(
row_data=to_display, fill_char=fill_char, pre_line=pre_line, inter_cell=inter_cell, post_line=post_line