""" Side-by-side split screen diff viewer and summary diff formatting using Rich. """ import difflib from pathlib import Path from typing import Any, Dict, List, Optional, Tuple from rich.columns import Columns from rich.panel import Panel from rich.syntax import Syntax from rich.table import Table from rich.text import Text from timelord.ui.theme import console def render_diff_summary(changes: Dict[str, Any]) -> Table: """Renders a summary table of changed files between Prime and Universe.""" table = Table( title="[bold bright_cyan]Temporal Divergence Summary[/bold bright_cyan]", border_style="cyan", header_style="bold magenta", ) table.add_column("Status", style="bold", width=12) table.add_column("File Path", style="bright_white") table.add_column("Lines (+ / -)", justify="right", style="dim") for f in changes.get("added", []): table.add_row("[green]+ ADDED[/green]", str(f), "[green]+new file[/green]") for f in changes.get("deleted", []): table.add_row("[red]- REMOVED[/red]", str(f), "[red]-deleted[/red]") for item in changes.get("modified", []): path = item.get("path", "") plus = item.get("additions", 0) minus = item.get("deletions", 0) table.add_row( "[yellow]~ MODIFIED[/yellow]", str(path), f"[green]+{plus}[/green] / [red]-{minus}[/red]", ) return table def render_side_by_side_diff( file_rel_path: str, content_a: str, content_b: str, label_a: str = "Prime (Earth-616)", label_b: str = "Parallel Universe", max_lines: int = 100, ) -> None: """Renders a side-by-side visual diff in two terminal panels.""" lines_a = content_a.splitlines() lines_b = content_b.splitlines() # Determine syntax lexer by file extension ext = Path(file_rel_path).suffix.lstrip(".") lexer_map = { "py": "python", "js": "javascript", "ts": "typescript", "json": "json", "html": "html", "css": "css", "md": "markdown", "sh": "bash", "yml": "yaml", "yaml": "yaml", } lexer = lexer_map.get(ext, "text") # Generate side-by-side aligned diff lines matcher = difflib.SequenceMatcher(None, lines_a, lines_b) left_lines = [] right_lines = [] for tag, i1, i2, j1, j2 in matcher.get_opcodes(): if tag == "equal": for line in lines_a[i1:i2]: left_lines.append(f" {line}") right_lines.append(f" {line}") elif tag == "replace": chunk_a = lines_a[i1:i2] chunk_b = lines_b[j1:j2] max_len = max(len(chunk_a), len(chunk_b)) for i in range(max_len): la = chunk_a[i] if i < len(chunk_a) else "" lb = chunk_b[i] if i < len(chunk_b) else "" left_lines.append(f"- {la}" if la else " ") right_lines.append(f"+ {lb}" if lb else " ") elif tag == "delete": for line in lines_a[i1:i2]: left_lines.append(f"- {line}") right_lines.append(" ") elif tag == "insert": for line in lines_b[j1:j2]: left_lines.append(" ") right_lines.append(f"+ {line}") # Clip to max_lines if too long is_truncated = len(left_lines) > max_lines if is_truncated: left_lines = left_lines[:max_lines] right_lines = right_lines[:max_lines] left_text = Text() for l in left_lines: if l.startswith("-"): left_text.append(l + "\n", style="bold red") else: left_text.append(l + "\n", style="dim white") right_text = Text() for l in right_lines: if l.startswith("+"): right_text.append(l + "\n", style="bold green") else: right_text.append(l + "\n", style="dim white") if is_truncated: left_text.append(f"\n... truncated to {max_lines} lines ...", style="dim yellow") right_text.append(f"\n... truncated to {max_lines} lines ...", style="dim yellow") panel_a = Panel( left_text, title=f"[bold cyan]{label_a}[/bold cyan] ({file_rel_path})", border_style="cyan", expand=True, ) panel_b = Panel( right_text, title=f"[bold magenta]{label_b}[/bold magenta] ({file_rel_path})", border_style="magenta", expand=True, ) console.print(Columns([panel_a, panel_b], expand=True))