Skip to main content

A library for extending terminal environments with custom hooks

Project description

Terminal Extensions

A lightweight Python library for extending your terminal environment with custom hooks. This library allows you to easily add custom functionality to your terminal by writing simple Python functions.

Features

  • Simple decorator-based API for registering hooks
  • Support for both prefix-specific and global hooks
  • Automatic loading of hooks from a .hooks directory
  • Minimal overhead and easy integration

Installation

pip install terminal-extensions

Quick Start

  1. Create a .hooks directory in your project
  2. Add Python files with your hooks:
from terminal_extensions import terminal_hook

# Hook that runs for all commands
@terminal_hook()
def log_commands(command: str) -> bool:
    print(f"Executing: {command}")
    return True  # Continue processing other hooks

# Hook that only runs for commands starting with "git"
@terminal_hook("git")
def git_helper(command: str) -> bool:
    if command == "git status":
        print("Nice job checking in!")
    return True
  1. Run the terminal extensions:
terminal-ext

Writing Hooks

Hook Types

  1. Global Hooks - Run for every command:
@terminal_hook()
def my_hook(command: str) -> bool:
    # Process any command
    return True
  1. Prefix Hooks - Run only for commands with specific prefixes:
@terminal_hook("docker")
def docker_hook(command: str) -> bool:
    # Process only docker commands
    return True

Hook Return Values

  • Return True to continue processing other hooks and execute the command
  • Return False to stop processing and prevent command execution

Hook Organization

Place your hooks in .py files within the .hooks directory:

your_project/
├── .hooks/
│   ├── git_hooks.py
│   ├── docker_hooks.py
│   └── general_hooks.py
└── ...

Examples

Command Logging

@terminal_hook()
def log_commands(command: str) -> bool:
    with open("command_history.log", "a") as f:
        f.write(f"{command}\n")
    return True

Security Check

@terminal_hook("rm")
def confirm_delete(command: str) -> bool:
    response = input("Are you sure you want to delete? [y/N] ")
    return response.lower() == 'y'

Command Enhancement

@terminal_hook("git")
def git_shortcuts(command: str) -> bool:
    if command == "git st":
        print("Expanding to git status...")
        return "git status"
    return True

Configuration

The library automatically looks for hooks in the .hooks directory of your current working directory.

Error Handling

Hooks are executed in a safe environment. If a hook raises an exception:

  • The error is logged to stderr
  • Processing continues with the next hook
  • The original command is still executed (unless explicitly prevented)

Contributing

See CONTRIBUTORS.md for guidelines on contributing to this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

terminal_extensions-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

terminal_extensions-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file terminal_extensions-0.1.0.tar.gz.

File metadata

  • Download URL: terminal_extensions-0.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for terminal_extensions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3732c8d4e89d98accb4cd4fb0d9c225ce03eefab3080f8ff467b700d75cfce0f
MD5 a75bac46208f901ab13b4deb225912be
BLAKE2b-256 946e7fa2e7bf8a227ee2340899fad56a916fd206b5113d3faa7dab8ad7ba05d2

See more details on using hashes here.

File details

Details for the file terminal_extensions-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for terminal_extensions-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a852e9a2dc6a4d362b9df92330bd32f01a5677f857d26dc145cc92943c5e7f4
MD5 ccb9b0b814cc55d4cf68b85ea3e303f4
BLAKE2b-256 ac1514d20083ad41c45a5871b752fc4b9f00eb3bfffdbd5023e60661f8d4a990

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page