Skip to content

Commit 653a767

Browse files
committed
Updated unit tests and comments.
1 parent ab1bdfa commit 653a767

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

cmd2/cmd2.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4043,6 +4043,14 @@ def complete_help_subcommands(
40434043
return completer.complete_subcommand_help(text, line, begidx, endidx, arg_tokens['subcommands'])
40444044

40454045
def _build_command_info(self) -> tuple[dict[str, list[str]], list[str], list[str], list[str]]:
4046+
"""Categorizes and sorts visible commands and help topics for display.
4047+
4048+
:return: tuple containing:
4049+
- dictionary mapping category names to lists of command names
4050+
- list of documented command names
4051+
- list of undocumented command names
4052+
- list of help topic names that are not also commands
4053+
"""
40464054
# Get a sorted list of help topics
40474055
help_topics = sorted(self.get_help_topics(), key=self.default_sort_key)
40484056

@@ -4268,7 +4276,8 @@ def columnize(self, str_list: list[str] | None, display_width: int = 80) -> None
42684276
# The output is wider than display_width. Print 1 column with each string on its own row.
42694277
nrows = len(str_list)
42704278
ncols = 1
4271-
colwidths = [1]
4279+
max_width = max(su.str_width(s) for s in str_list)
4280+
colwidths = [max_width]
42724281
for row in range(nrows):
42734282
texts = []
42744283
for col in range(ncols):

tests/test_cmd2.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,10 @@ class HelpApp(cmd2.Cmd):
12451245

12461246
def __init__(self, *args, **kwargs) -> None:
12471247
super().__init__(*args, **kwargs)
1248-
self.doc_leader = "I now present you a list of help topics."
1248+
self.doc_leader = "I now present you with a list of help topics."
1249+
self.doc_header = "My very custom doc header."
1250+
self.misc_header = "Various topics found here."
1251+
self.undoc_header = "Why did no one document these?"
12491252

12501253
def do_squat(self, arg) -> None:
12511254
"""This docstring help will never be shown because the help_squat method overrides it."""
@@ -1360,6 +1363,16 @@ def test_columnize_empty_list(capsys) -> None:
13601363
assert "<empty>" in out
13611364

13621365

1366+
def test_columnize_too_wide(capsys) -> None:
1367+
help_app = HelpApp()
1368+
commands = ["kind_of_long_string", "a_slightly_longer_string"]
1369+
help_app.columnize(commands, display_width=10)
1370+
out, err = capsys.readouterr()
1371+
1372+
expected = "kind_of_long_string \na_slightly_longer_string\n"
1373+
assert expected == out
1374+
1375+
13631376
class HelpCategoriesApp(cmd2.Cmd):
13641377
"""Class for testing custom help_* methods which override docstring help."""
13651378

0 commit comments

Comments
 (0)