Skip to content

Commit ef36ba2

Browse files
committed
Replace readline with prompt-toolkit in documentation and such
1 parent 9b12dd2 commit ef36ba2

File tree

11 files changed

+34
-80
lines changed

11 files changed

+34
-80
lines changed

.github/CONTRIBUTING.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ Nearly all project configuration, including for dependencies and quality tools i
6161

6262
See the `dependencies` list under the `[project]` heading in [pyproject.toml](../pyproject.toml).
6363

64-
| Prerequisite | Minimum Version | Purpose |
65-
| ---------------------------------------------------------- | --------------- | ------------------------------------------------------ |
66-
| [python](https://www.python.org/downloads/) | `3.10` | Python programming language |
67-
| [pyperclip](https://github.com/asweigart/pyperclip) | `1.8` | Cross-platform clipboard functions |
68-
| [rich](https://github.com/Textualize/rich) | `14.1.0` | Add rich text and beautiful formatting in the terminal |
69-
| [rich-argparse](https://github.com/hamdanal/rich-argparse) | `1.7.1` | A rich-enabled help formatter for argparse |
70-
71-
> `macOS` and `Windows` each have an extra dependency to ensure they have a viable alternative to
72-
> [readline](https://tiswww.case.edu/php/chet/readline/rltop.html) available.
64+
| Prerequisite | Minimum Version | Purpose |
65+
| ------------------------------------------------------------------------- | --------------- | ------------------------------------------------------ |
66+
| [prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit) | `3.0.52` | Replacement for GNU `readline` that is cross-platform |
67+
| [python](https://www.python.org/downloads/) | `3.10` | Python programming language |
68+
| [pyperclip](https://github.com/asweigart/pyperclip) | `1.8` | Cross-platform clipboard functions |
69+
| [rich](https://github.com/Textualize/rich) | `14.1.0` | Add rich text and beautiful formatting in the terminal |
70+
| [rich-argparse](https://github.com/hamdanal/rich-argparse) | `1.7.1` | A rich-enabled help formatter for argparse |
7371

7472
> Python 3.10 depends on [backports.strenum](https://github.com/clbarnes/backports.strenum) to use
7573
> the `enum.StrEnum` class introduced in Python 3.11.

cmd2/terminal_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
9696
9797
:param terminal_columns: terminal width (number of columns)
9898
:param prompt: current onscreen prompt
99-
:param line: current contents of the Readline line buffer
99+
:param line: current contents of the prompt-toolkit line buffer
100100
:param cursor_offset: the offset of the current cursor position within line
101101
:param alert_msg: the message to display to the user
102102
:return: the correct string so that the alert message appears to the user to be printed above the current line.

docs/examples/getting_started.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ persist between invocations of your application, you'll need to do a little work
266266

267267
Users can access command history using two methods:
268268

269-
- The [readline](https://docs.python.org/3/library/readline.html) library which provides a Python
270-
interface to the [GNU readline library](https://en.wikipedia.org/wiki/GNU_Readline)
269+
- The [prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit) library which
270+
provides a pure Python replacement for the
271+
[GNU readline library](https://en.wikipedia.org/wiki/GNU_Readline) which is fully cross-platform
272+
compatible
271273
- The `history` command which is built-in to `cmd2`
272274

273275
From the prompt in a `cmd2`-based application, you can press `Control-p` to move to the previously

docs/features/completion.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ from `cmd2.Cmd`:
2020
complete_foo = cmd2.Cmd.path_complete
2121
```
2222

23-
This will effectively define the `complete_foo` readline completer method in your class and make it
24-
utilize the same path completion logic as the built-in commands.
23+
This will effectively define the `complete_foo` prompt-toolkit completer method in your class and
24+
make it utilize the same path completion logic as the built-in commands.
2525

2626
The built-in logic allows for a few more advanced path completion capabilities, such as cases where
2727
you only want to match directories. Suppose you have a custom command `bar` implemented by the

docs/features/history.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
The `cmd` module from the Python standard library includes `readline` history.
66

7-
[cmd2.Cmd][] offers the same `readline` capabilities, but also maintains its own data structures for
8-
the history of all commands entered by the user. When the class is initialized, it creates an
9-
instance of the [cmd2.history.History][] class (which is a subclass of `list`) as
10-
`cmd2.Cmd.history`.
7+
[cmd2.Cmd][] offers the same `readline` capabilitie via use of `prompt-toolkit`, but also maintains
8+
its own data structures for the history of all commands entered by the user. When the class is
9+
initialized, it creates an instance of the [cmd2.history.History][] class (which is a subclass of
10+
`list`) as `cmd2.Cmd.history`.
1111

1212
Each time a command is executed (this gets complex, see
1313
[Command Processing Loop](./hooks.md#command-processing-loop) for exactly when) the parsed
@@ -20,9 +20,9 @@ this format instead of plain text to preserve the complete `cmd2.Statement` obje
2020

2121
!!! note
2222

23-
`readline` saves everything you type, whether it is a valid command or not. `cmd2` only saves input to internal history if the command parses successfully and is a valid command. This design choice was intentional, because the contents of history can be saved to a file as a script, or can be re-run. Not saving invalid input reduces unintentional errors when doing so.
23+
`prompt-toolkit` saves everything you type, whether it is a valid command or not. `cmd2` only saves input to internal history if the command parses successfully and is a valid command. This design choice was intentional, because the contents of history can be saved to a file as a script, or can be re-run. Not saving invalid input reduces unintentional errors when doing so.
2424

25-
However, this design choice causes an inconsistency between the `readline` history and the `cmd2` history when you enter an invalid command: it is saved to the `readline` history, but not to the `cmd2` history.
25+
However, this design choice causes an inconsistency between the `prompt-toolkit` history and the `cmd2` history when you enter an invalid command: it is saved to the `prompt-toolkit` history, but not to the `cmd2` history.
2626

2727
The `cmd2.Cmd.history` attribute, the `cmd2.history.History` class, and the
2828
[cmd2.history.HistoryItem][] class are all part of the public API for `cmd2.Cmd`. You could use
@@ -34,13 +34,13 @@ built-in `history` command works).
3434
You can use the :arrow_up: up and :arrow_down: down arrow keys to move through the history of
3535
previously entered commands.
3636

37-
If the `readline` module is installed, you can press `Control-p` to move to the previously entered
38-
command, and `Control-n` to move to the next command. You can also search through the command
39-
history using `Control-r`.
37+
You can press `Control-p` to move to the previously entered command, and `Control-n` to move to the
38+
next command. You can also search through the command history using `Control-r`.
4039

4140
You can refer to the [readline cheat sheet](http://readline.kablamo.org/emacs.html) or you can dig
42-
into the [GNU Readline User Manual](http://man7.org/linux/man-pages/man3/readline.3.html) for all
43-
the details, including instructions for customizing the key bindings.
41+
into the
42+
[Prompt Toolkit User Manual](https://python-prompt-toolkit.readthedocs.io/en/stable/pages/advanced_topics/key_bindings.html)
43+
for all the details, including instructions for customizing the key bindings.
4444

4545
`cmd2` makes a third type of history access available with the `history` command. Each time the user
4646
enters a command, `cmd2` saves the input. The `history` command lets you do interesting things with

docs/features/prompt.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ for an example of dynamically updating the prompt.
3131
`cmd2` provides these functions to provide asynchronous feedback to the user without interfering
3232
with the command line. This means the feedback is provided to the user when they are still entering
3333
text at the prompt. To use this functionality, the application must be running in a terminal that
34-
supports [VT100](https://en.wikipedia.org/wiki/VT100) control characters and `readline`. Linux, Mac,
35-
and Windows 10 and greater all support these.
34+
supports [VT100](https://en.wikipedia.org/wiki/VT100) control characters. Linux, Mac, and Windows 10
35+
and greater all support these.
3636

3737
- [cmd2.Cmd.async_alert][]
3838
- [cmd2.Cmd.async_update_prompt][]

docs/migrating/why.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ top-notch interactive command-line experience for their users.
3232
After switching from [cmd][cmd] to `cmd2`, your application will have the following new features and
3333
capabilities, without you having to do anything:
3434

35-
- More robust [History](../features/history.md). Both [cmd][cmd] and `cmd2` have readline history,
36-
but `cmd2` also has a robust `history` command which allows you to edit prior commands in a text
37-
editor of your choosing, re-run multiple commands at a time, save prior commands as a script to be
38-
executed later, and much more.
35+
- More robust [History](../features/history.md). Both [cmd][cmd] and `cmd2` have readline-style
36+
history, but `cmd2` also has a robust `history` command which allows you to edit prior commands in
37+
a text editor of your choosing, re-run multiple commands at a time, save prior commands as a
38+
script to be executed later, and much more.
3939
- Users can redirect output to a file or pipe it to some other operating system command. You did
4040
remember to use `self.stdout` instead of `sys.stdout` in all of your print functions, right? If
4141
you did, then this will work out of the box. If you didn't, you'll have to go back and fix them.

docs/overview/installation.md

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,3 @@ If you wish to permanently uninstall `cmd2`, this can also easily be done with
8888
[pip](https://pypi.org/project/pip):
8989

9090
$ pip uninstall cmd2
91-
92-
## readline Considerations
93-
94-
`cmd2` heavily relies on Python's built-in
95-
[readline](https://docs.python.org/3/library/readline.html) module for its tab completion
96-
capabilities. Tab completion for `cmd2` applications is only tested against :simple-gnu:
97-
[GNU Readline](https://tiswww.case.edu/php/chet/readline/rltop.html) or libraries fully compatible
98-
with it. It does not work properly with the :simple-netbsd: NetBSD
99-
[Editline](http://thrysoee.dk/editline/) library (`libedit`) which is similar, but not identical to
100-
GNU Readline. `cmd2` will disable all tab-completion support if an incompatible version of
101-
`readline` is found.
102-
103-
When installed using `pip`, `uv`, or similar Python packaging tool on either `macOS` or `Windows`,
104-
`cmd2` will automatically install a compatible version of readline.
105-
106-
Most Linux operating systems come with a compatible version of readline. However, if you are using a
107-
tool like `uv` to install Python on your system and configure a virtual environment, `uv` installed
108-
versions of Python come with `libedit`. If you are using `cmd2` on Linux with a version of Python
109-
installed via `uv`, you will likely need to manually add the `gnureadline` Python module to your
110-
`uv` virtual environment.
111-
112-
```sh
113-
uv pip install gnureadline
114-
```
115-
116-
macOS comes with the [libedit](http://thrysoee.dk/editline/) library which is similar, but not
117-
identical, to GNU Readline. Tab completion for `cmd2` applications is only tested against GNU
118-
Readline. In this case you just need to install the `gnureadline` Python package which is statically
119-
linked against GNU Readline:
120-
121-
```shell
122-
$ pip install -U gnureadline
123-
```

docs/overview/integrating.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,3 @@ We recommend that you follow the advice given by the Python Packaging User Guide
1313
[install_requires](https://packaging.python.org/discussions/install-requires-vs-requirements/). By
1414
setting an upper bound on the allowed version, you can ensure that your project does not
1515
inadvertently get installed with an incompatible future version of `cmd2`.
16-
17-
## OS Considerations
18-
19-
If you would like to use [Tab Completion](../features/completion.md), then you need a compatible
20-
version of [readline](https://tiswww.case.edu/php/chet/readline/rltop.html) installed on your
21-
operating system (OS). `cmd2` forces a sane install of `readline` on both `Windows` and `macOS`, but
22-
does not do so on `Linux`. If for some reason, you have a version of Python on a Linux OS who's
23-
built-in `readline` module is based on the
24-
[Editline Library (libedit)](https://www.thrysoee.dk/editline/) instead of `readline`, you will need
25-
to manually add a dependency on `gnureadline`. Make sure to include the following dependency in your
26-
`pyproject.toml` or `setup.py`:
27-
28-
'gnureadline'

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ classifiers = [
3030
]
3131
dependencies = [
3232
"backports.strenum; python_version == '3.10'",
33-
"prompt-toolkit>=3.0.48",
33+
"prompt-toolkit>=3.0.52",
3434
"pyperclip>=1.8.2",
3535
"rich>=14.1.0",
3636
"rich-argparse>=1.7.1",

0 commit comments

Comments
 (0)