summaryrefslogtreecommitdiff
path: root/timelord/ui/views.py
blob: 7e76aa169d9aeada36b47454001d897a890c38e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
Rich visual presentations: Status dashboard, Multiverse branching trees, and tables.
"""

from pathlib import Path
from typing import Any, Dict, List, Optional

from rich.panel import Panel
from rich.table import Table
from rich.tree import Tree
from rich.text import Text

from timelord.ui.theme import console


def render_status_dashboard(
    current_timeline: str,
    project_root: Path,
    last_snapshot: Optional[Dict[str, Any]],
    git_state: Dict[str, Any],
    active_universes: List[Dict[str, Any]],
    ports: List[int],
) -> None:
    """Renders the comprehensive temporal coordinate status dashboard."""
    is_prime = current_timeline.lower() in ("prime", "earth-616")

    status_table = Table.grid(padding=(0, 2))
    status_table.add_column(style="bold cyan", justify="right")
    status_table.add_column(style="white")

    # Current Timeline
    timeline_str = (
        "[bold bright_green]Prime Timeline (Earth-616)[/bold bright_green]"
        if is_prime
        else f"[bold magenta]🌌 Parallel Universe: {current_timeline}[/bold magenta]"
    )
    status_table.add_row("Temporal Realm:", timeline_str)
    status_table.add_row("Project Root:", f"[dim]{project_root}[/dim]")

    # Git state
    if git_state.get("is_git"):
        branch = git_state.get("branch", "detached")
        sha = git_state.get("head_sha", "unknown")
        dirty_flag = (
            "[bold yellow]● DIRTY (uncommitted changes)[/bold yellow]"
            if git_state.get("is_dirty")
            else "[green]✔ CLEAN[/green]"
        )
        status_table.add_row(
            "Git Continuum:",
            f"branch [cyan]{branch}[/cyan] ({sha}) {dirty_flag}",
        )
    else:
        status_table.add_row("Git Continuum:", "[dim]Non-git workspace[/dim]")

    # Configured ports
    if ports:
        ports_str = ", ".join(f"[bold yellow]{p}[/bold yellow]" for p in ports)
        status_table.add_row("Bound Ports:", ports_str)

    # Last Snapshot Anchor
    if last_snapshot:
        snap_id = last_snapshot.get("id")
        snap_time = last_snapshot.get("timestamp", "unknown")
        snap_label = last_snapshot.get("label", "no description")
        status_table.add_row(
            "Last Anchor:",
            f"[bold bright_yellow]{snap_id}[/bold bright_yellow] [dim]({snap_time})[/dim] - \"{snap_label}\"",
        )
    else:
        status_table.add_row("Last Anchor:", "[dim]No snapshots recorded yet[/dim]")

    # Active Parallel Universes
    if active_universes:
        univ_names = [f"[magenta]🌌 {u.get('name')}[/magenta]" for u in active_universes]
        status_table.add_row("Active Universes:", ", ".join(univ_names))
    else:
        status_table.add_row("Parallel Universes:", "[dim]No active parallel timelines[/dim]")

    panel = Panel(
        status_table,
        title="[bold bright_cyan]⚡ TEMPORAL COORDINATES ⚡[/bold bright_cyan]",
        subtitle="[dim]TIMELORD Chrono Engine[/dim]",
        border_style="cyan",
        padding=(1, 2),
    )
    console.print(panel)


def render_multiverse_tree(
    snapshots: List[Dict[str, Any]],
    universes: List[Dict[str, Any]],
    current_timeline: str = "Prime",
) -> None:
    """Renders the branching timeline tree showing Prime continuum and parallel universes."""
    root_label = Text()
    root_label.append("⚡ Prime Continuum (Earth-616)", style="bold bright_cyan")
    if current_timeline.lower() in ("prime", "earth-616"):
        root_label.append(" [YOU ARE HERE]", style="bold bright_green")

    tree = Tree(root_label)

    # Map universes by parent snapshot id
    universes_by_parent: Dict[str, List[Dict[str, Any]]] = {}
    for u in universes:
        parent = u.get("parent_snapshot_id", "root")
        universes_by_parent.setdefault(parent, []).append(u)

    if not snapshots:
        tree.add("[dim]No frozen snapshots recorded yet. Use `tl freeze` to anchor time.[/dim]")
    else:
        for snap in snapshots:
            snap_id = snap.get("id", "")
            snap_time = snap.get("timestamp", "")
            label = snap.get("label", "")
            git_info = ""
            if snap.get("git", {}).get("is_git"):
                git_info = f" [cyan]({snap['git']['branch']}@{snap['git']['head_sha']})[/cyan]"

            node_text = Text()
            node_text.append(f"● {snap_id}", style="bold yellow")
            node_text.append(f" [{snap_time}]", style="dim")
            node_text.append(f" \"{label}\"", style="white")
            if git_info:
                node_text.append(git_info)

            snap_node = tree.add(node_text)

            # Check if any universe branched from this snapshot
            branched_universes = universes_by_parent.get(snap_id, [])
            for u in branched_universes:
                uname = u.get("name", "")
                status = u.get("status", "active")
                offset = u.get("port_offset")
                is_current = current_timeline == uname

                u_text = Text()
                u_text.append(f"├─── 🌌 Universe: '{uname}'", style="bold magenta")
                if is_current:
                    u_text.append(" [YOU ARE HERE]", style="bold bright_green")
                if status == "pruned":
                    u_text.append(" (Pruned)", style="dim red")
                elif status == "merged":
                    u_text.append(" (Merged to Prime)", style="bold bright_blue")
                else:
                    u_text.append(" (Active)", style="bold bright_green")

                if offset:
                    u_text.append(f" [Port offset +{offset}]", style="yellow")

                snap_node.add(u_text)

    # Universes without snapshot parent
    orphans = universes_by_parent.get("root", [])
    for u in orphans:
        uname = u.get("name", "")
        tree.add(f"├─── 🌌 Universe: '{uname}' (Spawned from unrecorded timeline)", style="bold magenta")

    console.print(tree)


def render_snapshots_table(snapshots: List[Dict[str, Any]]) -> None:
    """Renders tabular list of snapshots."""
    table = Table(
        title="[bold yellow]Temporal Anchors (Snapshots)[/bold yellow]",
        border_style="yellow",
        header_style="bold bright_yellow",
    )
    table.add_column("Chrono ID", style="bold yellow", width=14)
    table.add_column("Timestamp", style="dim", width=20)
    table.add_column("Git Coordinates", style="cyan")
    table.add_column("Description / Label", style="white")
    table.add_column("Files", justify="right", style="dim", width=8)

    for s in snapshots:
        snap_id = s.get("id", "")
        ts = s.get("timestamp", "")[:19].replace("T", " ")
        label = s.get("label", "")
        git_str = "-"
        if s.get("git", {}).get("is_git"):
            git_str = f"{s['git'].get('branch', '')} ({s['git'].get('head_sha', '')})"
        files_count = str(s.get("stats", {}).get("total_files", "-"))
        table.add_row(snap_id, ts, git_str, label, files_count)

    console.print(table)


def render_universes_table(universes: List[Dict[str, Any]], current: str = "Prime") -> None:
    """Renders tabular list of parallel universes."""
    table = Table(
        title="[bold magenta]Multiverse Registry (Parallel Universes)[/bold magenta]",
        border_style="magenta",
        header_style="bold bright_magenta",
    )
    table.add_column("Universe Name", style="bold magenta")
    table.add_column("Status", width=12)
    table.add_column("Branched From", style="yellow")
    table.add_column("Created At", style="dim")
    table.add_column("Port Offset", justify="right", style="yellow")
    table.add_column("Active?", justify="center")

    for u in universes:
        name = u.get("name", "")
        status = u.get("status", "active")
        parent = u.get("parent_snapshot_id", "prime")
        ts = u.get("created_at", "")[:19].replace("T", " ")
        offset = f"+{u.get('port_offset')}" if u.get("port_offset") else "-"
        is_here = "⭐ CURRENT" if name == current else ""

        status_fmt = (
            "[bold green]Active[/bold green]"
            if status == "active"
            else "[dim red]Pruned[/dim red]"
            if status == "pruned"
            else "[blue]Merged[/blue]"
        )

        table.add_row(name, status_fmt, parent, ts, offset, is_here)

    console.print(table)