Skip to content

Commit acf37cf

Browse files
tleonhardtanselor
authored andcommitted
Improved AlternatingRowGrid
AlternatingRowGrid now accepts the primary and secondary (alternate) colors instead of the reset and alternating. This allows for more colorful tables.
1 parent 2612898 commit acf37cf

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

examples/color.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
try:
1010
from colorama import Back
1111
BACK_RESET = Back.RESET
12+
BACK_GREEN = Back.LIGHTGREEN_EX
1213
BACK_BLUE = Back.LIGHTBLUE_EX
1314
except ImportError:
1415
try:
1516
from colored import bg
1617
BACK_RESET = bg(0)
1718
BACK_BLUE = bg(27)
19+
BACK_GREEN = bg(119)
1820
except ImportError:
1921
BACK_RESET = ''
2022
BACK_BLUE = ''
23+
BACK_GREEN = ''
2124

2225
rows = [('A1', 'A2', 'A3', 'A4'),
2326
('B1', 'B2\nB2\nB2', 'B3', 'B4'),
@@ -27,5 +30,4 @@
2730
columns = ('Col1', 'Col2', 'Col3', 'Col4')
2831

2932
print("Table with colorful alternting rows")
30-
print(generate_table(rows, columns, grid_style=tf.AlternatingRowGrid(BACK_RESET, BACK_BLUE)))
31-
33+
print(generate_table(rows, columns, grid_style=tf.AlternatingRowGrid(BACK_GREEN, BACK_BLUE)))

tableformatter.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -523,52 +523,52 @@ class AlternatingRowGrid(FancyGrid):
523523
524524
This typically looks quite good, but also does a good job of conserving vertical space.
525525
"""
526-
def __init__(self, bg_reset: str=TableColors.BG_RESET, bg_color: str=TableColors.BG_COLOR_ROW) -> None:
526+
def __init__(self, bg_primary: str=TableColors.BG_RESET, bg_alternate: str=TableColors.BG_COLOR_ROW) -> None:
527527
"""Initialize the AlternatingRowGrid with the two alternating colors.
528528
529-
:param bg_reset: string reprsenting the default background color
530-
:param bg_color: string representing the alternate background color
529+
:param bg_primary: string reprsenting the primary background color starting with the 1st row
530+
:param bg_alternate: string representing the alternate background color starting with the 2nd row
531531
"""
532532
super().__init__()
533533
# Disable row dividers present in FancyGrid in order to save vertical space
534534
self.row_divider = False
535535
self.row_divider_span = ''
536536
self.row_divider_col_divider = ''
537537
self.row_divider_header_col_divider = ''
538-
self.bg_reset = bg_reset
539-
self.bg_color = bg_color
538+
self.bg_reset = TableColors.BG_RESET
539+
self.bg_primary = bg_primary
540+
self.bg_alt = bg_alternate
540541

541542
def border_left_span(self, row_index: Union[int, None]) -> str:
543+
prefix = self.bg_reset + '║'
544+
color = self.bg_reset
542545
if isinstance(row_index, int):
543546
if row_index % 2 == 0:
544-
return '║'
547+
color = self.bg_primary
545548
else:
546-
return self.bg_reset + '║' + self.bg_color
547-
return '║'
549+
color = self.bg_alt
550+
return prefix + color
548551

549552
def border_right_span(self, row_index: Union[int, None]) -> str:
550-
if isinstance(row_index, int):
551-
if row_index % 2 == 0:
552-
return '║'
553-
else:
554-
return self.bg_reset + '║'
555-
return '║'
553+
return self.bg_reset + '║'
556554

557555
def col_divider_span(self, row_index : Union[int, None]) -> str:
556+
color = self.bg_reset
558557
if isinstance(row_index, int):
559558
if row_index % 2 == 0:
560-
return '│'
559+
color = self.bg_primary
561560
else:
562-
return self.bg_reset + self.bg_color + '│'
563-
return '│'
561+
color = self.bg_alt
562+
return color + '│'
564563

565564
def header_col_divider_span(self, row_index: Union[int, None]) -> str:
565+
color = self.bg_reset
566566
if isinstance(row_index, int):
567567
if row_index % 2 == 0:
568-
return '║'
568+
color = self.bg_primary
569569
else:
570-
return self.bg_reset + self.bg_color + '║'
571-
return '║'
570+
color = self.bg_alt
571+
return color + '║'
572572

573573

574574
DEFAULT_GRID = AlternatingRowGrid()

0 commit comments

Comments
 (0)