Skip to content

Commit 50b5aa7

Browse files
committed
Fixed ValueError caused when passing Cmd.columnize() strings wider than display_width.
1 parent 23a7d22 commit 50b5aa7

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.4.3 (TBD, 2023)
2+
* Bug Fixes
3+
* Fixed ValueError caused when passing `Cmd.columnize()` strings wider than `display_width`.
4+
* Enhancements
5+
* Renamed `utils.str_to_bool()` -> `utils.to_bool()`.
6+
* Enhanced `utils.to_bool()` so that it accepts and converts `bool`, `int`, and `float` in addition to `str`.
7+
18
## 2.4.2 (July 13, 2022)
29
* Enhancements
310
* Updated argparse decorator to remove annotations when the docstring is used for a command's help text.

cmd2/cmd2.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3688,9 +3688,10 @@ def columnize(self, str_list: Optional[List[str]], display_width: int = 80) -> N
36883688
if totwidth <= display_width:
36893689
break
36903690
else:
3691+
# The output is wider than display_width. Print 1 column with each string on its own row.
36913692
nrows = len(str_list)
36923693
ncols = 1
3693-
colwidths = [0]
3694+
colwidths = [1]
36943695
for row in range(nrows):
36953696
texts = []
36963697
for col in range(ncols):

tests/test_cmd2.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ def test_select_eof(select_app, monkeypatch):
14541454
assert read_input_mock.call_count == 2
14551455

14561456

1457-
def test_select_ctrl_c(outsim_app, monkeypatch, capsys):
1457+
def test_select_ctrl_c(outsim_app, monkeypatch):
14581458
# Ctrl-C during select prints ^C and raises a KeyboardInterrupt
14591459
read_input_mock = mock.MagicMock(name='read_input', side_effect=KeyboardInterrupt)
14601460
monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock)
@@ -2866,3 +2866,12 @@ def test_transcripts_at_init():
28662866
transcript_files = ['foo', 'bar']
28672867
app = cmd2.Cmd(allow_cli_args=False, transcript_files=transcript_files)
28682868
assert app._transcript_files == transcript_files
2869+
2870+
2871+
def test_columnize_too_wide(outsim_app):
2872+
"""Test calling columnize with output that wider than display_width"""
2873+
str_list = ["way too wide", "much wider than the first"]
2874+
outsim_app.columnize(str_list, display_width=5)
2875+
2876+
expected = "\n".join(str_list) + "\n"
2877+
assert outsim_app.stdout.getvalue() == expected

0 commit comments

Comments
 (0)