|
| 1 | +"""Snapshot and recording functionality for tmux panes.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import dataclasses |
| 6 | +import datetime |
| 7 | +import typing as t |
| 8 | + |
| 9 | +from typing_extensions import Self |
| 10 | + |
| 11 | +from libtmux.formats import PANE_FORMATS |
| 12 | + |
| 13 | +if t.TYPE_CHECKING: |
| 14 | + from collections.abc import Iterator, Sequence |
| 15 | + |
| 16 | + from libtmux.pane import Pane |
| 17 | + |
| 18 | + |
| 19 | +@dataclasses.dataclass(frozen=True) |
| 20 | +class PaneSnapshot: |
| 21 | + """A frozen snapshot of a pane's state at a point in time. |
| 22 | +
|
| 23 | + This class captures both the content and metadata of a tmux pane, |
| 24 | + making it suitable for testing and debugging purposes. |
| 25 | +
|
| 26 | + Attributes |
| 27 | + ---------- |
| 28 | + content : list[str] |
| 29 | + The captured content of the pane |
| 30 | + timestamp : datetime.datetime |
| 31 | + When the snapshot was taken (in UTC) |
| 32 | + pane_id : str |
| 33 | + The ID of the pane |
| 34 | + window_id : str |
| 35 | + The ID of the window containing the pane |
| 36 | + session_id : str |
| 37 | + The ID of the session containing the window |
| 38 | + server_name : str |
| 39 | + The name of the tmux server |
| 40 | + metadata : dict[str, str] |
| 41 | + Additional pane metadata from tmux formats |
| 42 | + """ |
| 43 | + |
| 44 | + content: list[str] |
| 45 | + timestamp: datetime.datetime |
| 46 | + pane_id: str |
| 47 | + window_id: str |
| 48 | + session_id: str |
| 49 | + server_name: str |
| 50 | + metadata: dict[str, str] |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def from_pane( |
| 54 | + cls, |
| 55 | + pane: Pane, |
| 56 | + start: t.Literal["-"] | int | None = None, |
| 57 | + end: t.Literal["-"] | int | None = None, |
| 58 | + ) -> Self: |
| 59 | + """Create a snapshot from a pane. |
| 60 | +
|
| 61 | + Parameters |
| 62 | + ---------- |
| 63 | + pane : Pane |
| 64 | + The pane to snapshot |
| 65 | + start : int | "-" | None |
| 66 | + Start line for capture_pane |
| 67 | + end : int | "-" | None |
| 68 | + End line for capture_pane |
| 69 | +
|
| 70 | + Returns |
| 71 | + ------- |
| 72 | + PaneSnapshot |
| 73 | + A frozen snapshot of the pane's state |
| 74 | + """ |
| 75 | + metadata = { |
| 76 | + fmt: getattr(pane, fmt) |
| 77 | + for fmt in PANE_FORMATS |
| 78 | + if hasattr(pane, fmt) and getattr(pane, fmt) is not None |
| 79 | + } |
| 80 | + |
| 81 | + content = pane.capture_pane(start=start, end=end) |
| 82 | + if isinstance(content, str): |
| 83 | + content = [content] |
| 84 | + |
| 85 | + return cls( |
| 86 | + content=content, |
| 87 | + timestamp=datetime.datetime.now(datetime.timezone.utc), |
| 88 | + pane_id=str(pane.pane_id), |
| 89 | + window_id=str(pane.window.window_id), |
| 90 | + session_id=str(pane.session.session_id), |
| 91 | + server_name=str(pane.server.socket_name), |
| 92 | + metadata=metadata, |
| 93 | + ) |
| 94 | + |
| 95 | + def __str__(self) -> str: |
| 96 | + """Return a string representation of the snapshot. |
| 97 | +
|
| 98 | + Returns |
| 99 | + ------- |
| 100 | + str |
| 101 | + A formatted string showing the snapshot content and metadata |
| 102 | + """ |
| 103 | + return ( |
| 104 | + f"PaneSnapshot(pane={self.pane_id}, window={self.window_id}, " |
| 105 | + f"session={self.session_id}, server={self.server_name}, " |
| 106 | + f"timestamp={self.timestamp.isoformat()}, " |
| 107 | + f"content=\n{self.content_str})" |
| 108 | + ) |
| 109 | + |
| 110 | + @property |
| 111 | + def content_str(self) -> str: |
| 112 | + """Get the pane content as a single string. |
| 113 | +
|
| 114 | + Returns |
| 115 | + ------- |
| 116 | + str |
| 117 | + The pane content with lines joined by newlines |
| 118 | + """ |
| 119 | + return "\n".join(self.content) |
| 120 | + |
| 121 | + |
| 122 | +@dataclasses.dataclass |
| 123 | +class PaneRecording: |
| 124 | + """A time-series recording of pane snapshots. |
| 125 | +
|
| 126 | + This class maintains an ordered sequence of pane snapshots, |
| 127 | + allowing for analysis of how a pane's content changes over time. |
| 128 | +
|
| 129 | + Attributes |
| 130 | + ---------- |
| 131 | + snapshots : list[PaneSnapshot] |
| 132 | + The sequence of snapshots in chronological order |
| 133 | + """ |
| 134 | + |
| 135 | + snapshots: list[PaneSnapshot] = dataclasses.field(default_factory=list) |
| 136 | + |
| 137 | + def add_snapshot( |
| 138 | + self, |
| 139 | + pane: Pane, |
| 140 | + start: t.Literal["-"] | int | None = None, |
| 141 | + end: t.Literal["-"] | int | None = None, |
| 142 | + ) -> None: |
| 143 | + """Add a new snapshot to the recording. |
| 144 | +
|
| 145 | + Parameters |
| 146 | + ---------- |
| 147 | + pane : Pane |
| 148 | + The pane to snapshot |
| 149 | + start : int | "-" | None |
| 150 | + Start line for capture_pane |
| 151 | + end : int | "-" | None |
| 152 | + End line for capture_pane |
| 153 | + """ |
| 154 | + self.snapshots.append(PaneSnapshot.from_pane(pane, start=start, end=end)) |
| 155 | + |
| 156 | + def __len__(self) -> int: |
| 157 | + """Get the number of snapshots in the recording. |
| 158 | +
|
| 159 | + Returns |
| 160 | + ------- |
| 161 | + int |
| 162 | + The number of snapshots |
| 163 | + """ |
| 164 | + return len(self.snapshots) |
| 165 | + |
| 166 | + def __iter__(self) -> Iterator[PaneSnapshot]: |
| 167 | + """Iterate through snapshots in chronological order. |
| 168 | +
|
| 169 | + Returns |
| 170 | + ------- |
| 171 | + Iterator[PaneSnapshot] |
| 172 | + Iterator over the snapshots |
| 173 | + """ |
| 174 | + return iter(self.snapshots) |
| 175 | + |
| 176 | + def __getitem__(self, index: int) -> PaneSnapshot: |
| 177 | + """Get a snapshot by index. |
| 178 | +
|
| 179 | + Parameters |
| 180 | + ---------- |
| 181 | + index : int |
| 182 | + The index of the snapshot to retrieve |
| 183 | +
|
| 184 | + Returns |
| 185 | + ------- |
| 186 | + PaneSnapshot |
| 187 | + The snapshot at the specified index |
| 188 | + """ |
| 189 | + return self.snapshots[index] |
| 190 | + |
| 191 | + @property |
| 192 | + def latest(self) -> PaneSnapshot | None: |
| 193 | + """Get the most recent snapshot. |
| 194 | +
|
| 195 | + Returns |
| 196 | + ------- |
| 197 | + PaneSnapshot | None |
| 198 | + The most recent snapshot, or None if no snapshots exist |
| 199 | + """ |
| 200 | + return self.snapshots[-1] if self.snapshots else None |
| 201 | + |
| 202 | + def get_snapshots_between( |
| 203 | + self, |
| 204 | + start_time: datetime.datetime, |
| 205 | + end_time: datetime.datetime, |
| 206 | + ) -> Sequence[PaneSnapshot]: |
| 207 | + """Get snapshots between two points in time. |
| 208 | +
|
| 209 | + Parameters |
| 210 | + ---------- |
| 211 | + start_time : datetime.datetime |
| 212 | + The start of the time range |
| 213 | + end_time : datetime.datetime |
| 214 | + The end of the time range |
| 215 | +
|
| 216 | + Returns |
| 217 | + ------- |
| 218 | + Sequence[PaneSnapshot] |
| 219 | + Snapshots within the specified time range |
| 220 | + """ |
| 221 | + return [ |
| 222 | + snapshot |
| 223 | + for snapshot in self.snapshots |
| 224 | + if start_time <= snapshot.timestamp <= end_time |
| 225 | + ] |
0 commit comments