Pre-release
This release is a pre-release and may not be stable for production use.
managed-readline-sessions
The Problem
Every Python CLI tool that wants:
- Tab completion
- Command history
- Custom key bindings
...ends up writing the same fragile boilerplate that:
- Modifies global readline state
- Often leaks those modifications
- Reimplements the same completion caching
- Struggles with nested scenarios
The Solution
import os.path
import typing
from managed_readline_sessions import TabBasedTokenCompletionSession, ReadWriteHistoryFileSession, PrefilledExampleTextSession
all_commands = ['help', 'exit', 'load']
# Can return anything that is `typing.Iterable[str]`
# We will consume the iterable and cache its elements
# Every time the user initiates a tab-based token completion
# And the values passed to `line`, `token`, and `index` change
def get_token_completions(line: str, token: str, index: int) -> typing.Iterable[str]:
# The token starts at the beginning of the line with no characters in front of it
if index == 0:
for command in all_commands:
# The token is a prefix of a command
if command.startswith(token):
# Add a space at the end of the command such that the user moves on to enter the next token
yield command + ' '
# Delimit tokens with spaces
token_boundary_delimiters = {' '}
with ReadWriteHistoryFileSession(os.path.join(os.path.expanduser('~'), '.myapp_history')):
while True:
with TabBasedTokenCompletionSession(get_token_completions, token_boundary_delimiters):
# Show a template each time (e.g., a frequently used command style)
with PrefilledExampleTextSession('myapp run --input='):
try:
command_line = input('myapp> ')
# The line will start with 'myapp run --input=' pre-inserted for the user to edit
# Process `command_line`...
except EOFError:
break # History saved automatically
Now your tool has:
- Persistent history
- Tab completion
- Clean state management
- Professional UX
All in just 10 lines of bulletproof code!
Key Benefits
- ✓ Guaranteed cleanup - Never corrupt a user's shell session again
- ✓ Nested sessions - Works correctly when called from other tools
- ✓ Performance optimized - Smart completion caching
- ✓ Example-filled prompts - Guide users with prefilled templates
- ✓ Battle-tested - Properly handles edge cases most implementations miss
- ✓ Zero dependencies - Except
pyreadlineandtyping, which are pure-Python - ✓ Compatibility - Supports all operating systems and Python 2+
Real-World Use Cases
- REPLs - Give users tab completion without breaking their existing shell
- CLI tools - Add history support that persists between runs
- Interactive apps - Implement custom key bindings safely
- Debuggers - Offer completion without interfering with parent process
Installation
pip install managed-readline-sessions
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Release files for managed-readline-sessions 0.1.0a6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| managed_readline_sessions-0.1.0a6.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 11.6 kB
Release files / managed_readline_sessions-0.1.0a6.tar.gz
| Download URL | managed_readline_sessions-0.1.0a6.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26a20f2635946abfb63ec539cc1db713257601c239b3262fc1f5bcda24494165
|
|
BLAKE2b-256 checksum How to use checksums |
45d816047751d0fbfb8bc2b30532b01c0184d36a9cbd3e61c55ce33b31ffaf57
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|
Release files / managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl
| Download URL | managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
b010e6c198c0655e0e4a53f712deb4415fae9848033781e1d5103decb866e513
|
|
BLAKE2b-256 checksum How to use checksums |
f26c447b51ee39f20ef12fcca9afcd563def1ea1b60f0a3de95dfbe5ee7bed7e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|