CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-readline

GNU readline support for Python on platforms without readline

Pending
Overview
Eval results
Files

history.mddocs/

History Management

Comprehensive history management including file operations, length control, and direct history manipulation. These functions provide complete control over command history persistence, size limits, and individual history entry management.

Capabilities

History File Operations

Functions for reading from and writing to history files, enabling persistent command history across sessions.

def read_history_file(filename=None):
    """
    Load a readline history file.
    
    Parameters:
    - filename (str, optional): Path to history file to read.
                               If None, defaults to ~/.history
    
    Returns:
    None
    
    Raises:
    IOError: If the file cannot be read
    """

def write_history_file(filename=None):
    """
    Save a readline history file.
    
    Parameters:
    - filename (str, optional): Path to history file to write.
                               If None, defaults to ~/.history
    
    Returns:
    None
    
    Raises:
    IOError: If the file cannot be written
    """

Usage Example:

import readline
import atexit

# Load history at startup
try:
    readline.read_history_file()
except FileNotFoundError:
    pass  # No history file exists yet

# Save history at exit
atexit.register(readline.write_history_file)

# Use custom history file
readline.read_history_file('/path/to/custom_history')
readline.write_history_file('/path/to/custom_history')

History Length Control

Functions for controlling the maximum number of history entries that will be maintained and saved to files.

def set_history_length(length):
    """
    Set the maximal number of items written to history file.
    
    Parameters:
    - length (int): Maximum history entries. Negative value inhibits truncation.
    
    Returns:
    None
    """

def get_history_length():
    """
    Return the maximum number of items that will be written to history file.
    
    Returns:
    int: Maximum history length (-1 means no limit)
    """

Usage Example:

import readline

# Set history to 1000 entries
readline.set_history_length(1000)

# Disable history truncation
readline.set_history_length(-1)

# Check current limit
max_entries = readline.get_history_length()
print(f"History limit: {max_entries}")

History Access and Information

Functions for accessing current history state and retrieving specific history entries.

def get_current_history_length():
    """
    Return the current (not maximum) length of history.
    
    Returns:
    int: Current number of history entries
    """

def get_history_item(index):
    """
    Return the current contents of history item at index.
    
    Parameters:
    - index (int): History item index (1-based on GNU readline, 
                   0-based on libedit/Apple systems)
    
    Returns:
    str: History line content, or None if index is invalid
    """

Usage Example:

import readline

# Check current history size
current_size = readline.get_current_history_length()
print(f"Current history has {current_size} entries")

# Access recent history entries
if current_size > 0:
    # Get the most recent entry (last command)
    last_command = readline.get_history_item(current_size)
    print(f"Last command: {last_command}")
    
    # Show last 5 commands
    start = max(1, current_size - 4)
    for i in range(start, current_size + 1):
        command = readline.get_history_item(i)
        print(f"{i}: {command}")

History Manipulation

Functions for directly modifying history entries, including adding new entries and removing or replacing existing ones.

def add_history(string):
    """
    Add a line to the history buffer.
    
    Parameters:
    - string (str): Line to add to history
    
    Returns:
    None
    """

def remove_history_item(pos):
    """
    Remove history item given by its position.
    
    Parameters:
    - pos (int): Position of history item to remove (0-based indexing)
    
    Returns:
    None
    """

def replace_history_item(pos, line):
    """
    Replace history item given by its position with contents of line.
    
    Parameters:
    - pos (int): Position of history item to replace (0-based indexing)
    - line (str): New content for history item
    
    Returns:
    None
    """

def clear_history():
    """
    Clear the current readline history.
    
    Returns:
    None
    
    Note: Only available when compiled with HAVE_RL_COMPLETION_APPEND_CHARACTER
    """

Usage Example:

import readline

# Add custom entries to history
readline.add_history("ls -la")
readline.add_history("cd /tmp")
readline.add_history("python script.py")

# Replace a history entry (0-based indexing for manipulation)
readline.replace_history_item(0, "ls -la --color=auto")

# Remove unwanted history entry
readline.remove_history_item(1)  # Remove "cd /tmp"

# Clear all history
readline.clear_history()

Platform Considerations

Apple/macOS Systems: When using libedit emulation (default on macOS), get_history_item() uses 0-based indexing instead of the 1-based indexing used by GNU readline. However, remove_history_item() and replace_history_item() always use 0-based indexing on all platforms.

Install with Tessl CLI

npx tessl i tessl/pypi-readline

docs

completion.md

configuration.md

history.md

hooks.md

index.md

line-editing.md

tile.json