CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-thefuck

Magnificent app which corrects your previous console command

Overview
Eval results
Files

core-application.mddocs/

Core Application Functions

Main application entry points, command processing, and execution functionality. These functions provide the foundation for command correction and user interaction in the thefuck application.

Capabilities

Main Entry Points

The primary CLI entry points that handle argument parsing and dispatch to appropriate functionality.

def main():
    """
    Main CLI entry point with argument parsing.
    
    Handles command-line arguments including:
    - --version: Show version information
    - --alias: Print shell alias configuration
    - command arguments: Process command for correction
    
    Returns:
    None
    """

def fix_command():
    """
    Main entry point for command correction functionality.
    
    Sets up user directory, loads settings, processes the command from
    sys.argv, generates corrections, and handles user interaction.
    
    Returns:
    None
    """

def print_alias(entry_point=True):
    """
    Prints shell alias configuration for thefuck integration.
    
    Parameters:
    - entry_point (bool): Whether called from deprecated thefuck-alias entry point
    
    Returns:
    None
    
    Prints shell-specific alias command to stdout.
    """

User Directory Management

Functions for managing the user's thefuck configuration directory and initialization.

def setup_user_dir():
    """
    Returns user config dir, creates it when it doesn't exist.
    
    Creates ~/.thefuck directory structure including:
    - Main configuration directory
    - Rules subdirectory for custom rules
    - Settings file initialization
    
    Returns:
    pathlib.Path: Path to user configuration directory
    """

Command Processing

Core command execution and processing functionality.

def get_command(settings, args):
    """
    Creates command from args and executes it.
    
    Parameters:
    - settings (Settings): Application settings
    - args (list): Command line arguments (typically sys.argv)
    
    Returns:
    Command: Command object with script, stdout, stderr or None if empty command
    
    Executes the command with proper environment and timeout handling.
    """

def wait_output(settings, popen):
    """
    Returns True if we can get output of the command in the settings.wait_command time.
    
    Parameters:
    - settings (Settings): Application settings containing wait_command timeout
    - popen (subprocess.Popen): Process to wait for
    
    Returns:
    bool: True if command completed within timeout, False if killed due to timeout
    
    Command will be killed if it wasn't finished in time.
    """

def run_command(old_cmd, command, settings):
    """
    Runs command from rule for passed command.
    
    Parameters:
    - old_cmd (Command): Original failed command
    - command (CorrectedCommand): Corrected command to execute
    - settings (Settings): Application settings
    
    Returns:
    None
    
    Executes side effects, adds to shell history, and prints the command.
    """

Usage Examples

Basic Command Processing

import sys
from thefuck.main import setup_user_dir, get_command, fix_command
from thefuck.conf import get_settings

# Setup user environment
user_dir = setup_user_dir()
settings = get_settings(user_dir)

# Process a command
command = get_command(settings, ['thefuck', 'git', 'status'])
if command:
    print(f"Executed: {command.script}")
    print(f"Output: {command.stdout}")
    print(f"Errors: {command.stderr}")

Shell Alias Generation

from thefuck.main import print_alias
import sys

# Simulate thefuck --alias call
sys.argv = ['thefuck', '--alias', 'f']
print_alias(False)  # Prints shell-specific alias configuration

Complete Correction Flow

from thefuck.main import setup_user_dir, get_command, run_command
from thefuck.conf import get_settings
from thefuck.corrector import get_corrected_commands
from thefuck.ui import select_command

# Setup
user_dir = setup_user_dir()
settings = get_settings(user_dir)

# Process failed command
args = ['thefuck', 'git', 'pussh', 'origin', 'main']
command = get_command(settings, args)

if command:
    # Get corrections
    corrected_commands = get_corrected_commands(command, user_dir, settings)
    
    # Select correction (interactive)
    selected = select_command(corrected_commands, settings)
    
    if selected:
        # Execute the correction
        run_command(command, selected, settings)

Error Handling

Functions handle various error conditions:

  • Empty commands: get_command returns None for empty input
  • Command timeouts: wait_output kills processes that exceed time limits
  • Process failures: Commands that fail to execute properly are handled gracefully
  • User interruption: Keyboard interrupts during execution are caught and handled

Environment Integration

The core application functions integrate with the system environment:

  • Shell detection: Automatically detects and integrates with current shell
  • Environment variables: Respects thefuck-specific environment settings
  • History integration: Adds corrected commands to shell history
  • Working directory: Maintains proper working directory context

Install with Tessl CLI

npx tessl i tessl/pypi-thefuck

docs

configuration.md

core-application.md

data-types.md

index.md

rule-development.md

rule-system.md

shell-integration.md

user-interface.md

utilities.md

tile.json