Skip to content

Commit e38f0f7

Browse files
authored
Refactor input (#1585)
- Moved logic specific to the main command line from Cmd.read_input() to Cmd._read_command_line(). - Removed self.use_rawinput. - Added boolean, Cmd.interactive_pipe, which supports both interactive and non-interactive sessions when input comes from a pipe.
1 parent 4f545de commit e38f0f7

File tree

18 files changed

+513
-886
lines changed

18 files changed

+513
-886
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ cmd2/argparse_*.py @kmvanbrunt @anselor
3131
cmd2/clipboard.py @tleonhardt
3232
cmd2/cmd2.py @tleonhardt @kmvanbrunt
3333
cmd2/colors.py @tleonhardt @kmvanbrunt
34-
cmd2/command_definition.py @anselor
34+
cmd2/command_definition.py @anselor @kmvanbrunt
35+
cmd2/completion.py @kmvanbrunt
3536
cmd2/constants.py @tleonhardt @kmvanbrunt
3637
cmd2/decorators.py @kmvanbrunt @anselor
3738
cmd2/exceptions.py @kmvanbrunt @anselor
@@ -43,7 +44,6 @@ cmd2/py_bridge.py @kmvanbrunt
4344
cmd2/rich_utils.py @kmvanbrunt
4445
cmd2/string_utils.py @kmvanbrunt
4546
cmd2/styles.py @tleonhardt @kmvanbrunt
46-
cmd2/terminal_utils.py @kmvanbrunt
4747
cmd2/utils.py @tleonhardt @kmvanbrunt
4848

4949
# Documentation

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ prompt is displayed.
5656
`Statement.redirect_to`.
5757
- Changed `StatementParser.parse_command_only()` to return a `PartialStatement` object.
5858
- Renamed `Macro.arg_list` to `Macro.args`.
59+
- Removed `terminal_utils.py` since `prompt-toolkit` provides this functionality.
5960
- Enhancements
6061
- New `cmd2.Cmd` parameters
6162
- **auto_suggest**: (boolean) if `True`, provide fish shell style auto-suggestions. These

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: 213 additions & 222 deletions
Large diffs are not rendered by default.

cmd2/completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Provides classes and functions related to completion."""
1+
"""Provides classes and functions related to command-line completion."""
22

33
import re
44
import sys

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/terminal_utils.py

Lines changed: 0 additions & 144 deletions
This file was deleted.

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

docs/api/completion.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# cmd2.completion
2+
3+
::: cmd2.completion

docs/api/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ incremented according to the [Semantic Version Specification](https://semver.org
1818
- [cmd2.colors](./colors.md) - StrEnum of all color names supported by the Rich library
1919
- [cmd2.command_definition](./command_definition.md) - supports the definition of commands in
2020
separate classes to be composed into cmd2.Cmd
21+
- [cmd2.completion](./completion.md) - classes and functions related to command-line completion
2122
- [cmd2.constants](./constants.md) - constants used in `cmd2`
2223
- [cmd2.decorators](./decorators.md) - decorators for `cmd2` commands
2324
- [cmd2.exceptions](./exceptions.md) - custom `cmd2` exceptions
@@ -30,5 +31,4 @@ incremented according to the [Semantic Version Specification](https://semver.org
3031
- [cmd2.rich_utils](./rich_utils.md) - common utilities to support Rich in cmd2 applications
3132
- [cmd2.string_utils](./string_utils.md) - string utility functions
3233
- [cmd2.styles](./styles.md) - cmd2-specific Rich styles and a StrEnum of their corresponding names
33-
- [cmd2.terminal_utils](./terminal_utils.md) - support for terminal control escape sequences
3434
- [cmd2.utils](./utils.md) - various utility classes and functions

0 commit comments

Comments
 (0)