Skip to main content
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:

  1. Modifies global readline state
  2. Often leaks those modifications
  3. Reimplements the same completion caching
  4. 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 pyreadline and typing, which are pure-Python
  • ✓ Compatibility - Supports all operating systems and Python 2+

Real-World Use Cases

  1. REPLs - Give users tab completion without breaking their existing shell
  2. CLI tools - Add history support that persists between runs
  3. Interactive apps - Implement custom key bindings safely
  4. 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)

Source distribution for managed-readline-sessions 0.1.0a6
File Size Uploaded
managed_readline_sessions-0.1.0a6.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for managed-readline-sessions 0.1.0a6
File Interpreter ABI Platform
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

Release history Release notifications | RSS feed

This release

0.1.0a6 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page