Skip to content

Commit d4035b4

Browse files
authored
Merge pull request #1066 from python-cmd2/table_newline
Fixed issue where TableCreator was tossing blank last lines
2 parents 97f14b2 + ecba81e commit d4035b4

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 2.0.0 (TBD, 2021)
2-
* Breaking changes
2+
* Bug Fixes
3+
* Fixed issue where TableCreator was tossing blank last lines
4+
* Breaking Changes
35
* `cmd2` 2.0 supports Python 3.6+ (removed support for Python 3.5)
46
* Argparse Completion / Settables
57
* Replaced `choices_function` / `choices_method` with `choices_provider`.

cmd2/table_creator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ def add_word(word_to_add: str, is_last_word: bool):
351351
last_word = data_line_index == len(data_str_lines) - 1 and char_index == len(data_line)
352352
add_word(cur_word_buf.getvalue(), last_word)
353353

354+
# If the last line is empty, then add a newline
355+
elif data_line_index == len(data_str_lines) - 1:
356+
wrapped_buf.write('\n')
357+
354358
# Stop line loop if we've written to max_lines
355359
if total_lines == max_lines:
356360
# If this isn't the last data line and there is space

tests/test_table_creator.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
VerticalAlignment,
1919
)
2020

21+
# Turn off black formatting for entire file so multiline strings
22+
# can be visually aligned to match the tables being tested.
23+
# fmt: off
24+
2125

2226
def test_column_creation():
2327
# Width less than 1
@@ -119,19 +123,37 @@ def test_column_alignment():
119123
)
120124

121125

126+
def test_blank_last_line():
127+
"""This tests that an empty line is inserted when the last data line is blank"""
128+
column_1 = Column("Col 1", width=10)
129+
tc = TableCreator([column_1])
130+
131+
row_data = ['my line\n\n']
132+
row = tc.generate_row(row_data=row_data)
133+
assert row == ('my line \n'
134+
' ')
135+
136+
122137
def test_wrap_text():
123138
column_1 = Column("Col 1", width=10)
124139
tc = TableCreator([column_1])
125140

126141
# Test normal wrapping
127142
row_data = ['Some text to wrap\nA new line that will wrap\nNot wrap\n 1 2 3']
128143
row = tc.generate_row(row_data=row_data)
129-
assert row == ('Some text \n' 'to wrap \n' 'A new line\n' 'that will \n' 'wrap \n' 'Not wrap \n' ' 1 2 3 ')
144+
assert row == ('Some text \n'
145+
'to wrap \n'
146+
'A new line\n'
147+
'that will \n'
148+
'wrap \n'
149+
'Not wrap \n'
150+
' 1 2 3 ')
130151

131152
# Test preserving a multiple space sequence across a line break
132153
row_data = ['First last one']
133154
row = tc.generate_row(row_data=row_data)
134-
assert row == ('First \n' ' last one ')
155+
assert row == ('First \n'
156+
' last one ')
135157

136158

137159
def test_wrap_text_max_lines():
@@ -141,27 +163,32 @@ def test_wrap_text_max_lines():
141163
# Test not needing to truncate the final line
142164
row_data = ['First line last line']
143165
row = tc.generate_row(row_data=row_data)
144-
assert row == ('First line\n' 'last line ')
166+
assert row == ('First line\n'
167+
'last line ')
145168

146169
# Test having to truncate the last word because it's too long for the final line
147170
row_data = ['First line last lineextratext']
148171
row = tc.generate_row(row_data=row_data)
149-
assert row == ('First line\n' 'last line…')
172+
assert row == ('First line\n'
173+
'last line…')
150174

151175
# Test having to truncate the last word because it fits the final line but there is more text not being included
152176
row_data = ['First line thistxtfit extra']
153177
row = tc.generate_row(row_data=row_data)
154-
assert row == ('First line\n' 'thistxtfi…')
178+
assert row == ('First line\n'
179+
'thistxtfi…')
155180

156181
# Test having to truncate the last word because it fits the final line but there are more lines not being included
157182
row_data = ['First line thistxtfit\nextra']
158183
row = tc.generate_row(row_data=row_data)
159-
assert row == ('First line\n' 'thistxtfi…')
184+
assert row == ('First line\n'
185+
'thistxtfi…')
160186

161187
# Test having space left on the final line and adding an ellipsis because there are more lines not being included
162188
row_data = ['First line last line\nextra line']
163189
row = tc.generate_row(row_data=row_data)
164-
assert row == ('First line\n' 'last line…')
190+
assert row == ('First line\n'
191+
'last line…')
165192

166193

167194
def test_wrap_long_word():
@@ -174,7 +201,8 @@ def test_wrap_long_word():
174201

175202
# Test header row
176203
header = tc.generate_row()
177-
assert header == ('LongColumn \n' 'Name Col 2 ')
204+
assert header == ('LongColumn \n'
205+
'Name Col 2 ')
178206

179207
# Test data row
180208
row_data = list()
@@ -230,7 +258,8 @@ def test_wrap_long_word_max_data_lines():
230258
row_data.append("A LongerThan10RunsOverLast")
231259

232260
row = tc.generate_row(row_data=row_data)
233-
assert row == ('LongerThan LongerThan LongerThan A LongerT…\n' '10FitsLast 10FitsLas… 10RunsOve… ')
261+
assert row == ('LongerThan LongerThan LongerThan A LongerT…\n'
262+
'10FitsLast 10FitsLas… 10RunsOve… ')
234263

235264

236265
def test_wrap_long_char_wider_than_max_width():
@@ -318,7 +347,10 @@ def test_simple_table_creation():
318347
table = st.generate_table(row_data)
319348

320349
assert table == (
321-
'Col 1 Col 2 \n' 'Col 1 Row 1 Col 2 Row 1 \n' '\n' 'Col 1 Row 2 Col 2 Row 2 '
350+
'Col 1 Col 2 \n'
351+
'Col 1 Row 1 Col 2 Row 1 \n'
352+
'\n'
353+
'Col 1 Row 2 Col 2 Row 2 '
322354
)
323355

324356
# No row spacing
@@ -335,7 +367,9 @@ def test_simple_table_creation():
335367
st = SimpleTable([column_1, column_2])
336368
table = st.generate_table(row_data, include_header=False)
337369

338-
assert table == ('Col 1 Row 1 Col 2 Row 1 \n' '\n' 'Col 1 Row 2 Col 2 Row 2 ')
370+
assert table == ('Col 1 Row 1 Col 2 Row 1 \n'
371+
'\n'
372+
'Col 1 Row 2 Col 2 Row 2 ')
339373

340374
# Wide custom divider (divider needs no padding)
341375
st = SimpleTable([column_1, column_2], divider_char='深')

0 commit comments

Comments
 (0)