Skip to content

Commit 00412f4

Browse files
committed
Moved logic specific to the main command line from Cmd.read_input() to Cmd._read_command_line().
No longer setting Cmd.session.input to a pipe since that causes warnings from prompt-toolkit. For non-interactive sessions (stdin is a pipe), we use stdin.readline().
1 parent 4f68446 commit 00412f4

File tree

7 files changed

+364
-611
lines changed

7 files changed

+364
-611
lines changed

cmd2/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
from .string_utils import stylize
5353
from .styles import Cmd2Style
5454
from .utils import (
55-
CompletionMode,
5655
CustomCompletionSettings,
5756
Settable,
5857
categorize,
@@ -103,7 +102,6 @@
103102
"Cmd2Style",
104103
# Utilities
105104
'categorize',
106-
'CompletionMode',
107105
'CustomCompletionSettings',
108106
'Settable',
109107
'set_default_str_sort_key',

cmd2/cmd2.py

Lines changed: 161 additions & 196 deletions
Large diffs are not rendered by default.

cmd2/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# nothing here should be considered part of the public API of this module
55

66
INFINITY = float('inf')
7+
EOF = 'eof'
78

89
# Used for command parsing, output redirection, completion, and word breaks. Do not change.
910
QUOTES = ['"', "'"]

cmd2/pt_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212

1313
from prompt_toolkit import print_formatted_text
14+
from prompt_toolkit.application import get_app
1415
from prompt_toolkit.completion import (
1516
Completer,
1617
Completion,
@@ -95,7 +96,7 @@ def get_completions(self, document: Document, _complete_event: object) -> Iterab
9596
# and returning early, we trigger a new completion cycle where the quote
9697
# is already present, allowing for proper common prefix calculation.
9798
if completions._add_opening_quote and search_text_length > 0:
98-
buffer = self.cmd_app.session.app.current_buffer
99+
buffer = get_app().current_buffer
99100

100101
buffer.cursor_left(search_text_length)
101102
buffer.insert_text(completions._quote_char)

cmd2/utils.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
MutableSequence,
1818
)
1919
from difflib import SequenceMatcher
20-
from enum import Enum
2120
from typing import (
2221
TYPE_CHECKING,
2322
Any,
@@ -733,24 +732,6 @@ def get_defining_class(meth: Callable[..., Any]) -> type[Any] | None:
733732
return cast(type, getattr(meth, '__objclass__', None)) # handle special descriptor objects
734733

735734

736-
class CompletionMode(Enum):
737-
"""Enum for what type of completion to perform in cmd2.Cmd.read_input()."""
738-
739-
# Completion will be disabled during read_input() call
740-
# Use of custom up-arrow history supported
741-
NONE = 1
742-
743-
# read_input() will complete cmd2 commands and their arguments
744-
# cmd2's command line history will be used for up arrow if history is not provided.
745-
# Otherwise use of custom up-arrow history supported.
746-
COMMANDS = 2
747-
748-
# read_input() will complete based on one of its following parameters:
749-
# choices, choices_provider, completer, parser
750-
# Use of custom up-arrow history supported
751-
CUSTOM = 3
752-
753-
754735
class CustomCompletionSettings:
755736
"""Used by cmd2.Cmd.complete() to complete strings other than command arguments."""
756737

examples/read_input.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ def do_basic_with_history(self, _) -> None:
3232
else:
3333
self.custom_history.append(input_str)
3434

35-
@cmd2.with_category(EXAMPLE_COMMANDS)
36-
def do_commands(self, _) -> None:
37-
"""Call read_input the same way cmd2 prompt does to read commands."""
38-
self.poutput("Tab completing and up-arrow history configured for commands")
39-
with contextlib.suppress(EOFError):
40-
self.read_input("> ", completion_mode=cmd2.CompletionMode.COMMANDS)
41-
4235
@cmd2.with_category(EXAMPLE_COMMANDS)
4336
def do_custom_choices(self, _) -> None:
4437
"""Call read_input to use custom history and choices."""
@@ -47,17 +40,16 @@ def do_custom_choices(self, _) -> None:
4740
input_str = self.read_input(
4841
"> ",
4942
history=self.custom_history,
50-
completion_mode=cmd2.CompletionMode.CUSTOM,
5143
choices=['choice_1', 'choice_2', 'choice_3'],
5244
)
5345
except EOFError:
5446
pass
5547
else:
5648
self.custom_history.append(input_str)
5749

58-
def choices_provider(self) -> list[str]:
50+
def choices_provider(self) -> cmd2.Choices:
5951
"""Example choices provider function."""
60-
return ["from_provider_1", "from_provider_2", "from_provider_3"]
52+
return cmd2.Choices.from_values(["from_provider_1", "from_provider_2", "from_provider_3"])
6153

6254
@cmd2.with_category(EXAMPLE_COMMANDS)
6355
def do_custom_choices_provider(self, _) -> None:
@@ -67,7 +59,6 @@ def do_custom_choices_provider(self, _) -> None:
6759
input_str = self.read_input(
6860
"> ",
6961
history=self.custom_history,
70-
completion_mode=cmd2.CompletionMode.CUSTOM,
7162
choices_provider=ReadInputApp.choices_provider,
7263
)
7364
except EOFError:
@@ -80,9 +71,7 @@ def do_custom_completer(self, _) -> None:
8071
"""Call read_input to use custom history and completer function."""
8172
self.poutput("Tab completing paths and using custom history")
8273
try:
83-
input_str = self.read_input(
84-
"> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM, completer=cmd2.Cmd.path_complete
85-
)
74+
input_str = self.read_input("> ", history=self.custom_history, completer=cmd2.Cmd.path_complete)
8675
self.custom_history.append(input_str)
8776
except EOFError:
8877
pass
@@ -99,9 +88,7 @@ def do_custom_parser(self, _) -> None:
9988
self.poutput(parser.format_usage())
10089

10190
try:
102-
input_str = self.read_input(
103-
"> ", history=self.custom_history, completion_mode=cmd2.CompletionMode.CUSTOM, parser=parser
104-
)
91+
input_str = self.read_input("> ", history=self.custom_history, parser=parser)
10592
except EOFError:
10693
pass
10794
else:

0 commit comments

Comments
 (0)