"""Main TUI display components for AutoRestTest.""" import sys from typing import Any, Dict, List, Optional from rich.align import Align from rich.box import DOUBLE, HEAVY, ROUNDED from rich.columns import Columns from rich.console import Console, Group from rich.layout import Layout from rich.panel import Panel from rich.progress import ( BarColumn, MofNCompleteColumn, Progress, SpinnerColumn, TaskProgressColumn, TextColumn, TimeElapsedColumn, TimeRemainingColumn, ) from rich.style import Style from rich.table import Table from rich.text import Text from .themes import DEFAULT_THEME, TUITheme class TUIDisplay: """Main TUI display handler for AutoRestTest.""" def __init__(self, theme: TUITheme = DEFAULT_THEME, width: int = 80): self.console = Console(force_terminal=True, width=width) self.theme = theme self.width = width def clear(self): """Clear the terminal screen.""" self.console.clear() def print_banner(self): """Display the AutoRestTest banner.""" banner_text = """ ___ __ ___ __ ______ __ / _ |__ __/ /____ / _ \\___ ___/ /_/_ __/__ ___ / /_ / __ / // / __/ _ \\/ , _/ -_|_-< __/ / / / -_|_-= 70 else self.theme.warning if success_pct >= 40 else self.theme.error ) summary_table.add_row("API Title", title) summary_table.add_row("Duration", f"{duration}s ({duration // 60}m {duration % 60}s)") summary_table.add_row("Total Requests", f"{total_requests:,}") summary_table.add_row("Total Operations", str(total_operations)) summary_table.add_row( "Successful Operations", f"[{success_color}]{successful_operations} ({success_pct:.1f}%)[/{success_color}]", ) summary_table.add_row( "Unique Server Errors", f"[{self.theme.error if unique_errors > 0 else self.theme.success}]{unique_errors}[/]", ) # Combine into final panel panel = Panel( Group( Align.center(report_title), Text(), Columns([summary_table, status_table], expand=True, equal=True), ), box=DOUBLE, border_style=self.theme.primary, padding=(1, 2), ) self.console.print() self.console.print(panel) def print_error(self, message: str, details: Optional[str] = None): """Display an error message.""" error_text = Text() error_text.append(self.theme.symbol_error + " ", style=self.theme.symbol_error_color) error_text.append(message, style=f"bold {self.theme.error}") content = error_text if details: detail_text = Text(f"\n{details}", style=self.theme.text_dim) content = Group(error_text, detail_text) panel = Panel( content, box=ROUNDED, border_style=self.theme.error, title="Error", title_align="left", ) self.console.print(panel) def print_warning(self, message: str): """Display a warning message.""" warning_text = Text() warning_text.append(self.theme.symbol_warning + " ", style=self.theme.symbol_warning_color) warning_text.append(message, style=self.theme.warning) self.console.print(warning_text) def print_success(self, message: str): """Display a success message.""" success_text = Text() success_text.append(self.theme.symbol_success + " ", style=self.theme.symbol_success_color) success_text.append(message, style=self.theme.success) self.console.print(success_text) def create_progress(self) -> Progress: """Create a styled progress bar context manager.""" return Progress( SpinnerColumn(style=self.theme.primary), TextColumn("[progress.description]{task.description}"), BarColumn( complete_style=self.theme.progress_complete, finished_style=self.theme.success, ), TaskProgressColumn(), TimeElapsedColumn(), TimeRemainingColumn(), console=self.console, transient=False, ) def confirm(self, message: str, default: bool = True) -> bool: """Display a confirmation prompt.""" default_str = "Y/n" if default else "y/N" self.console.print(f"\n[{self.theme.symbol_info_color}]{self.theme.symbol_info}[/{self.theme.symbol_info_color}] {message} [{default_str}]: ", end="") try: response = input().strip().lower() if not response: return default return response in ("y", "yes", "true", "1") except (EOFError, KeyboardInterrupt): self.console.print() return default def wait_for_key(self, message: str = "Press Enter to continue..."): """Wait for user to press Enter.""" self.console.print(f"\n[dim]{message}[/dim]", end="") try: input() except (EOFError, KeyboardInterrupt): pass