Skip to main content

Class for nice and easy user interaction, with coloring and automatic translation support

Project description

x_console

PyPI version Python Versions License

Python CLI class for nice and easy user interaction, with coloring, formatting and automatic translation support.

Features

  • Rich Text Formatting - Apply colors and styles with simple token markers
  • Automatic Translation - Detect input language and translate output (supports both online and offline modes)
  • Interactive Prompts - Simple prompts, menus and selection dialogs
  • Spinners and Progress Indicators - Show processing status with animated spinners
  • Rich Command Line Interface - Built on top of rich_click with beautiful styling
  • Debug and Logging - Formatted debug messages and warnings

Installation

# Basic installation
pip install x_console

# With online translation support (using Google Translate)
pip install "x_console[online]"

# With offline translation support (using EasyNMT)
pip install "x_console[offline]"

# Full installation with all translation features
pip install "x_console[full]"

Basic Usage

from x_console import CLIManager

# Initialize the CLI manager
cli = CLIManager(debug=True, debug_prefix="APP")

# Display formatted message
cli.echo("Hello *World*! This text has _italic_ and |dim| formatting.")

# Ask for user input
name = cli.prompt("What is your name?")
cli.echo("Nice to meet you, *{name}*!", name=name)

# Show a process with spinner
def my_task():
    # This generator yields status update messages
    yield ("Starting process...", {})
    yield ("Processing step {step}...", {"step": 1})
    yield ("Processing step {step}...", {"step": 2})
    yield ("Finalizing...", {})
    
cli.process(my_task, message="Running task")

Text Formatting

x_console provides simple token-based formatting:

# Default token mappings:
# * for yellow text
# _ for italic text
# | for dim text

cli.echo("This is *yellow* text with _italic_ and |dim| formatting.")

# Custom tokens
cli.setColorTokens({
    "*": "bold red",
    "#": "blue",
    "~": "green italic",
    "@": "cyan underline"
})

cli.echo("Now *red bold*, #blue#, ~green italic~ and @underlined@!")

Language Detection and Translation

# Automatic language detection and translation
cli.setup_language("Hola mundo")  # Detects Spanish and sets as target language

# Messages will now be automatically translated
cli.echo("Hello world!")  # Will output "Hola mundo!"

# Translate text manually
english_text = cli.translate("Bonjour le monde", target_lang="en", online=True)
print(english_text)  # "Hello world"

Interactive Menus

# Create a selection menu
choices = ["Option 1", "Option 2", "Option 3"]
selected = cli.select("Choose an option:", choices, default="Option 1")
cli.echo("You selected: *{option}*", option=selected)

Command Line Applications

x_console integrates with rich_click for beautiful CLI applications:

from x_console import CLIManager

cli = CLIManager()

@cli.command()
@cli.option("--name", "-n", help="Your name")
@cli.option("--verbose", "-v", is_flag=True, help="Enable verbose output")
def hello(name, verbose):
    """Say hello to someone."""
    if verbose:
        cli.debug_("Running with verbose mode")
    cli.echo("Hello, *{name}*!", name=name or "World")

if __name__ == "__main__":
    hello()

Processing with Spinners

import time

def long_process():
    for i in range(1, 11):
        # Simulate work
        time.sleep(0.5)
        # Yield status updates as (message_template, kwargs) tuples
        yield ("Step {step} of {total}...", {"step": i, "total": 10})
    
    # Final yield with completion message
    yield ("Process completed successfully!", {})

# Run the process with a spinner
cli.process(long_process, message="Running long process")

Debug and Warning Messages

# Enable debug mode
cli.debug = True

# Debug message (only shown when debug=True)
cli.debug_("Connected to {server} on port {port}", server="example.com", port=8080)

# Warning message (only shown when debug=True)
cli.warn_("Connection timeout after {seconds}s", seconds=30)

# Log messages (always shown)
cli.log("Application started")

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

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

x_console-0.1.2.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

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

x_console-0.1.2-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file x_console-0.1.2.tar.gz.

File metadata

  • Download URL: x_console-0.1.2.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for x_console-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3721c50a56544e668703b7f1f5a3561639f9272d56708ff4a1c9c06c9093f668
MD5 6378ef8f5371411a6978b594e5b175c4
BLAKE2b-256 589cf9303670de88b5df38067f7702945dca943dbf3d778f8ef05e0953cbc697

See more details on using hashes here.

File details

Details for the file x_console-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: x_console-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for x_console-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e7e1353d534c40efb7effe9e41669573511281c6da9fcf6989f5ad2b2f4a0441
MD5 e00d02be027e373e7a83d4ef27d72d45
BLAKE2b-256 836edf70e362e2ec47d28c4b062ff5ef2c5ccd6d2265db5c478de386152c9c20

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