@@ -416,8 +416,9 @@ def _generate_cell_lines(self, cell_data: Any, is_header: bool, col: Column, fil
416
416
417
417
def generate_row (
418
418
self ,
419
+ row_data : Sequence [Any ],
420
+ is_header : bool ,
419
421
* ,
420
- row_data : Optional [Sequence [Any ]] = None ,
421
422
fill_char : str = SPACE ,
422
423
pre_line : str = EMPTY ,
423
424
inter_cell : str = (2 * SPACE ),
@@ -426,8 +427,9 @@ def generate_row(
426
427
"""
427
428
Generate a header or data table row
428
429
429
- :param row_data: If this is None then a header row is generated. Otherwise data should have an entry for each
430
- column in the row. (Defaults to None)
430
+ :param row_data: data with an entry for each column in the row
431
+ :param is_header: True if writing a header cell, otherwise writing a data cell. This determines whether to
432
+ use header or data alignment settings defined in the Columns.
431
433
:param fill_char: character that fills remaining space in a cell. Defaults to space. If this is a tab,
432
434
then it will be converted to one space. (Cannot be a line breaking character)
433
435
:param pre_line: string to print before each line of a row. This can be used for a left row border and
@@ -453,13 +455,8 @@ def __init__(self) -> None:
453
455
# Display width of this cell
454
456
self .width = 0
455
457
456
- if row_data is None :
457
- row_data = [col .header for col in self .cols ]
458
- is_header = True
459
- else :
460
- if len (row_data ) != len (self .cols ):
461
- raise ValueError ("Length of row_data must match length of cols" )
462
- is_header = False
458
+ if len (row_data ) != len (self .cols ):
459
+ raise ValueError ("Length of row_data must match length of cols" )
463
460
464
461
# Replace tabs (tabs in data strings will be handled in _generate_cell_lines())
465
462
fill_char = fill_char .replace ('\t ' , SPACE )
@@ -654,14 +651,14 @@ def generate_header(self) -> str:
654
651
655
652
# Apply background color to header text in Columns which allow it
656
653
to_display : List [Any ] = []
657
- for index , col in enumerate ( self .cols ) :
654
+ for col in self .cols :
658
655
if col .style_header_text :
659
656
to_display .append (self .apply_header_bg (col .header ))
660
657
else :
661
658
to_display .append (col .header )
662
659
663
660
# Create the header labels
664
- header_labels = self .generate_row (row_data = to_display , fill_char = fill_char , inter_cell = inter_cell )
661
+ header_labels = self .generate_row (to_display , is_header = True , fill_char = fill_char , inter_cell = inter_cell )
665
662
header_buf .write (header_labels )
666
663
667
664
# Add the divider if necessary
@@ -696,7 +693,7 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
696
693
else :
697
694
to_display .append (row_data [index ])
698
695
699
- return self .generate_row (row_data = to_display , fill_char = fill_char , inter_cell = inter_cell )
696
+ return self .generate_row (to_display , is_header = False , fill_char = fill_char , inter_cell = inter_cell )
700
697
701
698
def generate_table (self , table_data : Sequence [Sequence [Any ]], * , include_header : bool = True , row_spacing : int = 1 ) -> str :
702
699
"""
@@ -855,7 +852,8 @@ def generate_table_top_border(self) -> str:
855
852
post_line = self .padding * '═' + '╗'
856
853
857
854
return self .generate_row (
858
- row_data = self .empty_data ,
855
+ self .empty_data ,
856
+ is_header = False ,
859
857
fill_char = self .apply_border_color (fill_char ),
860
858
pre_line = self .apply_border_color (pre_line ),
861
859
inter_cell = self .apply_border_color (inter_cell ),
@@ -876,7 +874,8 @@ def generate_header_bottom_border(self) -> str:
876
874
post_line = self .padding * '═' + '╣'
877
875
878
876
return self .generate_row (
879
- row_data = self .empty_data ,
877
+ self .empty_data ,
878
+ is_header = False ,
880
879
fill_char = self .apply_border_color (fill_char ),
881
880
pre_line = self .apply_border_color (pre_line ),
882
881
inter_cell = self .apply_border_color (inter_cell ),
@@ -898,7 +897,8 @@ def generate_row_bottom_border(self) -> str:
898
897
post_line = self .padding * '─' + '╢'
899
898
900
899
return self .generate_row (
901
- row_data = self .empty_data ,
900
+ self .empty_data ,
901
+ is_header = False ,
902
902
fill_char = self .apply_border_color (fill_char ),
903
903
pre_line = self .apply_border_color (pre_line ),
904
904
inter_cell = self .apply_border_color (inter_cell ),
@@ -919,7 +919,8 @@ def generate_table_bottom_border(self) -> str:
919
919
post_line = self .padding * '═' + '╝'
920
920
921
921
return self .generate_row (
922
- row_data = self .empty_data ,
922
+ self .empty_data ,
923
+ is_header = False ,
923
924
fill_char = self .apply_border_color (fill_char ),
924
925
pre_line = self .apply_border_color (pre_line ),
925
926
inter_cell = self .apply_border_color (inter_cell ),
@@ -941,7 +942,7 @@ def generate_header(self) -> str:
941
942
942
943
# Apply background color to header text in Columns which allow it
943
944
to_display : List [Any ] = []
944
- for index , col in enumerate ( self .cols ) :
945
+ for col in self .cols :
945
946
if col .style_header_text :
946
947
to_display .append (self .apply_header_bg (col .header ))
947
948
else :
@@ -953,7 +954,7 @@ def generate_header(self) -> str:
953
954
header_buf .write ('\n ' )
954
955
header_buf .write (
955
956
self .generate_row (
956
- row_data = to_display , fill_char = fill_char , pre_line = pre_line , inter_cell = inter_cell , post_line = post_line
957
+ to_display , is_header = True , fill_char = fill_char , pre_line = pre_line , inter_cell = inter_cell , post_line = post_line
957
958
)
958
959
)
959
960
header_buf .write ('\n ' )
@@ -988,7 +989,7 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
988
989
to_display .append (row_data [index ])
989
990
990
991
return self .generate_row (
991
- row_data = to_display , fill_char = fill_char , pre_line = pre_line , inter_cell = inter_cell , post_line = post_line
992
+ to_display , is_header = False , fill_char = fill_char , pre_line = pre_line , inter_cell = inter_cell , post_line = post_line
992
993
)
993
994
994
995
def generate_table (self , table_data : Sequence [Sequence [Any ]], * , include_header : bool = True ) -> str :
0 commit comments