Skip to content

Commit 9f88697

Browse files
committed
Added max_column_completion_items init param and default it to 7
Also: - Disable dynamic complete_style switching when READLINE_LIKE is specified
1 parent 0763d8b commit 9f88697

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

cmd2/cmd2.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def __init__(
310310
intro: RenderableType = '',
311311
bottom_toolbar: bool = False,
312312
complete_style: CompleteStyle = CompleteStyle.COLUMN,
313+
max_column_completion_items: int = 7,
313314
) -> None:
314315
"""Easy but powerful framework for writing line-oriented command interpreters, extends Python's cmd package.
315316
@@ -365,6 +366,8 @@ def __init__(
365366
1. CompleteStyle.COLUMN (default) - displays hints with help next to them in one big column
366367
2. CompleteStyle.MULTI_COLUMN - displays hints across multiple columns, with help when selected
367368
3. CompleteStyle.READLINE_LIKE - displays like readline, complete_in_thread doesn't work
369+
:param max_column_completion_items: The maximum number of completion results to display in a single column,
370+
used to provide the initial value for a settable with the same name.
368371
"""
369372
# Check if py or ipy need to be disabled in this instance
370373
if not include_py:
@@ -430,7 +433,7 @@ def _(event: Any) -> None: # pragma: no cover
430433

431434
# The maximum number of completion results to display in a single column (CompleteStyle.COLUMN).
432435
# If the number of results exceeds this, CompleteStyle.MULTI_COLUMN will be used.
433-
self.max_column_completion_items: int = 7
436+
self.max_column_completion_items: int = max_column_completion_items
434437

435438
# A dictionary mapping settable names to their Settable instance
436439
self._settables: dict[str, Settable] = {}
@@ -2509,11 +2512,12 @@ def complete(
25092512
self.display_matches.sort(key=self.default_sort_key)
25102513
self.matches_sorted = True
25112514

2512-
# Swap between COLUMN and MULTI_COLUMN style based on the number of matches
2513-
if len(self.completion_matches) > self.max_column_completion_items:
2514-
self.session.complete_style = CompleteStyle.MULTI_COLUMN
2515-
else:
2516-
self.session.complete_style = CompleteStyle.COLUMN
2515+
# Swap between COLUMN and MULTI_COLUMN style based on the number of matches if not using READLINE_LIKE
2516+
if self.session.complete_style != CompleteStyle.READLINE_LIKE:
2517+
if len(self.completion_matches) > self.max_column_completion_items:
2518+
self.session.complete_style = CompleteStyle.MULTI_COLUMN
2519+
else:
2520+
self.session.complete_style = CompleteStyle.COLUMN
25172521

25182522
try:
25192523
return self.completion_matches[state]

docs/features/initialization.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Here are instance attributes of `cmd2.Cmd` which developers might wish to overri
4545
- **hidden_commands**: commands to exclude from the help menu and tab completion
4646
- **last_result**: stores results from the last command run to enable usage of results in a Python script or interactive console. Built-in commands don't make use of this. It is purely there for user-defined commands and convenience.
4747
- **macros**: dictionary of macro names and their values
48+
- **max_column_completion_items**: The maximum number of completion results to display in a single column (Default: 7)
4849
- **max_completion_items**: max number of CompletionItems to display during tab completion (Default: 50)
4950
- **pager**: sets the pager command used by the `Cmd.ppaged()` method for displaying wrapped output using a pager
5051
- **pager_chop**: sets the pager command used by the `Cmd.ppaged()` method for displaying chopped/truncated output using a pager

0 commit comments

Comments
 (0)