Files
command-assistant/edison_theme_demo.py
2025-04-09 09:34:15 +02:00

123 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""
Edison theme demonstration script.
This script shows how Edison's theme functionality works with its markdown_utils module.
"""
import sys
import os
import yaml
from rich.console import Console
from rich.table import Table
# Add the parent directory to the path so we can import Edison modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Import Edison's markdown_utils module
from edison.utils.markdown_utils import print_command_rich, format_command_rich
# Sample commands to demonstrate syntax highlighting
COMMANDS = {
"Simple": "ls -la /var/log",
"Moderate": "find /home -type f -name '*.txt' -size +1M | xargs grep -l 'ERROR' | sort",
"Complex": """#!/bin/bash
# This script processes log files
for file in $(find /var/log -name "*.log" -mtime -1); do
echo "Processing $file..."
if grep -q "ERROR" "$file"; then
echo "Errors found in $file"
grep "ERROR" "$file" | awk '{print $1, $2, $NF}' > "/tmp/$(basename $file).errors"
fi
done"""
}
def load_edison_config():
"""Load Edison's configuration file."""
config_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"edison", "edison.yaml")
try:
with open(config_path, 'r') as file:
return yaml.safe_load(file)
except Exception as e:
print(f"Error loading Edison configuration: {e}")
return {"ui": {"theme": "monokai"}}
def main():
"""Main function to demonstrate Edison's theme functionality."""
console = Console()
# List of themes to demonstrate
themes = [
"monokai",
"github-dark",
"solarized-dark",
"dracula",
"nord",
"gruvbox-dark",
]
# Print header
console.print("\n[bold cyan]Edison Theme Integration Demonstration[/bold cyan]")
console.print("This shows how Edison's theme functionality works with its markdown_utils module.\n")
# Load Edison's configuration
edison_config = load_edison_config()
current_theme = edison_config.get("ui", {}).get("theme", "monokai")
console.print(f"[bold]Current Edison theme:[/bold] [green]{current_theme}[/green]\n")
# Display theme usage examples
console.print("[bold]1. How Edison uses themes in the code:[/bold]")
console.print("""
# In edison/utils/markdown_utils.py:
def print_command_rich(command, shell="bash", theme="monokai"):
console = Console()
syntax = Syntax(command, shell, theme=theme, word_wrap=True)
panel = Panel(syntax, title="Command", border_style="blue")
console.print(panel)
# In edison/ui/console.py:
def print_command(command, config=None):
if config and config.get("ui", {}).get("rich_formatting", True):
command_style = config.get("ui", {}).get("command_style", "panel")
theme = config.get("ui", {}).get("theme", "monokai")
shell = config.get("shell", "bash")
if command_style == "panel":
from edison.utils.markdown_utils import print_command_rich
print_command_rich(command, shell=shell, theme=theme)
""")
console.print("\n[bold]2. Theme comparison with different commands:[/bold]\n")
# For each complexity level, show the command with different themes
for complexity, command in COMMANDS.items():
console.print(f"[bold yellow]Command Complexity: {complexity}[/bold yellow]")
console.print(f"[dim]{command}[/dim]\n")
for theme in themes:
console.print(f"[bold]Theme: [green]{theme}[/green][/bold]")
print_command_rich(command, theme=theme)
console.print()
if complexity != list(COMMANDS.keys())[-1]:
console.print("[italic]Press Enter to see the next command complexity...[/italic]")
input()
# Show how to use Edison's theme functionality in custom code
console.print("\n[bold]3. How to use Edison's theme functionality in your own code:[/bold]")
console.print("""
# Import Edison's markdown_utils module
from edison.utils.markdown_utils import print_command_rich
# Print a command with a specific theme
print_command_rich("ls -la", theme="github-dark")
# Or get the formatted string to use elsewhere
from edison.utils.markdown_utils import format_command_rich
formatted = format_command_rich("grep -r 'pattern' .", theme="nord")
print(formatted)
""")
if __name__ == "__main__":
main()