1717from typing import (
1818 Any ,
1919 Deque ,
20+ List ,
2021 Optional ,
2122 Sequence ,
2223 Tuple ,
@@ -64,8 +65,10 @@ def __init__(
6465 width : Optional [int ] = None ,
6566 header_horiz_align : HorizontalAlignment = HorizontalAlignment .LEFT ,
6667 header_vert_align : VerticalAlignment = VerticalAlignment .BOTTOM ,
68+ override_header_style : bool = True ,
6769 data_horiz_align : HorizontalAlignment = HorizontalAlignment .LEFT ,
6870 data_vert_align : VerticalAlignment = VerticalAlignment .TOP ,
71+ override_data_style : bool = True ,
6972 max_data_lines : Union [int , float ] = constants .INFINITY ,
7073 ) -> None :
7174 """
@@ -77,8 +80,15 @@ def __init__(
7780 this width using word-based wrapping (defaults to actual width of header or 1 if header is blank)
7881 :param header_horiz_align: horizontal alignment of header cells (defaults to left)
7982 :param header_vert_align: vertical alignment of header cells (defaults to bottom)
83+ :param override_header_style: if True, then the table is allowed to apply text styles to the header, which may
84+ interfere with any styles the header already has. If False, the header is printed as is.
85+ Table classes which apply style to headers must respect this flag. (defaults to True)
8086 :param data_horiz_align: horizontal alignment of data cells (defaults to left)
8187 :param data_vert_align: vertical alignment of data cells (defaults to top)
88+ :param override_data_style: if True, then the table is allowed to apply text styles to the data, which may
89+ interfere with any styles the data already has. If False, the data is printed as is.
90+ Table classes which apply style to data must respect this flag. See the AlternatingTable
91+ class for an example of this. (defaults to True)
8292 :param max_data_lines: maximum lines allowed in a data cell. If line count exceeds this, then the final
8393 line displayed will be truncated with an ellipsis. (defaults to INFINITY)
8494 :raises: ValueError if width is less than 1
@@ -93,8 +103,11 @@ def __init__(
93103
94104 self .header_horiz_align = header_horiz_align
95105 self .header_vert_align = header_vert_align
106+ self .override_header_style = override_header_style
107+
96108 self .data_horiz_align = data_horiz_align
97109 self .data_vert_align = data_vert_align
110+ self .override_data_style = override_data_style
98111
99112 if max_data_lines < 1 :
100113 raise ValueError ("Max data lines cannot be less than 1" )
@@ -856,6 +869,13 @@ class AlternatingTable(BorderedTable):
856869 """
857870 Implementation of BorderedTable which uses background colors to distinguish between rows instead of row border
858871 lines. This class can be used to create the whole table at once or one row at a time.
872+
873+ AlternatingTable will not apply background color to data whose Columns set override_data_style to False.
874+ Background color will still be applied to those Columns's padding and fill characters.
875+
876+ To nest an AlternatingTable within another AlternatingTable, set override_data_style to False on the Column
877+ which contains the nested table. That will prevent the current row's background color from affecting the colors
878+ of the nested table.
859879 """
860880
861881 def __init__ (
@@ -908,22 +928,27 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
908928 :param row_data: data with an entry for each column in the row
909929 :return: data row string
910930 """
911- pre_line = '║' + self .padding * SPACE
931+ # Only color the padding and not the outer border characters
932+ pre_line = '║' + self ._apply_bg_color (self .padding * SPACE )
912933
913934 inter_cell = self .padding * SPACE
914935 if self .column_borders :
915936 inter_cell += '│'
916937 inter_cell += self .padding * SPACE
938+ inter_cell = self ._apply_bg_color (inter_cell )
917939
918- post_line = self .padding * SPACE + '║'
940+ # Only color the padding and not the outer border characters
941+ post_line = self ._apply_bg_color (self .padding * SPACE ) + '║'
919942
920943 fill_char = self ._apply_bg_color (SPACE )
921- pre_line = self ._apply_bg_color (pre_line )
922- inter_cell = self ._apply_bg_color (inter_cell )
923- post_line = self ._apply_bg_color (post_line )
924944
925- # Apply appropriate background color to data, but don't change the original
926- to_display = [self ._apply_bg_color (col ) for col in row_data ]
945+ # Apply background colors to data whose Columns allow it
946+ to_display : List [Any ] = []
947+ for index , col in enumerate (self .cols ):
948+ if col .override_data_style :
949+ to_display .append (self ._apply_bg_color (row_data [index ]))
950+ else :
951+ to_display .append (row_data [index ])
927952
928953 row = self .generate_row (
929954 row_data = to_display , fill_char = fill_char , pre_line = pre_line , inter_cell = inter_cell , post_line = post_line
0 commit comments