Skip to content

Commit 0d60017

Browse files
committed
Updated table example.
Renamed Column settings: override_header_style -> style_header_text override_data_style -> style_data_text
1 parent fcf3410 commit 0d60017

File tree

3 files changed

+92
-79
lines changed

3 files changed

+92
-79
lines changed

cmd2/table_creator.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ def __init__(
6464
width: Optional[int] = None,
6565
header_horiz_align: HorizontalAlignment = HorizontalAlignment.LEFT,
6666
header_vert_align: VerticalAlignment = VerticalAlignment.BOTTOM,
67-
override_header_style: bool = True,
67+
style_header_text: bool = True,
6868
data_horiz_align: HorizontalAlignment = HorizontalAlignment.LEFT,
6969
data_vert_align: VerticalAlignment = VerticalAlignment.TOP,
70-
override_data_style: bool = True,
70+
style_data_text: bool = True,
7171
max_data_lines: Union[int, float] = constants.INFINITY,
7272
) -> None:
7373
"""
@@ -79,16 +79,16 @@ def __init__(
7979
this width using word-based wrapping (defaults to actual width of header or 1 if header is blank)
8080
:param header_horiz_align: horizontal alignment of header cells (defaults to left)
8181
:param header_vert_align: vertical alignment of header cells (defaults to bottom)
82-
:param override_header_style: if True, then the table is allowed to apply text styles to the header, which may
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)
82+
:param style_header_text: if True, then the table is allowed to apply styles to the header text, which may
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)
8686
:param data_horiz_align: horizontal alignment of data cells (defaults to left)
8787
: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-
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)
88+
:param style_data_text: if True, then the table is allowed to apply styles to the data text, which may
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)
9292
:param max_data_lines: maximum lines allowed in a data cell. If line count exceeds this, then the final
9393
line displayed will be truncated with an ellipsis. (defaults to INFINITY)
9494
:raises: ValueError if width is less than 1
@@ -103,11 +103,11 @@ def __init__(
103103

104104
self.header_horiz_align = header_horiz_align
105105
self.header_vert_align = header_vert_align
106-
self.override_header_style = override_header_style
106+
self.style_header_text = style_header_text
107107

108108
self.data_horiz_align = data_horiz_align
109109
self.data_vert_align = data_vert_align
110-
self.override_data_style = override_data_style
110+
self.style_data_text = style_data_text
111111

112112
if max_data_lines < 1:
113113
raise ValueError("Max data lines cannot be less than 1")
@@ -655,7 +655,7 @@ def generate_header(self) -> str:
655655
# Apply background color to header text in Columns which allow it
656656
to_display: List[Any] = []
657657
for index, col in enumerate(self.cols):
658-
if col.override_header_style:
658+
if col.style_header_text:
659659
to_display.append(self.apply_header_bg(col.header))
660660
else:
661661
to_display.append(col.header)
@@ -691,7 +691,7 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
691691
# Apply background color to data text in Columns which allow it
692692
to_display: List[Any] = []
693693
for index, col in enumerate(self.cols):
694-
if col.override_data_style:
694+
if col.style_data_text:
695695
to_display.append(self.apply_data_bg(row_data[index]))
696696
else:
697697
to_display.append(row_data[index])
@@ -940,7 +940,7 @@ def generate_header(self) -> str:
940940
# Apply background color to header text in Columns which allow it
941941
to_display: List[Any] = []
942942
for index, col in enumerate(self.cols):
943-
if col.override_header_style:
943+
if col.style_header_text:
944944
to_display.append(self.apply_header_bg(col.header))
945945
else:
946946
to_display.append(col.header)
@@ -981,7 +981,7 @@ def generate_data_row(self, row_data: Sequence[Any]) -> str:
981981
# Apply background color to data text in Columns which allow it
982982
to_display: List[Any] = []
983983
for index, col in enumerate(self.cols):
984-
if col.override_data_style:
984+
if col.style_data_text:
985985
to_display.append(self.apply_data_bg(row_data[index]))
986986
else:
987987
to_display.append(row_data[index])
@@ -1028,7 +1028,7 @@ class AlternatingTable(BorderedTable):
10281028
Implementation of BorderedTable which uses background colors to distinguish between rows instead of row border
10291029
lines. This class can be used to create the whole table at once or one row at a time.
10301030
1031-
To nest an AlternatingTable within another AlternatingTable, set override_data_style to False on the Column
1031+
To nest an AlternatingTable within another AlternatingTable, set style_data_text to False on the Column
10321032
which contains the nested table. That will prevent the current row's background color from affecting the colors
10331033
of the nested table.
10341034
"""

examples/table_creation.py

Lines changed: 60 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ def __str__(self) -> str:
4242
class Book:
4343
"""Class used for example data"""
4444

45-
def __init__(self, title: str, due_date: str) -> None:
45+
def __init__(self, title: str, year_published: str) -> None:
4646
self.title = title
47-
self.due_date = due_date
47+
self.year_published = year_published
4848

4949

50-
class Person:
50+
class Author:
5151
"""Class used for example data"""
5252

53-
def __init__(self, name: str, birthday: str, department: str) -> None:
53+
def __init__(self, name: str, birthday: str, place_of_birth: str) -> None:
5454
self.name = name
5555
self.birthday = birthday
56-
self.department = department
56+
self.place_of_birth = place_of_birth
5757
self.books: List[Book] = []
5858

5959

@@ -108,48 +108,61 @@ def basic_tables():
108108

109109
def nested_tables():
110110
"""
111-
Demonstrates how to nest tables using the override_data_style keyword to handle tables with conflicting styles.
111+
Demonstrates how to nest tables using the style_data_text keyword to handle tables with conflicting styles.
112112
In these cases, the inner tables reset the background color applied by the outer AlternatingTable.
113113
114114
It also demonstrates coloring various aspects of tables.
115115
"""
116116

117117
# Create data for this example
118-
person_data: List[Person] = []
119-
person_1 = Person("Bill Anderson", "01/22/1955", "Accounting")
120-
person_1.books.append(Book("Great Expectations", "11/01/2025"))
121-
person_1.books.append(Book("Strange Case of Dr Jekyll and Mr Hyde", "07/16/2026"))
122-
person_1.books.append(Book("Dune", "01/24/2027"))
123-
124-
person_2 = Person("Arthur Smith", "06/11/1974", "Automotive")
125-
person_2.books.append(Book("Nineteen Eighty-Four", "08/07/2025"))
126-
person_2.books.append(Book("Pride and Prejudice", "04/13/2026"))
127-
person_2.books.append(Book("Fahrenheit 451", "07/29/2026"))
128-
person_2.books.append(Book("The Count of Monte Cristo", "10/15/2027"))
129-
130-
person_data.append(person_1)
131-
person_data.append(person_2)
132-
133-
# Define table which presents Person data fields vertically with no header.
118+
author_data: List[Author] = []
119+
author_1 = Author("Frank Herbert", "10/08/1920", "Tacoma, Washington")
120+
author_1.books.append(Book("Dune", "1965"))
121+
author_1.books.append(Book("Dune Messiah", "1969"))
122+
author_1.books.append(Book("Children of Dune", "1976"))
123+
author_1.books.append(Book("God Emperor of Dune", "1981"))
124+
author_1.books.append(Book("Heretics of Dune", "1984"))
125+
author_1.books.append(Book("Chapterhouse: Dune", "1985"))
126+
127+
author_2 = Author("Jane Austen", "12/16/1775", "Steventon, Hampshire, England")
128+
author_2.books.append(Book("Sense and Sensibility", "1811"))
129+
author_2.books.append(Book("Pride and Prejudice", "1813"))
130+
author_2.books.append(Book("Mansfield Park ", "1814"))
131+
author_2.books.append(Book("Emma", "1815"))
132+
author_2.books.append(Book("Northanger Abbey", "1818"))
133+
author_2.books.append(Book("Persuasion", "1818"))
134+
author_2.books.append(Book("Lady Susan", "1871"))
135+
136+
author_data.append(author_1)
137+
author_data.append(author_2)
138+
139+
# Define table which presents Author data fields vertically with no header.
134140
# This will be nested in the parent table.
135-
person_columns: List[Column] = list()
136-
person_columns.append(Column("", width=10))
137-
person_columns.append(Column("", width=20))
141+
author_columns: List[Column] = list()
142+
author_columns.append(Column("", width=14))
143+
author_columns.append(Column("", width=20))
138144

139145
# The text labels in this table will be bold text. They will also be aligned by the table code.
140146
# When styled text is aligned, a TextStyle.RESET_ALL sequence is inserted between the aligned text
141-
# and the fill characters. Therefore, the Person table will contain TextStyle.RESET_ALL sequences,
147+
# and the fill characters. Therefore, the Author table will contain TextStyle.RESET_ALL sequences,
142148
# which would interfere with the background color applied by the parent table. To account for this,
143-
# we will color the Person tables to match the background colors of the parent AlternatingTable's rows
144-
# and set override_data_style to False in the Person column. See below for that.
145-
odd_person_tbl = SimpleTable(person_columns, data_bg=EightBitBg.GRAY_0)
146-
even_person_tbl = SimpleTable(person_columns, data_bg=EightBitBg.GRAY_15)
149+
# we will color the Author tables to match the background colors of the parent AlternatingTable's rows
150+
# and set style_data_text to False in the Author column. See below for that.
151+
odd_author_tbl = SimpleTable(author_columns, data_bg=EightBitBg.GRAY_0)
152+
even_author_tbl = SimpleTable(author_columns, data_bg=EightBitBg.GRAY_15)
147153

148154
# Define AlternatingTable table for books checked out by people in the first table.
149155
# This will also be nested in the parent table.
150156
books_columns: List[Column] = list()
151-
books_columns.append(Column("Title", width=28))
152-
books_columns.append(Column("Due Date", width=10))
157+
books_columns.append(Column("Title", width=25))
158+
books_columns.append(
159+
Column(
160+
"Published",
161+
width=9,
162+
header_horiz_align=HorizontalAlignment.RIGHT,
163+
data_horiz_align=HorizontalAlignment.RIGHT,
164+
)
165+
)
153166

154167
books_tbl = AlternatingTable(
155168
books_columns,
@@ -160,13 +173,13 @@ def nested_tables():
160173
even_bg=EightBitBg.GRAY_15,
161174
)
162175

163-
# Define parent AlternatingTable which contains Person and Book tables
176+
# Define parent AlternatingTable which contains Author and Book tables
164177
parent_tbl_columns: List[Column] = list()
165178

166-
# Both the Person and Books tables already have background colors. Set override_data_style
179+
# Both the Author and Books tables already have background colors. Set style_data_text
167180
# to False so the parent AlternatingTable does not apply background color to them.
168-
parent_tbl_columns.append(Column("Person", width=odd_person_tbl.total_width(), override_data_style=False))
169-
parent_tbl_columns.append(Column("Books", width=books_tbl.total_width(), override_data_style=False))
181+
parent_tbl_columns.append(Column("Author", width=odd_author_tbl.total_width(), style_data_text=False))
182+
parent_tbl_columns.append(Column("Books", width=books_tbl.total_width(), style_data_text=False))
170183

171184
parent_tbl = AlternatingTable(
172185
parent_tbl_columns,
@@ -178,26 +191,26 @@ def nested_tables():
178191

179192
# Construct the tables
180193
parent_table_data: List[List[Any]] = []
181-
for row, person in enumerate(person_data, start=1):
182-
# First build the person table and color it based on row number
183-
person_tbl = even_person_tbl if row % 2 == 0 else odd_person_tbl
194+
for row, author in enumerate(author_data, start=1):
195+
# First build the author table and color it based on row number
196+
author_tbl = even_author_tbl if row % 2 == 0 else odd_author_tbl
184197

185198
# This table has three rows and two columns
186199
table_data = [
187-
[ansi.style("Name", bold=True), person.name],
188-
[ansi.style("Birthday", bold=True), person.birthday],
189-
[ansi.style("Department", bold=True), person.department],
200+
[ansi.style("Name", bold=True), author.name],
201+
[ansi.style("Birthday", bold=True), author.birthday],
202+
[ansi.style("Place of Birth", bold=True), author.place_of_birth],
190203
]
191204

192-
# Build the person table string
193-
person_tbl_str = person_tbl.generate_table(table_data, include_header=False, row_spacing=0)
205+
# Build the author table string
206+
author_tbl_str = author_tbl.generate_table(table_data, include_header=False, row_spacing=0)
194207

195-
# Now build this person's book table
196-
table_data = [[book.title, book.due_date] for book in person.books]
208+
# Now build this author's book table
209+
table_data = [[book.title, book.year_published] for book in author.books]
197210
book_tbl_str = books_tbl.generate_table(table_data)
198211

199212
# Add these tables to the parent table's data
200-
parent_table_data.append(['\n' + person_tbl_str, '\n' + book_tbl_str + '\n\n'])
213+
parent_table_data.append(['\n' + author_tbl_str, '\n' + book_tbl_str + '\n\n'])
201214

202215
# Build the parent table
203216
top_table_str = parent_tbl.generate_table(parent_table_data)

tests/test_table_creator.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ def test_column_creation():
7171
tc = TableCreator([c])
7272
assert tc.cols[0].width == ansi.style_aware_wcswidth("line with tabs")
7373

74-
# Add basic tests for override_header_style and override_data_style to make sure these members don't get removed.
74+
# Add basic tests for style_header_text and style_data_text to make sure these members don't get removed.
7575
c = Column("Column 1")
76-
assert c.override_header_style is True
77-
assert c.override_data_style is True
76+
assert c.style_header_text is True
77+
assert c.style_data_text is True
7878

79-
c = Column("Column 1", override_header_style=False)
80-
assert c.override_header_style is False
81-
assert c.override_data_style is True
79+
c = Column("Column 1", style_header_text=False)
80+
assert c.style_header_text is False
81+
assert c.style_data_text is True
8282

83-
c = Column("Column 1", override_data_style=False)
84-
assert c.override_header_style is True
85-
assert c.override_data_style is False
83+
c = Column("Column 1", style_data_text=False)
84+
assert c.style_header_text is True
85+
assert c.style_data_text is False
8686

8787

8888
def test_column_alignment():
@@ -465,9 +465,9 @@ def test_simple_table_creation():
465465
'\x1b[0m\x1b[104mCol 1 Row 2\x1b[49m\x1b[0m\x1b[104m \x1b[49m\x1b[0m\x1b[104m \x1b[49m\x1b[0m\x1b[104mCol 2 Row 2\x1b[49m\x1b[0m\x1b[104m \x1b[49m\x1b[0m'
466466
)
467467

468-
# Make sure SimpleTable respects override_header_style override_data_style flags.
468+
# Make sure SimpleTable respects style_header_text and style_data_text flags.
469469
# Don't apply parent table's background colors to header or data text in second column.
470-
st = SimpleTable([column_1, Column("Col 2", width=16, override_header_style=False, override_data_style=False)],
470+
st = SimpleTable([column_1, Column("Col 2", width=16, style_header_text=False, style_data_text=False)],
471471
divider_char=None, header_bg=Bg.GREEN, data_bg=Bg.LIGHT_BLUE)
472472
table = st.generate_table(row_data)
473473
assert table == (
@@ -576,9 +576,9 @@ def test_bordered_table_creation():
576576
'\x1b[93m╚═\x1b[39m\x1b[0m\x1b[0m\x1b[93m═══════════════\x1b[39m\x1b[0m\x1b[93m═╧═\x1b[39m\x1b[0m\x1b[0m\x1b[93m═══════════════\x1b[39m\x1b[0m\x1b[93m═╝\x1b[39m'
577577
)
578578

579-
# Make sure BorderedTable respects override_header_style override_data_style flags.
579+
# Make sure BorderedTable respects style_header_text and style_data_text flags.
580580
# Don't apply parent table's background colors to header or data text in second column.
581-
bt = BorderedTable([column_1, Column("Col 2", width=15, override_header_style=False, override_data_style=False)],
581+
bt = BorderedTable([column_1, Column("Col 2", width=15, style_header_text=False, style_data_text=False)],
582582
header_bg=Bg.GREEN, data_bg=Bg.LIGHT_BLUE)
583583
table = bt.generate_table(row_data)
584584
assert table == (
@@ -702,9 +702,9 @@ def test_alternating_table_creation():
702702
'\x1b[93m╚═\x1b[39m\x1b[0m\x1b[0m\x1b[93m═══════════════\x1b[39m\x1b[0m\x1b[93m═╧═\x1b[39m\x1b[0m\x1b[0m\x1b[93m═══════════════\x1b[39m\x1b[0m\x1b[93m═╝\x1b[39m'
703703
)
704704

705-
# Make sure AlternatingTable respects override_header_style override_data_style flags.
705+
# Make sure AlternatingTable respects style_header_text and style_data_text flags.
706706
# Don't apply parent table's background colors to header or data text in second column.
707-
at = AlternatingTable([column_1, Column("Col 2", width=15, override_header_style=False, override_data_style=False)],
707+
at = AlternatingTable([column_1, Column("Col 2", width=15, style_header_text=False, style_data_text=False)],
708708
header_bg=Bg.GREEN, odd_bg=Bg.LIGHT_BLUE, even_bg=Bg.LIGHT_RED)
709709
table = at.generate_table(row_data)
710710
assert table == (

0 commit comments

Comments
 (0)