Skip to content

Conversation

@tleonhardt
Copy link
Member

@tleonhardt tleonhardt commented Dec 29, 2025

This PR migrates from using GNU Readline for user input, tab-completion, and history to using prompt-toolkit for the same purpose.

prompt-toolkit is a pure-Python readline replacement that is fully cross-platform compatible and doesn't rely on the presence of underlying C dynamic libraries. It also opens the door to some advanced features in the future.

All use of readline is removed and the cmd2.rl_utils module has been deleted. There is a new cmd2.pt_utils module in its place. Currently all linting is passing and the documentation builds. This is essentially final draft that needs more testing. There are likely a some things that need to be considered related to asynicio support, signal handlers, and blocking popen calls.

All tests are passing on all platforms. I have done some manual testing on both macOS and Linux. But I don't have a Windows VM to do any manual testing on Windows however.

NOTE FOR REVIEWERS: The substantive code changes are in the 3 following files (all in the cmd2 directory):

  • argparse_completer.py (modified)
  • cmd2.py (modified)
  • pt_utils.py (new)

TODO:

  • Fix so tab-completion hints shown up above or below the prompt but not in the the bottom bar
  • Fix tab completion for for command/sub-command names and arguments so it uses prompt-toolkit completion
  • Fix subcommand suggestion for alias create <TAB> - currently showing nothing
  • Increaes code coverage for new code
  • Modify getting_started.py example to show how to use the optional bottom toolbar feature
  • Update documentation
  • Self review and code / comment cleeanup
  • Do a bunch of manual testing related to edge cases on all platforms
    • Windows
    • macOS
    • Linux
  • Think about asyncio cases
  • Think about interactions with signal handlers
  • Thank about blocking calls to things like popen

Tons of tests failing and some even getting stuck.
…f the correct arguments to app.complete()

This fixes all of the test_argparse_comleter.py tests.

There are still failing tests in test_cmd2.py, test_history.py, and test_run_pyscript.py as well as a test in test_cmd2.py that gets stuck.
TODO:
- prompt-toolkit history isn't properly initialized with history from a persistent history file, as shown by the remaining failing history test
… of mocking the built-in input function.

There are still 3 failing and 1 skipped test in test_cmd2.py

Additionally, some tests in test_run_pyscript.py are getting stuck.All tests in other files are passing.
Also:
- Fixed make clean so it cleans up code coverage file artifacts
Also added a bottom toolboar for displaying these type hints.
@tleonhardt tleonhardt added this to the 4.0.0 milestone Dec 29, 2025
@tleonhardt tleonhardt added enhancement major dependencies Pull requests that update a dependency file labels Dec 29, 2025
@github-actions
Copy link
Contributor

🤖 Hi @tleonhardt, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@codecov
Copy link

codecov bot commented Dec 29, 2025

Codecov Report

❌ Patch coverage is 99.65035% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.08%. Comparing base (a99a17e) to head (ed6351d).

Files with missing lines Patch % Lines
cmd2/cmd2.py 99.17% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1553      +/-   ##
==========================================
+ Coverage   98.94%   99.08%   +0.13%     
==========================================
  Files          21       21              
  Lines        4942     4895      -47     
==========================================
- Hits         4890     4850      -40     
+ Misses         52       45       -7     
Flag Coverage Δ
unittests 99.08% <99.65%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions
Copy link
Contributor

🤖 I'm sorry @tleonhardt, but I was unable to process your request. Please see the logs for more details.

@tleonhardt tleonhardt mentioned this pull request Dec 29, 2025
12 tasks
Set complete_style=CompleteStyle.READLINE_LIKE and complete_in_thread=True to restore <TAB><TAB> behavior showing all options, mimicking readline.
…ttom toolbar

Removed completion hints from the bottom toolbar and implemented printing them above the prompt with ANSI support. Added an optional 'include_bottom_toolbar' parameter to Cmd.__init__ which displays the application name (sys.argv[0]) in the bottom toolbar when enabled.
Subcommands completion hint (often a list of subcommands) was being displayed above the prompt, which is redundant when prompt-toolkit displays the subcommands in the completion menu. This change suppresses the hint for _SubParsersAction, allowing the completion menu to be the primary guide for subcommands.
Suppressed the generation of a completion hint for the main command name completion. This ensures that the command list appears in the prompt-toolkit completion menu below the prompt without a redundant or distracting hint above the prompt.
Switched PromptSession to CompleteStyle.COLUMN and disabled completion while typing to ensure the menu appears on the first Tab press with descriptions. Updated ArgparseCompleter to return CompletionItems for subcommands and main commands, allowing their descriptions to appear in the prompt-toolkit menu instead of a redundant hint table above the prompt. Restored printing of hint tables for other argument types.
Fixed an issue where argparse subcommands and flags were not showing in the prompt-toolkit completion menu on an empty Tab press. Improved Cmd2Completer to accurately calculate the word being completed using cmd2 delimiters. Refactored ArgparseCompleter to reduce complexity and return CompletionItems for flags, providing descriptions in the completion menu. Updated test suite to reflect improved functionality.
Fixed an issue where argparse subcommands and flags were not showing in the prompt-toolkit completion menu on an empty Tab press. Improved Cmd2Completer to accurately calculate the word being completed using cmd2 delimiters. Refactored ArgparseCompleter to reduce complexity and return CompletionItems for subcommands and flags, providing descriptions in the completion menu. Updated test suite to reflect improved functionality and maintain compatibility.
Updated ArgparseCompleter integration to display required argument hints in the prompt-toolkit bottom toolbar. This ensures hints like 'Hint: name name of this alias' are visible below the prompt when no completion matches are available. Restored printing of hint tables above the prompt for other argument types.
Updated ArgparseCompleter integration to display required argument hints by printing them above the prompt and allowing prompt-toolkit to redraw the prompt. This ensures hints like 'Hint: name name of this alias' are visible when no completion matches are available. Removed previous bottom toolbar implementation for hints.
Added unit tests for completion hints, hint tables, custom delimiter logic, and CompletionItem metadata handling in pt_utils.py. Achieved 100% code coverage (excluding TYPE_CHECKING blocks) and modernized existing tests to use real Document objects.
Updated Cmd._bottom_toolbar to display the application name in green on the left and the current ISO timestamp in blue on the right when include_bottom_toolbar is enabled. Used padding to achieve right-alignment of the timestamp. Updated associated tests to match the new return format.
Modified getting_started.py to spawn a background thread that triggers a UI redraw twice a second. This ensures that dynamic content in the bottom toolbar, such as the timestamp, stays current while waiting for user input.
Updated Cmd._bottom_toolbar to format the current time with 0.01s precision (two decimal places for fractional seconds). Maintained the ISO-like format including the timezone offset.
Updated documentation throughout the docs/ directory to reflect the migration from GNU Readline to prompt-toolkit and the addition of the new bottom toolbar feature. Added an upgrade guide section for 4.x, updated the history and prompt feature pages, and adjusted mkdocs configuration to allow documentation of the _bottom_toolbar method.
@tleonhardt tleonhardt marked this pull request as ready for review January 17, 2026 04:48
@tleonhardt tleonhardt changed the title [DRAFT] Migrate from using readline to prompt-toolkit Migrate from using readline to prompt-toolkit Jan 17, 2026
…pletion

Added a unit test to verify that visible commands, aliases, and macros are correctly returned as CompletionItem objects for tab completion. Specifically addressed the case where a command has no docstring.
@tleonhardt
Copy link
Member Author

@kmvanbrunt This is ready for your review when you get a chance.

@anselor @kotfu @theagilehacker @joqual If any of you have feedback, I'd also greatly appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file enhancement major

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants