diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2023-01-27 18:15:38 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2023-01-27 19:24:27 -0500 |
commit | 50b5aa7523c1b12fa038c8bfb4b68efe680a63c7 (patch) | |
tree | 17007271b5e211dced84de2f1dc8ee14cf626a68 | |
parent | 23a7d22cb3d2261e77bed3480f33f45cafd21cc2 (diff) | |
download | cmd2-git-50b5aa7523c1b12fa038c8bfb4b68efe680a63c7.tar.gz |
Fixed ValueError caused when passing Cmd.columnize() strings wider than display_width.
-rw-r--r-- | CHANGELOG.md | 7 | ||||
-rw-r--r-- | cmd2/cmd2.py | 3 | ||||
-rwxr-xr-x | tests/test_cmd2.py | 11 |
3 files changed, 19 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index b972fc82..ef4b6883 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 2.4.3 (TBD, 2023) +* Bug Fixes + * Fixed ValueError caused when passing `Cmd.columnize()` strings wider than `display_width`. +* Enhancements + * Renamed `utils.str_to_bool()` -> `utils.to_bool()`. + * Enhanced `utils.to_bool()` so that it accepts and converts `bool`, `int`, and `float` in addition to `str`. + ## 2.4.2 (July 13, 2022) * Enhancements * Updated argparse decorator to remove annotations when the docstring is used for a command's help text. diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 7fb85a1a..417d45c3 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3688,9 +3688,10 @@ class Cmd(cmd.Cmd): if totwidth <= display_width: break else: + # The output is wider than display_width. Print 1 column with each string on its own row. nrows = len(str_list) ncols = 1 - colwidths = [0] + colwidths = [1] for row in range(nrows): texts = [] for col in range(ncols): diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 4a29c2f6..5bcceb34 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -1454,7 +1454,7 @@ def test_select_eof(select_app, monkeypatch): assert read_input_mock.call_count == 2 -def test_select_ctrl_c(outsim_app, monkeypatch, capsys): +def test_select_ctrl_c(outsim_app, monkeypatch): # Ctrl-C during select prints ^C and raises a KeyboardInterrupt read_input_mock = mock.MagicMock(name='read_input', side_effect=KeyboardInterrupt) monkeypatch.setattr("cmd2.Cmd.read_input", read_input_mock) @@ -2866,3 +2866,12 @@ def test_transcripts_at_init(): transcript_files = ['foo', 'bar'] app = cmd2.Cmd(allow_cli_args=False, transcript_files=transcript_files) assert app._transcript_files == transcript_files + + +def test_columnize_too_wide(outsim_app): + """Test calling columnize with output that wider than display_width""" + str_list = ["way too wide", "much wider than the first"] + outsim_app.columnize(str_list, display_width=5) + + expected = "\n".join(str_list) + "\n" + assert outsim_app.stdout.getvalue() == expected |