@@ -80,15 +80,15 @@ def __init__(
80
80
:param header_horiz_align: horizontal alignment of header cells (defaults to left)
81
81
:param header_vert_align: vertical alignment of header cells (defaults to bottom)
82
82
:param override_header_style: if True, then the table is allowed to apply text styles to the header, which may
83
- interfere with any styles the header already has. If False, the header is printed as is.
84
- Table classes which apply style to headers must respect this flag. See the BorderedTable
85
- class for an example of this. (defaults to True)
83
+ conflict with any styles the header already has. If False, the header is printed as is.
84
+ Table classes which apply style to headers must account for the value of this flag.
85
+ (defaults to True)
86
86
:param data_horiz_align: horizontal alignment of data cells (defaults to left)
87
87
:param data_vert_align: vertical alignment of data cells (defaults to top)
88
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 BorderedTable
91
- class for an example of this. (defaults to True)
89
+ conflict with any styles the data already has. If False, the data is printed as is.
90
+ Table classes which apply style to data must account for the value of this flag.
91
+ (defaults to True)
92
92
:param max_data_lines: maximum lines allowed in a data cell. If line count exceeds this, then the final
93
93
line displayed will be truncated with an ellipsis. (defaults to INFINITY)
94
94
:raises: ValueError if width is less than 1
@@ -549,7 +549,14 @@ class SimpleTable(TableCreator):
549
549
"""
550
550
551
551
def __init__ (
552
- self , cols : Sequence [Column ], * , column_spacing : int = 2 , tab_width : int = 4 , divider_char : Optional [str ] = '-'
552
+ self ,
553
+ cols : Sequence [Column ],
554
+ * ,
555
+ column_spacing : int = 2 ,
556
+ tab_width : int = 4 ,
557
+ divider_char : Optional [str ] = '-' ,
558
+ header_bg : Optional [ansi .BgColor ] = None ,
559
+ data_bg : Optional [ansi .BgColor ] = None ,
553
560
) -> None :
554
561
"""
555
562
SimpleTable initializer
@@ -560,14 +567,19 @@ def __init__(
560
567
then it will be converted to one space.
561
568
:param divider_char: optional character used to build the header divider row. Set this to blank or None if you don't
562
569
want a divider row. Defaults to dash. (Cannot be a line breaking character)
563
- :raises: ValueError if column_spacing is less than 0
570
+ :param header_bg: optional background color for header cells (defaults to None)
571
+ :param data_bg: optional background color for data cells (defaults to None)
564
572
:raises: ValueError if tab_width is less than 1
573
+ :raises: ValueError if column_spacing is less than 0
565
574
:raises: TypeError if divider_char is longer than one character
566
575
:raises: ValueError if divider_char is an unprintable character
567
576
"""
577
+ super ().__init__ (cols , tab_width = tab_width )
578
+
568
579
if column_spacing < 0 :
569
580
raise ValueError ("Column spacing cannot be less than 0" )
570
- self .inter_cell = column_spacing * SPACE
581
+
582
+ self .column_spacing = column_spacing
571
583
572
584
if divider_char == '' :
573
585
divider_char = None
@@ -580,8 +592,29 @@ def __init__(
580
592
if divider_char_width == - 1 :
581
593
raise ValueError ("Divider character is an unprintable character" )
582
594
583
- super ().__init__ (cols , tab_width = tab_width )
584
595
self .divider_char = divider_char
596
+ self .header_bg = header_bg
597
+ self .data_bg = data_bg
598
+
599
+ def apply_header_bg (self , value : Any ) -> str :
600
+ """
601
+ If defined, apply the header background color to header text
602
+ :param value: object whose text is to be colored
603
+ :return: formatted text
604
+ """
605
+ if self .header_bg is None :
606
+ return str (value )
607
+ return ansi .style (value , bg = self .header_bg )
608
+
609
+ def apply_data_bg (self , value : Any ) -> str :
610
+ """
611
+ If defined, apply the data background color to data text
612
+ :param value: object whose text is to be colored
613
+ :return: formatted data string
614
+ """
615
+ if self .data_bg is None :
616
+ return str (value )
617
+ return ansi .style (value , bg = self .data_bg )
585
618
586
619
@classmethod
587
620
def base_width (cls , num_cols : int , * , column_spacing : int = 2 ) -> int :
@@ -608,17 +641,28 @@ def base_width(cls, num_cols: int, *, column_spacing: int = 2) -> int:
608
641
609
642
def total_width (self ) -> int :
610
643
"""Calculate the total display width of this table"""
611
- base_width = self .base_width (len (self .cols ), column_spacing = ansi . style_aware_wcswidth ( self .inter_cell ) )
644
+ base_width = self .base_width (len (self .cols ), column_spacing = self .column_spacing )
612
645
data_width = sum (col .width for col in self .cols )
613
646
return base_width + data_width
614
647
615
648
def generate_header (self ) -> str :
616
649
"""Generate table header with an optional divider row"""
617
650
header_buf = io .StringIO ()
618
651
652
+ fill_char = self .apply_header_bg (SPACE )
653
+ inter_cell = self .apply_header_bg (self .column_spacing * SPACE )
654
+
655
+ # Apply background color to header text in Columns which allow it
656
+ to_display : List [Any ] = []
657
+ for index , col in enumerate (self .cols ):
658
+ if col .override_header_style :
659
+ to_display .append (self .apply_header_bg (col .header ))
660
+ else :
661
+ to_display .append (col .header )
662
+
619
663
# Create the header labels
620
- header = self .generate_row (inter_cell = self . inter_cell )
621
- header_buf .write (header )
664
+ header_labels = self .generate_row (row_data = to_display , fill_char = fill_char , inter_cell = inter_cell )
665
+ header_buf .write (header_labels )
622
666
623
667
# Add the divider if necessary
624
668
divider = self .generate_divider ()
@@ -641,7 +685,18 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
641
685
:param row_data: data with an entry for each column in the row
642
686
:return: data row string
643
687
"""
644
- return self .generate_row (row_data = row_data , inter_cell = self .inter_cell )
688
+ fill_char = self .apply_data_bg (SPACE )
689
+ inter_cell = self .apply_data_bg (self .column_spacing * SPACE )
690
+
691
+ # Apply background color to data text in Columns which allow it
692
+ to_display : List [Any ] = []
693
+ for index , col in enumerate (self .cols ):
694
+ if col .override_data_style :
695
+ to_display .append (self .apply_data_bg (row_data [index ]))
696
+ else :
697
+ to_display .append (row_data [index ])
698
+
699
+ return self .generate_row (row_data = to_display , fill_char = fill_char , inter_cell = inter_cell )
645
700
646
701
def generate_table (self , table_data : Sequence [Sequence [Any ]], * , include_header : bool = True , row_spacing : int = 1 ) -> str :
647
702
"""
@@ -665,9 +720,11 @@ def generate_table(self, table_data: Sequence[Sequence[Any]], *, include_header:
665
720
if len (table_data ) > 0 :
666
721
table_buf .write ('\n ' )
667
722
723
+ row_divider = utils .align_left ('' , fill_char = self .apply_data_bg (SPACE ), width = self .total_width ()) + '\n '
724
+
668
725
for index , row_data in enumerate (table_data ):
669
726
if index > 0 and row_spacing > 0 :
670
- table_buf .write (row_spacing * ' \n ' )
727
+ table_buf .write (row_spacing * row_divider )
671
728
672
729
row = self .generate_data_row (row_data )
673
730
table_buf .write (row )
@@ -707,6 +764,7 @@ def __init__(
707
764
:param border_fg: optional foreground color for borders (defaults to None)
708
765
:param header_bg: optional background color for header cells (defaults to None)
709
766
:param data_bg: optional background color for data cells (defaults to None)
767
+ :raises: ValueError if tab_width is less than 1
710
768
:raises: ValueError if padding is less than 0
711
769
"""
712
770
super ().__init__ (cols , tab_width = tab_width )
@@ -1003,6 +1061,7 @@ def __init__(
1003
1061
:param header_bg: optional background color for header cells (defaults to None)
1004
1062
:param odd_bg: optional background color for odd numbered data rows (defaults to None)
1005
1063
:param even_bg: optional background color for even numbered data rows (defaults to StdBg.DARK_GRAY)
1064
+ :raises: ValueError if tab_width is less than 1
1006
1065
:raises: ValueError if padding is less than 0
1007
1066
"""
1008
1067
super ().__init__ (
0 commit comments