Files
TinyTorch/tinytorch/cli/core/console.py
Vijay Janapa Reddi 0cbcbf8a6e Refactor CLI to senior software engineer standards
BREAKING CHANGE: Major architectural refactoring of CLI system

New Professional Architecture:
- Clean separation of concerns with proper package structure
- Command pattern implementation with base classes
- Centralized configuration management
- Proper exception hierarchy and error handling
- Logging framework integration
- Type hints throughout
- Dependency injection pattern

Structure:
tinytorch/cli/
├── __init__.py              # Package initialization
├── main.py                  # Professional CLI entry point
├── core/                    # Core CLI functionality
│   ├── __init__.py
│   ├── config.py           # Configuration management
│   ├── console.py          # Centralized console output
│   └── exceptions.py       # Exception hierarchy
├── commands/               # Command implementations
│   ├── __init__.py
│   ├── base.py            # Base command class
│   └── notebooks.py       # Notebooks command
└── tools/                 # CLI tools
    ├── __init__.py
    └── py_to_notebook.py  # Conversion tool

Features Added:
- Proper entry points in pyproject.toml
- Professional logging with file output
- Environment validation with detailed error messages
- Dry-run mode for notebooks command
- Force rebuild option
- Timeout protection for subprocess calls
- Backward compatibility wrapper (bin/tito)
- Extensible command registration system

Benefits:
- Maintainable: Single responsibility per module
- Testable: Clean interfaces and dependency injection
- Extensible: Easy to add new commands
- Professional: Industry-standard patterns
- Robust: Proper error handling and validation
- Installable: Proper package structure with entry points
2025-07-10 22:05:10 -04:00

48 lines
1.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Console management for consistent CLI output.
"""
from rich.console import Console
from rich.panel import Panel
from rich.text import Text
from rich.table import Table
from rich.tree import Tree
from rich.progress import Progress, SpinnerColumn, BarColumn, TextColumn
from typing import Optional
import sys
# Global console instance
_console: Optional[Console] = None
def get_console() -> Console:
"""Get the global console instance."""
global _console
if _console is None:
_console = Console(stderr=False)
return _console
def print_banner():
"""Print the TinyTorch banner using Rich."""
console = get_console()
banner_text = Text("Tiny🔥Torch: Build ML Systems from Scratch", style="bold red")
console.print(Panel(banner_text, style="bright_blue", padding=(1, 2)))
def print_error(message: str, title: str = "Error"):
"""Print an error message with consistent formatting."""
console = get_console()
console.print(Panel(f"[red]❌ {message}[/red]", title=title, border_style="red"))
def print_success(message: str, title: str = "Success"):
"""Print a success message with consistent formatting."""
console = get_console()
console.print(Panel(f"[green]✅ {message}[/green]", title=title, border_style="green"))
def print_warning(message: str, title: str = "Warning"):
"""Print a warning message with consistent formatting."""
console = get_console()
console.print(Panel(f"[yellow]⚠️ {message}[/yellow]", title=title, border_style="yellow"))
def print_info(message: str, title: str = "Info"):
"""Print an info message with consistent formatting."""
console = get_console()
console.print(Panel(f"[cyan] {message}[/cyan]", title=title, border_style="cyan"))