CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-gnureadline

The standard Python readline extension statically linked against the GNU readline library.

Pending
Overview
Eval results
Files

line-editing.mddocs/

Command Line Editing

Core line editing functionality for building interactive command-line applications with full GNU Readline support. These functions provide the foundation for input handling, buffer manipulation, and display control.

Capabilities

Configuration and Initialization

Parse readline configuration and initialize the readline library with custom settings.

def parse_and_bind(string: str):
    """
    Execute the init line provided in the string argument.
    
    Parameters:
    - string: Readline initialization command (e.g., "tab: complete", "set bell-style none")
    """

def read_init_file(filename: str = None):
    """
    Execute a readline initialization file.
    
    Parameters:
    - filename: Path to init file (default: ~/.inputrc or uses last filename)
    """

Usage Examples

import gnureadline

# Configure readline behavior
gnureadline.parse_and_bind("tab: complete")
gnureadline.parse_and_bind("set bell-style none")
gnureadline.parse_and_bind("set completion-ignore-case on")

# Load configuration from file
gnureadline.read_init_file("~/.inputrc")
gnureadline.read_init_file("/etc/inputrc")

Buffer Manipulation

Access and modify the current line buffer contents during input processing.

def get_line_buffer() -> str:
    """
    Get the current contents of the line buffer.
    
    Returns:
    str: Current line buffer content
    """

def insert_text(string: str):
    """
    Insert text into the line buffer at the cursor position.
    
    Parameters:
    - string: Text to insert
    """

Usage Examples

import gnureadline

# Set up a completion function that manipulates the buffer
def smart_completer(text: str, state: int):
    if state == 0:
        # Get current buffer to provide context-aware completion
        buffer = gnureadline.get_line_buffer()
        print(f"Current buffer: '{buffer}'")
        
        # Insert helpful text
        if text == "help":
            gnureadline.insert_text(" - available commands: ls, cd, exit")
    
    return None

gnureadline.set_completer(smart_completer)

Display Control

Control how readline displays the current line and prompt.

def redisplay():
    """Force redisplay of the current line."""

Usage Examples

import gnureadline
import signal

# Redisplay after signal handling
def signal_handler(signum, frame):
    print("\nCaught signal, refreshing display...")
    gnureadline.redisplay()

signal.signal(signal.SIGWINCH, signal_handler)

Advanced Configuration Examples

Custom Key Bindings

import gnureadline

# Set up custom key bindings
gnureadline.parse_and_bind('"\\C-l": clear-screen')
gnureadline.parse_and_bind('"\\C-u": unix-line-discard')
gnureadline.parse_and_bind('"\\C-w": unix-word-rubout')

# Vi-style editing mode
gnureadline.parse_and_bind("set editing-mode vi")

# Emacs-style editing mode (default)
gnureadline.parse_and_bind("set editing-mode emacs")

Multi-line Input Handling

import gnureadline

# Configure for multi-line input
gnureadline.parse_and_bind("set horizontal-scroll-mode off")
gnureadline.parse_and_bind("set print-completions-horizontally off")

def get_multiline_input(prompt=">>> "):
    """Get multi-line input with proper display handling."""
    lines = []
    
    while True:
        try:
            if not lines:
                line = input(prompt)
            else:
                line = input("... ")
            
            lines.append(line)
            
            # Check for continuation
            if line.endswith("\\"):
                continue
            else:
                break
                
        except EOFError:
            break
    
    # Force redisplay after multi-line input
    gnureadline.redisplay()
    return "\n".join(lines)

Install with Tessl CLI

npx tessl i tessl/pypi-gnureadline

docs

completion.md

history.md

hooks.md

index.md

line-editing.md

utilities.md

tile.json