git-vidi 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1268 @@
1
+ """Commit detail modal screen."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from functools import partial
6
+ from typing import Callable
7
+
8
+ from rich.style import Style
9
+ from rich.table import Table
10
+ from rich.text import Text
11
+ from textual import events
12
+ from textual.app import ComposeResult
13
+ from textual.binding import Binding
14
+ from textual.containers import Horizontal, ScrollableContainer, Vertical, VerticalScroll
15
+ from textual.screen import ModalScreen
16
+ from textual.widgets import DataTable, Footer, Header, Input, Static
17
+
18
+ from ..data import compact_label_for_change, section_for_change
19
+ from ..formatting import build_repo_stats_sub_title
20
+ from ..models import CommitFileChange, CommitPatchSection, RepoContext, RepoScanSnapshot
21
+ from ..repository import RepoService
22
+ from ..text_view import Line, TextView
23
+ from ..theme import make_change_stat_style, make_style, resolve_theme_palette
24
+ from ..utils import rel_time, utc_now
25
+ from .commit_files import CommitFilesScreen
26
+ from .shared import (
27
+ BACK_GROUP,
28
+ COMMIT_GROUP,
29
+ JUMP_GROUP,
30
+ MOVE_GROUP,
31
+ PAGE_GROUP,
32
+ SEARCH_GROUP,
33
+ DebouncedResize,
34
+ )
35
+
36
+
37
+ class CommitDetailScreen(DebouncedResize, ModalScreen[None]):
38
+ """Full-screen modal for a single commit."""
39
+
40
+ PANE_GROUP = Binding.Group("Pane")
41
+ FILE_TABLES = ("commit-detail-files-diff", "commit-detail-files-outline")
42
+
43
+ CSS = """
44
+ CommitDetailScreen {
45
+ background: $background;
46
+ }
47
+
48
+ #commit-detail-summary-bar {
49
+ padding: 0 1;
50
+ background: $surface;
51
+ border: solid $border;
52
+ }
53
+
54
+ #commit-detail-body {
55
+ layout: horizontal;
56
+ height: 1fr;
57
+ margin: 0 1 1 1;
58
+ }
59
+
60
+ .commit-detail-panel {
61
+ padding: 1 1;
62
+ background: $panel;
63
+ border: solid $border;
64
+ }
65
+
66
+ #commit-detail-overview-panel:focus-within,
67
+ #commit-detail-files-panel:focus-within,
68
+ #commit-detail-patch-panel:focus-within {
69
+ border: solid $primary;
70
+ }
71
+
72
+ .commit-detail-panel-title {
73
+ text-style: bold;
74
+ margin-bottom: 0;
75
+ }
76
+
77
+ #commit-detail-sidebar {
78
+ width: 36;
79
+ margin-right: 1;
80
+ height: 1fr;
81
+ }
82
+
83
+ #commit-detail-overview-panel {
84
+ height: 1fr;
85
+ max-height: 12;
86
+ min-height: 5;
87
+ margin-bottom: 1;
88
+ }
89
+
90
+ #commit-detail-overview-scroll {
91
+ height: 1fr;
92
+ overflow-y: auto;
93
+ scrollbar-gutter: stable;
94
+ }
95
+
96
+ #commit-detail-files-panel {
97
+ height: 1fr;
98
+ min-height: 6;
99
+ }
100
+
101
+ #commit-detail-files-title {
102
+ margin-bottom: 0;
103
+ }
104
+
105
+ #commit-detail-files-meta {
106
+ margin-bottom: 0;
107
+ height: 1;
108
+ }
109
+
110
+ #commit-detail-files-diff,
111
+ #commit-detail-files-outline {
112
+ height: 1fr;
113
+ min-height: 3;
114
+ }
115
+
116
+ #commit-detail-main {
117
+ width: 1fr;
118
+ height: 1fr;
119
+ }
120
+
121
+ #commit-detail-patch-panel {
122
+ height: 1fr;
123
+ }
124
+
125
+ #commit-detail-search,
126
+ #commit-detail-files-filter {
127
+ display: none;
128
+ margin-bottom: 1;
129
+ border: solid $border;
130
+ background: $surface;
131
+ color: $text;
132
+ }
133
+
134
+ #commit-detail-search:focus,
135
+ #commit-detail-files-filter:focus {
136
+ border: solid $primary;
137
+ }
138
+
139
+ #commit-detail-selected-scroll,
140
+ #commit-detail-full-scroll {
141
+ height: 1fr;
142
+ }
143
+
144
+ DataTable {
145
+ height: 1fr;
146
+ }
147
+ """
148
+
149
+ BINDINGS = [
150
+ Binding("down", "cursor_down", "Down", priority=True, group=MOVE_GROUP),
151
+ Binding("up", "cursor_up", "Up", priority=True, group=MOVE_GROUP),
152
+ Binding("j", "nav_down", "Down", priority=True, show=False),
153
+ Binding("k", "nav_up", "Up", priority=True, show=False),
154
+ Binding("tab", "focus_next_pane", "Next Pane", priority=True, group=PANE_GROUP),
155
+ Binding(
156
+ "shift+tab",
157
+ "focus_prev_pane",
158
+ "Prev Pane",
159
+ priority=True,
160
+ key_display="S-Tab",
161
+ group=PANE_GROUP,
162
+ ),
163
+ Binding("[", "prev_commit", "Prev Commit", group=COMMIT_GROUP),
164
+ Binding("]", "next_commit", "Next Commit", group=COMMIT_GROUP),
165
+ Binding("pageup", "page_up", "Page Up", priority=True, group=PAGE_GROUP),
166
+ Binding("pagedown", "page_down", "Page Down", priority=True, group=PAGE_GROUP),
167
+ Binding("home", "scroll_top", "Top", priority=True, group=JUMP_GROUP),
168
+ Binding("end", "scroll_bottom", "Bottom", priority=True, group=JUMP_GROUP),
169
+ Binding("p", "toggle_patch_scope", "Patch"),
170
+ Binding("f", "browse_files", "Files"),
171
+ Binding("slash", "open_search", "Search"),
172
+ # enter/shift+enter step patch matches while the search box is focused,
173
+ # and n/N once there are matches.
174
+ Binding("enter", "search_next", "Next Match", priority=True),
175
+ Binding("shift+enter", "search_prev", "Prev Match", priority=True),
176
+ Binding("n", "next_match", "Next Match", group=SEARCH_GROUP),
177
+ Binding("N", "prev_match", "Prev Match", group=SEARCH_GROUP),
178
+ Binding("escape", "close_screen", "Back", group=BACK_GROUP),
179
+ Binding("q", "close_screen", "Back", show=False, group=BACK_GROUP),
180
+ ]
181
+
182
+ DEFAULT_PANE = "files"
183
+
184
+ def __init__(
185
+ self,
186
+ service: RepoService,
187
+ commit_hash: str,
188
+ source_branch: str | None = None,
189
+ commit_hashes: list[str] | None = None,
190
+ show_full_patch: bool = False,
191
+ ) -> None:
192
+ super().__init__()
193
+ self.service = service
194
+ self.commit_hash = commit_hash
195
+ self.source_branch = source_branch
196
+ # Sibling commits, newest first, for stepping with [ / ].
197
+ self._commit_hashes = list(commit_hashes) if commit_hashes else [commit_hash]
198
+ if commit_hash in self._commit_hashes:
199
+ self._commit_index = self._commit_hashes.index(commit_hash)
200
+ else:
201
+ self._commit_hashes = [commit_hash]
202
+ self._commit_index = 0
203
+ self.detail = service.get_commit_detail(commit_hash)
204
+ self.show_full_patch = show_full_patch
205
+ self.active_pane = self.DEFAULT_PANE
206
+ self.selected_file_index = 0
207
+ self._suspend_file_events = False
208
+ self._patch_section_offsets: dict[CommitPatchSection, int] = {}
209
+ self._outline_offsets: list[tuple[int, int]] = []
210
+ self._ignore_next_patch_scroll_sync = False
211
+ self._reconcile_revision = 0
212
+ self._pending_focus_target: str | None = None
213
+ self._pending_focus_origin_id: str | None = None
214
+ self._pending_patch_scroll_to_selection = False
215
+ self._files_table_signatures: dict[str, tuple[int, int, int] | None] = dict.fromkeys(
216
+ self.FILE_TABLES
217
+ )
218
+ self._mode_transition_active = False
219
+ self._full_patch_ready = False
220
+ self._force_requested_focus = False
221
+ self._theme_palette = resolve_theme_palette(self.app.current_theme)
222
+ # in-patch search state
223
+ self._search_query = ""
224
+ # The objects each patch view was last built from.
225
+ self._patch_built_from: dict[str, tuple[object, ...]] = {}
226
+ # changed-files filter state, diff mode only
227
+ self._files_query = ""
228
+
229
+ def action_next_commit(self) -> None:
230
+ # Newer commits sit toward index 0 in the newest-first list.
231
+ self._go_to_commit(self._commit_index - 1)
232
+
233
+ def action_prev_commit(self) -> None:
234
+ self._go_to_commit(self._commit_index + 1)
235
+
236
+ def action_browse_files(self) -> None:
237
+ """Browse every file in this commit's tree, stepping commits with [ / ]."""
238
+ self.app.push_screen(
239
+ CommitFilesScreen(self.service, self._commit_hashes, self._commit_index)
240
+ )
241
+
242
+ def _go_to_commit(self, index: int) -> None:
243
+ if not 0 <= index < len(self._commit_hashes) or index == self._commit_index:
244
+ return
245
+ self._commit_index = index
246
+ self._full_patch_ready = False
247
+ self._show_commit(self._commit_hashes[index])
248
+ self._reset_detail_scroll()
249
+
250
+ def _show_commit(self, commit_hash: str) -> None:
251
+ self.commit_hash = commit_hash
252
+ self.detail = self.service.get_commit_detail(commit_hash)
253
+ self.selected_file_index = 0
254
+ self._forget_files_table_layouts()
255
+ self._ignore_next_patch_scroll_sync = False
256
+ self._render_detail_view(
257
+ focus_target="patch" if self.show_full_patch else self.DEFAULT_PANE,
258
+ scroll_to_selection=self.show_full_patch,
259
+ )
260
+
261
+ def _reset_detail_scroll(self) -> None:
262
+ self.query_one("#commit-detail-overview-scroll", VerticalScroll).scroll_home(
263
+ animate=False
264
+ )
265
+ self._selected_patch_scroll().scroll_home(animate=False)
266
+ self._full_patch_scroll().scroll_home(animate=False)
267
+
268
+ def compose(self) -> ComposeResult:
269
+ yield Header(show_clock=False)
270
+ with Static(id="commit-detail-summary-bar"):
271
+ yield Static(id="commit-detail-summary-content")
272
+ with Horizontal(id="commit-detail-body"):
273
+ with Vertical(id="commit-detail-sidebar"):
274
+ with Vertical(
275
+ id="commit-detail-overview-panel",
276
+ classes="commit-detail-panel",
277
+ ):
278
+ yield Static("Commit Overview", classes="commit-detail-panel-title")
279
+ with VerticalScroll(id="commit-detail-overview-scroll"):
280
+ yield Static(id="commit-detail-overview")
281
+ with Vertical(
282
+ id="commit-detail-files-panel",
283
+ classes="commit-detail-panel",
284
+ ):
285
+ yield Static(id="commit-detail-files-title", classes="commit-detail-panel-title")
286
+ yield Static(id="commit-detail-files-meta")
287
+ yield Input(placeholder="Filter files", id="commit-detail-files-filter")
288
+ yield DataTable(id="commit-detail-files-diff", show_row_labels=False)
289
+ yield DataTable(id="commit-detail-files-outline", show_row_labels=False)
290
+ with Vertical(id="commit-detail-main"):
291
+ with Vertical(
292
+ id="commit-detail-patch-panel",
293
+ classes="commit-detail-panel",
294
+ ):
295
+ yield Static(id="commit-detail-patch-title", classes="commit-detail-panel-title")
296
+ yield Input(placeholder="Search patch", id="commit-detail-search")
297
+ yield TextView(wrap=self.app.wrap_diffs, id="commit-detail-selected-scroll")
298
+ yield TextView(wrap=self.app.wrap_diffs, id="commit-detail-full-scroll")
299
+ yield Footer()
300
+
301
+ def on_mount(self) -> None:
302
+ self.watch(self.app, "theme", self._on_theme_change, init=False)
303
+ diff_table = self.query_one("#commit-detail-files-diff", DataTable)
304
+ outline_table = self.query_one("#commit-detail-files-outline", DataTable)
305
+ self.watch(self._full_patch_scroll(), "scroll_y", self._on_full_patch_scroll_y, init=False)
306
+ for table in (diff_table, outline_table):
307
+ table.cursor_type = "row"
308
+ table.show_horizontal_scrollbar = False
309
+ diff_table.add_column("File", width=18, key="file")
310
+ diff_table.add_column("+", width=4, key="adds")
311
+ diff_table.add_column("-", width=4, key="dels")
312
+ outline_table.add_column("File", width=18, key="file")
313
+ outline_table.add_column("+", width=4, key="adds")
314
+ outline_table.add_column("-", width=4, key="dels")
315
+ self.sub_title = self._build_repo_stats_sub_title()
316
+ self._apply_theme()
317
+ self._render_detail_view(
318
+ focus_target="patch" if self.show_full_patch else self.DEFAULT_PANE
319
+ )
320
+
321
+ def _on_theme_change(self, old_value: str, new_value: str) -> None:
322
+ self._apply_theme()
323
+ self._render_detail_view()
324
+
325
+ def _render_after_resize(self) -> None:
326
+ self._render_detail_view(refresh_patch_content=False)
327
+
328
+ def _focus_targets(self) -> tuple[ScrollableContainer | DataTable, ...]:
329
+ return tuple(self._pane_target(pane) for pane in self._available_panes())
330
+
331
+ def _selected_patch_scroll(self) -> TextView:
332
+ return self.query_one("#commit-detail-selected-scroll", TextView)
333
+
334
+ def _full_patch_scroll(self) -> TextView:
335
+ return self.query_one("#commit-detail-full-scroll", TextView)
336
+
337
+ def _active_patch_scroll(self) -> TextView:
338
+ return self._full_patch_scroll() if self.show_full_patch else self._selected_patch_scroll()
339
+
340
+ def _pane_target(self, pane: str) -> ScrollableContainer | DataTable:
341
+ targets: dict[str, ScrollableContainer | DataTable] = {
342
+ "overview": self.query_one("#commit-detail-overview-scroll", VerticalScroll),
343
+ "files": self._current_files_table(),
344
+ "patch": self._active_patch_scroll(),
345
+ }
346
+ return targets[pane]
347
+
348
+ def _available_panes(self) -> tuple[str, ...]:
349
+ # Fixed focus stops per mode, without the overview in full-patch mode.
350
+ if self.show_full_patch:
351
+ return ("files", "patch")
352
+ return ("overview", "files", "patch")
353
+
354
+ def _sync_active_pane_from_focus(self) -> None:
355
+ if self._mode_transition_active or self._pending_focus_target is not None:
356
+ return
357
+ focused_id = getattr(self.focused, "id", None)
358
+ mapping = {
359
+ "commit-detail-overview-scroll": "overview",
360
+ "commit-detail-files-diff": "files",
361
+ "commit-detail-files-outline": "files",
362
+ "commit-detail-selected-scroll": "patch",
363
+ "commit-detail-full-scroll": "patch",
364
+ }
365
+ pane = mapping.get(focused_id)
366
+ if pane in self._available_panes():
367
+ self.active_pane = pane
368
+
369
+ def _focus_active_pane(self) -> None:
370
+ panes = self._available_panes()
371
+ if not panes:
372
+ return
373
+ if self.active_pane not in panes:
374
+ self.active_pane = "patch" if self.show_full_patch and "patch" in panes else panes[0]
375
+ target = self._pane_target(self.active_pane)
376
+ if self.focused is not target:
377
+ target.focus()
378
+
379
+ def _refresh_scroll_focusability(self) -> None:
380
+ overview_scroll = self.query_one("#commit-detail-overview-scroll", VerticalScroll)
381
+ selected_patch_scroll = self._selected_patch_scroll()
382
+ full_patch_scroll = self._full_patch_scroll()
383
+
384
+ # Only the panes for the current mode are focus stops.
385
+ overview_scroll.can_focus = not self.show_full_patch
386
+ selected_patch_scroll.can_focus = not self.show_full_patch
387
+ full_patch_scroll.can_focus = self.show_full_patch
388
+ available = self._available_panes()
389
+ preferred = "patch" if self.show_full_patch else self.DEFAULT_PANE
390
+ pending = self._pending_focus_target
391
+ if pending == self.active_pane:
392
+ preferred = pending
393
+ if self.active_pane not in available:
394
+ if preferred == pending and pending is not None:
395
+ return
396
+ self.active_pane = preferred if preferred in available else available[0]
397
+
398
+ def _apply_theme(self) -> None:
399
+ # Everything but the muted title color comes from the theme via CSS.
400
+ self._theme_palette = resolve_theme_palette(self.app.current_theme)
401
+ for title in self.query(".commit-detail-panel-title"):
402
+ title.styles.color = self._theme_palette.muted
403
+
404
+ def on_descendant_focus(self, event: events.DescendantFocus) -> None:
405
+ self._sync_active_pane_from_focus()
406
+
407
+ def _apply_detail_layout(self) -> None:
408
+ sidebar = self.query_one("#commit-detail-sidebar", Vertical)
409
+ overview_panel = self.query_one("#commit-detail-overview-panel", Vertical)
410
+
411
+ if self.show_full_patch:
412
+ sidebar.styles.width = 28
413
+ overview_panel.styles.display = "none"
414
+ else:
415
+ sidebar.styles.width = 32
416
+ overview_panel.styles.display = "block"
417
+
418
+ def _ensure_valid_focus(self, *, prefer_patch: bool = False) -> None:
419
+ preferred = "patch" if prefer_patch else self.active_pane
420
+ available = self._available_panes()
421
+ if preferred in available:
422
+ self.active_pane = preferred
423
+ elif available:
424
+ self.active_pane = available[0]
425
+ self._focus_active_pane()
426
+
427
+ def _files_table_is_measured(self, table: DataTable | None = None) -> bool:
428
+ table = table or self._current_files_table()
429
+ return bool(table.size.width or table.content_region.width)
430
+
431
+ def _diff_files_table(self) -> DataTable:
432
+ return self.query_one("#commit-detail-files-diff", DataTable)
433
+
434
+ def _outline_files_table(self) -> DataTable:
435
+ return self.query_one("#commit-detail-files-outline", DataTable)
436
+
437
+ def _current_files_table(self) -> DataTable:
438
+ return self._outline_files_table() if self.show_full_patch else self._diff_files_table()
439
+
440
+ def _forget_files_table_layouts(self) -> None:
441
+ self._files_table_signatures = dict.fromkeys(self.FILE_TABLES)
442
+
443
+ def _refresh_files_table(self, *, force: bool = False) -> None:
444
+ table = self._current_files_table()
445
+ if not self._files_table_is_measured(table):
446
+ return
447
+ signature = self._files_table_layout(table)
448
+ table_id = table.id or ""
449
+ if (
450
+ not force
451
+ and signature == self._files_table_signatures.get(table_id)
452
+ and table.row_count == len(self._visible_file_indices())
453
+ ):
454
+ self._sync_outline_cursor(self.selected_file_index)
455
+ return
456
+ self._update_files_table()
457
+ self._files_table_signatures[table_id] = signature
458
+
459
+ def _schedule_reconcile(
460
+ self,
461
+ *,
462
+ focus_target: str | None = None,
463
+ scroll_to_selection: bool = False,
464
+ force_files_table_refresh: bool = False,
465
+ ) -> None:
466
+ if focus_target is not None:
467
+ self._pending_focus_target = focus_target
468
+ self._pending_focus_origin_id = getattr(self.focused, "id", None)
469
+ self._pending_patch_scroll_to_selection = (
470
+ self._pending_patch_scroll_to_selection or scroll_to_selection
471
+ )
472
+ if force_files_table_refresh:
473
+ self._forget_files_table_layouts()
474
+ self._reconcile_revision += 1
475
+ revision = self._reconcile_revision
476
+ self.call_after_refresh(partial(self._reconcile_after_refresh, revision))
477
+
478
+ def _reconcile_after_refresh(self, revision: int) -> None:
479
+ if not self.is_mounted or revision != self._reconcile_revision:
480
+ return
481
+
482
+ self._refresh_files_table()
483
+ self._refresh_scroll_focusability()
484
+
485
+ if self._pending_patch_scroll_to_selection and self.show_full_patch:
486
+ self._scroll_patch_to_selected()
487
+ self._pending_patch_scroll_to_selection = False
488
+
489
+ pending = self._pending_focus_target
490
+ requested_focus = self._pane_target(pending) if pending is not None else None
491
+ targets = self._focus_targets()
492
+ if (
493
+ requested_focus in targets
494
+ and self.focused is not requested_focus
495
+ and (
496
+ self._force_requested_focus
497
+ or self.focused not in targets
498
+ or getattr(self.focused, "id", None) == self._pending_focus_origin_id
499
+ )
500
+ ):
501
+ requested_pane = self._pending_focus_target
502
+ if requested_pane is not None:
503
+ self.active_pane = requested_pane
504
+ requested_focus.focus()
505
+ elif self.focused not in targets:
506
+ self._ensure_valid_focus(prefer_patch=self.show_full_patch)
507
+ else:
508
+ self._sync_active_pane_from_focus()
509
+
510
+ self._pending_focus_target = None
511
+ self._pending_focus_origin_id = None
512
+ self._mode_transition_active = False
513
+ self._force_requested_focus = False
514
+
515
+ def _render_detail_view(
516
+ self,
517
+ *,
518
+ focus_target: str | None = None,
519
+ scroll_to_selection: bool = False,
520
+ refresh_patch_content: bool = True,
521
+ ) -> None:
522
+ if self.detail.file_changes and not (
523
+ 0 <= self.selected_file_index < len(self.detail.file_changes)
524
+ ):
525
+ self.selected_file_index = 0
526
+ if focus_target is not None:
527
+ self.active_pane = focus_target
528
+ self._apply_detail_layout()
529
+ self._diff_files_table().styles.display = "none" if self.show_full_patch else "block"
530
+ self._outline_files_table().styles.display = "block" if self.show_full_patch else "none"
531
+ self._selected_patch_scroll().styles.display = "none" if self.show_full_patch else "block"
532
+ self._full_patch_scroll().styles.display = "block" if self.show_full_patch else "none"
533
+
534
+ palette = self._theme_palette
535
+ now = utc_now()
536
+
537
+ summary_bar = Text()
538
+ summary_bar.append("Commit ", style=make_style(palette.muted, bold=True))
539
+ summary_bar.append(
540
+ self.detail.commit_hash[:12],
541
+ style=make_style(palette.foreground, bold=True),
542
+ )
543
+ summary_bar.append(" | ", style=make_style(palette.muted))
544
+ summary_bar.append(self.detail.author_name, style=make_style(palette.foreground))
545
+ summary_bar.append(" | ", style=make_style(palette.muted))
546
+ summary_bar.append(
547
+ self.detail.authored_at.strftime("%Y-%m-%d %H:%M"),
548
+ style=make_style(palette.muted),
549
+ )
550
+ summary_bar.append(" | ", style=make_style(palette.muted))
551
+ summary_bar.append(
552
+ rel_time(self.detail.authored_at, now),
553
+ style=make_style(palette.muted),
554
+ )
555
+ self.query_one("#commit-detail-summary-content", Static).update(summary_bar)
556
+
557
+ overview = Text()
558
+ overview.append(
559
+ f"{self.detail.subject}\n",
560
+ style=make_style(palette.foreground, bold=True),
561
+ )
562
+ if self.detail.body:
563
+ overview.append(f"{self.detail.body}\n\n", style=make_style(palette.foreground))
564
+ overview.append("Author Email\n", style=make_style(palette.muted, bold=True))
565
+ overview.append(
566
+ f"{self.detail.author_email}\n",
567
+ style=make_style(palette.foreground),
568
+ )
569
+ if self.detail.parents:
570
+ overview.append("\nParents\n", style=make_style(palette.muted, bold=True))
571
+ overview.append(
572
+ ", ".join(parent[:7] for parent in self.detail.parents),
573
+ style=make_style(palette.foreground),
574
+ )
575
+ self.query_one("#commit-detail-overview", Static).update(overview)
576
+
577
+ current_table = self._current_files_table()
578
+ if not self._files_table_is_measured(current_table):
579
+ self._current_files_table().clear(columns=False)
580
+ table_id = current_table.id or ""
581
+ self._files_table_signatures[table_id] = None
582
+ else:
583
+ self._refresh_files_table(force=False)
584
+ files_title = (
585
+ "Patch Outline"
586
+ if self.show_full_patch
587
+ else "Files Changed"
588
+ )
589
+ self.query_one("#commit-detail-files-title", Static).update(files_title)
590
+ self._render_files_meta()
591
+
592
+ self._render_patch_panel(refresh_content=refresh_patch_content)
593
+ self._schedule_reconcile(
594
+ focus_target=focus_target,
595
+ scroll_to_selection=scroll_to_selection,
596
+ force_files_table_refresh=False,
597
+ )
598
+
599
+ def _render_files_meta(self) -> None:
600
+ palette = self._theme_palette
601
+ total = len(self.detail.file_changes)
602
+ total_additions = sum(change.additions or 0 for change in self.detail.file_changes)
603
+ total_deletions = sum(change.deletions or 0 for change in self.detail.file_changes)
604
+ file_word = "file" if total == 1 else "files"
605
+
606
+ files_meta = Text()
607
+ if self._files_query.strip() and not self.show_full_patch:
608
+ shown = len(self._visible_file_indices())
609
+ files_meta.append(
610
+ f"{shown} of {total} {file_word}",
611
+ style=make_style(palette.foreground, bold=True),
612
+ )
613
+ else:
614
+ files_meta.append(
615
+ f"{total} {file_word}",
616
+ style=make_style(palette.foreground, bold=True),
617
+ )
618
+ files_meta.append(" ", style=make_style(palette.muted))
619
+ files_meta.append(
620
+ f"+{total_additions}",
621
+ style=make_change_stat_style(palette, total_additions, positive=True),
622
+ )
623
+ files_meta.append(" ", style=make_style(palette.muted))
624
+ files_meta.append(
625
+ f"-{total_deletions}",
626
+ style=make_change_stat_style(palette, total_deletions, positive=False),
627
+ )
628
+ if self.show_full_patch:
629
+ files_meta.append(" ", style=make_style(palette.muted))
630
+ files_meta.append(
631
+ "Select to jump",
632
+ style=make_style(palette.muted),
633
+ )
634
+ self.query_one("#commit-detail-files-meta", Static).update(files_meta)
635
+
636
+ def _build_repo_stats_sub_title(self) -> str:
637
+ context = self.app.context
638
+ branch = self.source_branch or context.current_branch
639
+ ahead, behind = self.service.trunk_divergence(branch, context.branches)
640
+ return build_repo_stats_sub_title(
641
+ context, self.service.scanned_file_count(context), branch, ahead, behind
642
+ )
643
+
644
+ def _files_table_layout(self, table: DataTable | None = None) -> tuple[int, int, int]:
645
+ table = table or self._current_files_table()
646
+ width = (
647
+ table.scrollable_content_region.width
648
+ or table.content_region.width
649
+ or table.size.width
650
+ or 28
651
+ )
652
+ # Table chrome is 6 cells of column padding plus the 2-cell scrollbar gutter.
653
+ chrome = 8
654
+ stats_width = 4
655
+ file_width = max(8, width - (stats_width * 2) - chrome)
656
+ return file_width, stats_width, stats_width
657
+
658
+ def _apply_files_table_layout(self) -> int:
659
+ table = self._current_files_table()
660
+ file_width, adds_width, dels_width = self._files_table_layout(table)
661
+ table.columns["file"].width = file_width
662
+ if "adds" in table.columns:
663
+ table.columns["adds"].width = adds_width
664
+ if "dels" in table.columns:
665
+ table.columns["dels"].width = dels_width
666
+ return file_width
667
+
668
+ def _file_index_for_section(self, section: CommitPatchSection) -> int | None:
669
+ for index, change in enumerate(self.detail.file_changes):
670
+ if change.path in {section.path, section.old_path, section.new_path}:
671
+ return index
672
+ return None
673
+
674
+ def _section_for_change(
675
+ self,
676
+ index: int,
677
+ change: CommitFileChange,
678
+ ) -> CommitPatchSection | None:
679
+ return section_for_change(self.detail.patch_sections, index, change)
680
+
681
+ def _display_label_for_file(self, index: int) -> str:
682
+ if not (0 <= index < len(self.detail.file_changes)):
683
+ return "No file selected"
684
+ change = self.detail.file_changes[index]
685
+ section = self._section_for_change(index, change)
686
+ return compact_label_for_change(change, section, 56)
687
+
688
+ def _build_stat_cell(
689
+ self,
690
+ value: int | None,
691
+ *,
692
+ positive: bool,
693
+ ) -> Text:
694
+ palette = self._theme_palette
695
+ if value is None:
696
+ return Text("-", style=make_style(palette.muted))
697
+ return Text(
698
+ str(value),
699
+ style=make_change_stat_style(palette, value, positive=positive),
700
+ )
701
+
702
+ def _visible_patch_sections(self) -> tuple[CommitPatchSection, ...]:
703
+ if self.show_full_patch:
704
+ return self.detail.patch_sections
705
+ if not self.detail.file_changes:
706
+ return ()
707
+ change = self.detail.file_changes[self.selected_file_index]
708
+ section = self._section_for_change(self.selected_file_index, change)
709
+ return (section,) if section is not None else ()
710
+
711
+ def _build_patch_title(self) -> Table:
712
+ palette = self._theme_palette
713
+ title = Table.grid(expand=True)
714
+ title.add_column(ratio=1)
715
+ title.add_column(no_wrap=True, justify="right")
716
+
717
+ current = Text()
718
+ if self.show_full_patch:
719
+ current.append("Full Patch", style=make_style(palette.muted, bold=True))
720
+ current.append(" ", style=make_style(palette.muted))
721
+ current.append(
722
+ f"{len(self.detail.patch_sections)} files",
723
+ style=make_style(palette.foreground),
724
+ )
725
+ else:
726
+ current.append("Selected Diff", style=make_style(palette.muted, bold=True))
727
+ if self.detail.file_changes:
728
+ current.append(" ", style=make_style(palette.muted))
729
+ current.append(
730
+ self._display_label_for_file(self.selected_file_index),
731
+ style=make_style(palette.foreground),
732
+ )
733
+
734
+ if self._search_query:
735
+ current.append(" ", style=make_style(palette.muted))
736
+ if self._match_lines:
737
+ current.append(
738
+ f"{self._current_match + 1}/{len(self._match_lines)} matches",
739
+ style=make_style(palette.primary),
740
+ )
741
+ else:
742
+ current.append("no matches", style=make_style(palette.muted))
743
+
744
+ modes = Text()
745
+ modes.append(
746
+ "File Diff",
747
+ style=make_style(
748
+ palette.primary if not self.show_full_patch else palette.muted,
749
+ bold=not self.show_full_patch,
750
+ ),
751
+ )
752
+ modes.append(" · ", style=make_style(palette.muted))
753
+ modes.append(
754
+ "Full Patch",
755
+ style=make_style(
756
+ palette.primary if self.show_full_patch else palette.muted,
757
+ bold=self.show_full_patch,
758
+ ),
759
+ )
760
+ title.add_row(current, modes)
761
+ return title
762
+
763
+ def _patch_line_styles(self) -> dict[str, Style]:
764
+ palette = self._theme_palette
765
+ return {
766
+ "header": make_style(palette.muted, bold=True),
767
+ "rename": make_style(palette.primary, bold=True),
768
+ "file": make_style(palette.muted),
769
+ "hunk": make_style(palette.primary, bold=True),
770
+ "added": make_style(palette.success),
771
+ "removed": make_style(palette.error),
772
+ "plain": make_style(palette.foreground),
773
+ }
774
+
775
+ @staticmethod
776
+ def _patch_line_kind(line: str) -> str:
777
+ if (
778
+ line.startswith("diff --git")
779
+ or line.startswith("index ")
780
+ or line.startswith("similarity index")
781
+ ):
782
+ return "header"
783
+ if line.startswith("rename from ") or line.startswith("rename to "):
784
+ return "rename"
785
+ if line.startswith("--- ") or line.startswith("+++ "):
786
+ return "file"
787
+ if line.startswith("@@"):
788
+ return "hunk"
789
+ if line.startswith("+") and not line.startswith("+++"):
790
+ return "added"
791
+ if line.startswith("-") and not line.startswith("---"):
792
+ return "removed"
793
+ return "plain"
794
+
795
+ def _append_patch_lines(
796
+ self,
797
+ lines: list[Line],
798
+ styles: list[Style],
799
+ patch_text: str,
800
+ line_styles: dict[str, Style],
801
+ ) -> None:
802
+ kind = self._patch_line_kind
803
+ patch_lines = patch_text.splitlines()
804
+ lines.extend(patch_lines)
805
+ styles.extend([line_styles[kind(line)] for line in patch_lines])
806
+
807
+ def _patch_sources_changed(self, key: str, sources: tuple[object, ...]) -> bool:
808
+ """Record the objects a patch view is built from and report whether they changed."""
809
+ built = self._patch_built_from.get(key)
810
+ same = built is not None and len(built) == len(sources) and all(
811
+ old is new for old, new in zip(built, sources)
812
+ )
813
+ self._patch_built_from[key] = sources
814
+ return not same
815
+
816
+ def _fill_selected_patch(self, line_styles: dict[str, Style]) -> None:
817
+ palette = self._theme_palette
818
+ sections = self._visible_patch_sections()
819
+ if not self._patch_sources_changed("selected", (palette, *sections)):
820
+ return
821
+ lines: list[Line] = []
822
+ styles: list[Style] = []
823
+ for section_index, section in enumerate(sections):
824
+ if section_index > 0:
825
+ lines.append("")
826
+ styles.append(line_styles["plain"])
827
+ self._append_patch_lines(lines, styles, section.patch, line_styles)
828
+ if not sections:
829
+ lines.append("No diff available for this selection.")
830
+ styles.append(make_style(palette.muted, bold=True))
831
+ self._selected_patch_scroll().set_lines(lines, styles)
832
+
833
+ def _fill_full_patch(self, line_styles: dict[str, Style]) -> None:
834
+ palette = self._theme_palette
835
+ if not self._patch_sources_changed("full", (palette, self.detail)):
836
+ return
837
+ self._patch_section_offsets = {}
838
+ self._outline_offsets = []
839
+ lines: list[Line] = []
840
+ styles: list[Style] = []
841
+ for section_index, section in enumerate(self.detail.patch_sections):
842
+ if section_index > 0:
843
+ lines.append("")
844
+ styles.append(line_styles["plain"])
845
+ self._patch_section_offsets[section] = len(lines)
846
+ file_index = self._file_index_for_section(section)
847
+ if file_index is not None:
848
+ self._outline_offsets.append((len(lines), file_index))
849
+ title = section.path
850
+ if section.is_rename:
851
+ title = f"{section.old_path} -> {section.new_path}"
852
+ heading = Text(title, style=make_style(palette.foreground, bold=True))
853
+ if section.is_binary:
854
+ heading.append(" [binary]", style=make_style(palette.muted, bold=True))
855
+ lines.append(heading)
856
+ styles.append(line_styles["plain"])
857
+ self._append_patch_lines(lines, styles, section.patch, line_styles)
858
+ if not self.detail.patch_sections:
859
+ lines.append("No patch available for this commit.")
860
+ styles.append(make_style(palette.muted, bold=True))
861
+ self._full_patch_scroll().set_lines(lines, styles)
862
+
863
+ def _render_patch_panel(
864
+ self,
865
+ *,
866
+ refresh_content: bool,
867
+ ) -> None:
868
+ if refresh_content:
869
+ palette = self._theme_palette
870
+ line_styles = self._patch_line_styles()
871
+ for view in (self._selected_patch_scroll(), self._full_patch_scroll()):
872
+ view.set_match_styles(
873
+ make_style(palette.selected_fg, background=palette.selected_bg),
874
+ make_style(palette.selected_fg, background=palette.primary, bold=True),
875
+ )
876
+ self._fill_selected_patch(line_styles)
877
+ self._fill_full_patch(line_styles)
878
+ self._active_patch_scroll().search(self._search_query)
879
+ self.query_one("#commit-detail-patch-title", Static).update(self._build_patch_title())
880
+
881
+ def apply_wrap_settings(self) -> None:
882
+ for view in (self._selected_patch_scroll(), self._full_patch_scroll()):
883
+ view.wrap = self.app.wrap_diffs
884
+
885
+ @property
886
+ def _match_lines(self) -> list[int]:
887
+ return self._active_patch_scroll().matches
888
+
889
+ @property
890
+ def _current_match(self) -> int:
891
+ return self._active_patch_scroll().current_match
892
+
893
+ # In-patch search
894
+
895
+ def _search_input(self) -> Input:
896
+ return self.query_one("#commit-detail-search", Input)
897
+
898
+ def _files_filter_input(self) -> Input:
899
+ return self.query_one("#commit-detail-files-filter", Input)
900
+
901
+ def _search_focused(self) -> bool:
902
+ try:
903
+ return self._search_input().has_focus or self._files_filter_input().has_focus
904
+ except Exception:
905
+ return False
906
+
907
+ def check_action(self, action: str, parameters: tuple[object, ...]) -> bool | None:
908
+ # While typing a search, j/k and the pane keys go to the input.
909
+ if self._search_focused() and action in (
910
+ "nav_down",
911
+ "nav_up",
912
+ "focus_next_pane",
913
+ "focus_prev_pane",
914
+ ):
915
+ return False
916
+ # enter/shift+enter act only while the patch search box is focused, and
917
+ # n/N only when there are matches.
918
+ if action in ("search_next", "search_prev"):
919
+ return self._search_input().has_focus
920
+ if action in ("next_match", "prev_match"):
921
+ return bool(self._match_lines)
922
+ return True
923
+
924
+ def action_open_search(self) -> None:
925
+ # / filters the files list from the files pane in diff mode and searches
926
+ # the patch from anywhere else.
927
+ if self.active_pane == "files" and not self.show_full_patch:
928
+ inp = self._files_filter_input()
929
+ inp.display = True
930
+ inp.focus()
931
+ return
932
+ inp = self._search_input()
933
+ inp.placeholder = "Search patch" if self.show_full_patch else "Search diff"
934
+ inp.display = True
935
+ inp.focus()
936
+
937
+ def on_input_changed(self, event: Input.Changed) -> None:
938
+ if event.input.id == "commit-detail-search":
939
+ self._search_query = event.value
940
+ view = self._active_patch_scroll()
941
+ view.search(self._search_query)
942
+ self._render_patch_panel(refresh_content=False)
943
+ view.scroll_to_current_match()
944
+ elif event.input.id == "commit-detail-files-filter":
945
+ self._files_query = event.value
946
+ self._refresh_files_table(force=True)
947
+ self._render_files_meta()
948
+
949
+ def on_input_submitted(self, event: Input.Submitted) -> None:
950
+ # Patch-search enter goes to the search_next binding, not here.
951
+ if event.input.id != "commit-detail-files-filter":
952
+ return
953
+ # Enter in the files filter shows the highlighted file and moves into the list.
954
+ visible = self._visible_file_indices()
955
+ table = self._current_files_table()
956
+ row = table.cursor_row
957
+ if visible and row is not None and 0 <= row < len(visible):
958
+ self.selected_file_index = visible[row]
959
+ self._render_patch_panel(refresh_content=True)
960
+ self._schedule_reconcile(focus_target="files")
961
+ else:
962
+ table.focus()
963
+
964
+ def _close_search(self) -> None:
965
+ # Hide the box but keep the matches and highlights for n / N.
966
+ self._search_input().display = False
967
+ self._active_patch_scroll().focus()
968
+
969
+ def _close_files_filter(self) -> None:
970
+ inp = self._files_filter_input()
971
+ inp.value = ""
972
+ inp.display = False
973
+ self._files_query = ""
974
+ self._refresh_files_table(force=True)
975
+ self._render_files_meta()
976
+ self._current_files_table().focus()
977
+
978
+ def action_next_match(self) -> None:
979
+ self._step_match(1)
980
+
981
+ def action_prev_match(self) -> None:
982
+ self._step_match(-1)
983
+
984
+ # enter / shift+enter, gated to the search box in check_action.
985
+ def action_search_next(self) -> None:
986
+ self.action_next_match()
987
+
988
+ def action_search_prev(self) -> None:
989
+ self.action_prev_match()
990
+
991
+ def _step_match(self, delta: int) -> None:
992
+ view = self._active_patch_scroll()
993
+ if not view.matches:
994
+ return
995
+ view.step_match(delta)
996
+ self._render_patch_panel(refresh_content=False)
997
+ view.scroll_to_current_match()
998
+
999
+ def _scroll_patch_to_selected(self) -> None:
1000
+ if not self.show_full_patch or not self.detail.file_changes:
1001
+ return
1002
+ change = self.detail.file_changes[self.selected_file_index]
1003
+ section = self._section_for_change(self.selected_file_index, change)
1004
+ if section is None:
1005
+ return
1006
+ offset = self._patch_section_offsets.get(section)
1007
+ if offset is None:
1008
+ return
1009
+ patch_scroll = self._full_patch_scroll()
1010
+ target_y = patch_scroll.row_for_line(offset)
1011
+ if int(patch_scroll.scroll_y) == target_y:
1012
+ self._ignore_next_patch_scroll_sync = False
1013
+ return
1014
+ self._ignore_next_patch_scroll_sync = True
1015
+ patch_scroll.scroll_to(
1016
+ y=target_y,
1017
+ animate=False,
1018
+ )
1019
+
1020
+ def _sync_outline_cursor(self, index: int) -> None:
1021
+ if not (0 <= index < len(self.detail.file_changes)):
1022
+ return
1023
+ visible = self._visible_file_indices()
1024
+ if index not in visible:
1025
+ return
1026
+ row = visible.index(index)
1027
+ table = self._current_files_table()
1028
+ if table.cursor_row == row:
1029
+ return
1030
+ self._suspend_file_events = True
1031
+ try:
1032
+ table.move_cursor(row=row, animate=False, scroll=True)
1033
+ finally:
1034
+ self._suspend_file_events = False
1035
+
1036
+ def _sync_selection_from_patch_scroll(self, scroll_y: float) -> None:
1037
+ if not self.show_full_patch or not self._outline_offsets:
1038
+ return
1039
+ patch_scroll = self._full_patch_scroll()
1040
+ viewport_height = (
1041
+ patch_scroll.scrollable_content_region.height
1042
+ or patch_scroll.content_region.height
1043
+ or patch_scroll.size.height
1044
+ or 1
1045
+ )
1046
+ anchor_line = patch_scroll.line_at_row(int(scroll_y) + max(viewport_height // 2, 0))
1047
+ active_index = self._outline_offsets[0][1]
1048
+ for offset, file_index in self._outline_offsets:
1049
+ if offset > anchor_line:
1050
+ break
1051
+ active_index = file_index
1052
+ if active_index == self.selected_file_index:
1053
+ self._sync_outline_cursor(active_index)
1054
+ return
1055
+ self.selected_file_index = active_index
1056
+ self._sync_outline_cursor(active_index)
1057
+
1058
+ def _on_full_patch_scroll_y(self, old_value: float, new_value: float) -> None:
1059
+ if self._ignore_next_patch_scroll_sync:
1060
+ self._ignore_next_patch_scroll_sync = False
1061
+ return
1062
+ self._sync_selection_from_patch_scroll(new_value)
1063
+
1064
+ def _visible_file_indices(self) -> list[int]:
1065
+ """Original indices of the files shown in the table, narrowed by the files
1066
+ filter in diff mode."""
1067
+ query = self._files_query.strip().lower()
1068
+ if not query or self.show_full_patch:
1069
+ return list(range(len(self.detail.file_changes)))
1070
+ indices: list[int] = []
1071
+ for index, change in enumerate(self.detail.file_changes):
1072
+ section = self._section_for_change(index, change)
1073
+ haystack = change.path.lower()
1074
+ if section is not None and section.is_rename:
1075
+ haystack = f"{section.old_path} {section.new_path}".lower()
1076
+ if query in haystack:
1077
+ indices.append(index)
1078
+ return indices
1079
+
1080
+ def _update_files_table(self) -> None:
1081
+ table = self._current_files_table()
1082
+ file_width = self._apply_files_table_layout()
1083
+ table.clear(columns=False)
1084
+ self._suspend_file_events = True
1085
+ try:
1086
+ visible = self._visible_file_indices()
1087
+ for index in visible:
1088
+ change = self.detail.file_changes[index]
1089
+ section = self._section_for_change(index, change)
1090
+ table.add_row(
1091
+ Text(compact_label_for_change(change, section, file_width)),
1092
+ self._build_stat_cell(change.additions, positive=True),
1093
+ self._build_stat_cell(change.deletions, positive=False),
1094
+ key=str(index),
1095
+ )
1096
+ if visible:
1097
+ row = visible.index(self.selected_file_index) if self.selected_file_index in visible else 0
1098
+ table.move_cursor(row=row, animate=False, scroll=True)
1099
+ finally:
1100
+ self._suspend_file_events = False
1101
+
1102
+ def _file_row_index(
1103
+ self, event: DataTable.RowHighlighted | DataTable.RowSelected
1104
+ ) -> int | None:
1105
+ """The file index of a files-table row event, or None for other tables."""
1106
+ if self._suspend_file_events or event.data_table.id not in self.FILE_TABLES:
1107
+ return None
1108
+ row_key = event.row_key.value if event.row_key else None
1109
+ return None if row_key is None else int(str(row_key))
1110
+
1111
+ def on_data_table_row_highlighted(self, event: DataTable.RowHighlighted) -> None:
1112
+ index = self._file_row_index(event)
1113
+ # While typing in the files filter, the table is repopulated and its
1114
+ # cursor moves, with the highlight ignored to keep focus on the input.
1115
+ if index is None or self._files_filter_input().has_focus:
1116
+ return
1117
+ if index != self.selected_file_index:
1118
+ self.selected_file_index = index
1119
+ if self.show_full_patch:
1120
+ if not self._mode_transition_active:
1121
+ self._schedule_reconcile(scroll_to_selection=True)
1122
+ else:
1123
+ self._render_patch_panel(refresh_content=True)
1124
+ self._schedule_reconcile()
1125
+
1126
+ def on_data_table_row_selected(self, event: DataTable.RowSelected) -> None:
1127
+ index = self._file_row_index(event)
1128
+ if index is None:
1129
+ return
1130
+ self.selected_file_index = index
1131
+ if self.show_full_patch:
1132
+ self._schedule_reconcile(
1133
+ focus_target="patch",
1134
+ scroll_to_selection=True,
1135
+ )
1136
+ else:
1137
+ self._render_patch_panel(refresh_content=True)
1138
+ self._schedule_reconcile(focus_target="patch")
1139
+
1140
+ def _scroll_target(self) -> ScrollableContainer | DataTable:
1141
+ return self._pane_target(self.active_pane)
1142
+
1143
+ def action_focus_next_pane(self) -> None:
1144
+ self._step_pane(1)
1145
+
1146
+ def action_focus_prev_pane(self) -> None:
1147
+ self._step_pane(-1)
1148
+
1149
+ def _step_pane(self, delta: int) -> None:
1150
+ panes = self._available_panes()
1151
+ if not panes:
1152
+ return
1153
+ current = self.active_pane if self.active_pane in panes else panes[0]
1154
+ index = panes.index(current)
1155
+ self.active_pane = panes[(index + delta) % len(panes)]
1156
+ self._focus_active_pane()
1157
+
1158
+ def _move_in_pane(
1159
+ self,
1160
+ on_table: Callable[[DataTable], object],
1161
+ on_scroll: Callable[[ScrollableContainer], object],
1162
+ ) -> None:
1163
+ """Apply the table or the scroll form of a movement to the active pane."""
1164
+ target = self._scroll_target()
1165
+ if isinstance(target, DataTable):
1166
+ on_table(target)
1167
+ else:
1168
+ on_scroll(target)
1169
+
1170
+ def _move_file_cursor(self, table: DataTable, row: int) -> None:
1171
+ if self.detail.file_changes:
1172
+ table.move_cursor(row=row, animate=False, scroll=True)
1173
+
1174
+ def action_cursor_up(self) -> None:
1175
+ self._move_in_pane(
1176
+ lambda table: table.action_cursor_up(), lambda view: view.scroll_up(animate=False)
1177
+ )
1178
+
1179
+ def action_cursor_down(self) -> None:
1180
+ self._move_in_pane(
1181
+ lambda table: table.action_cursor_down(), lambda view: view.scroll_down(animate=False)
1182
+ )
1183
+
1184
+ # j / k get their own actions, held back while a search box has focus.
1185
+ def action_nav_down(self) -> None:
1186
+ self.action_cursor_down()
1187
+
1188
+ def action_nav_up(self) -> None:
1189
+ self.action_cursor_up()
1190
+
1191
+ def action_page_up(self) -> None:
1192
+ self._move_in_pane(
1193
+ lambda table: table.action_page_up(), lambda view: view.scroll_page_up(animate=False)
1194
+ )
1195
+
1196
+ def action_page_down(self) -> None:
1197
+ self._move_in_pane(
1198
+ lambda table: table.action_page_down(),
1199
+ lambda view: view.scroll_page_down(animate=False),
1200
+ )
1201
+
1202
+ def action_scroll_top(self) -> None:
1203
+ self._move_in_pane(
1204
+ lambda table: self._move_file_cursor(table, 0),
1205
+ lambda view: view.scroll_home(animate=False),
1206
+ )
1207
+
1208
+ def action_scroll_bottom(self) -> None:
1209
+ self._move_in_pane(
1210
+ lambda table: self._move_file_cursor(table, len(self.detail.file_changes) - 1),
1211
+ lambda view: view.scroll_end(animate=False),
1212
+ )
1213
+
1214
+ def action_toggle_patch_scope(self) -> None:
1215
+ was_full_patch = self.show_full_patch
1216
+ self._mode_transition_active = True
1217
+ self._force_requested_focus = True
1218
+ self.show_full_patch = not self.show_full_patch
1219
+ self.active_pane = "files" if was_full_patch else "patch"
1220
+ scroll_to_selection = not was_full_patch and not self._full_patch_ready
1221
+ if not was_full_patch:
1222
+ self._full_patch_ready = True
1223
+ self._render_detail_view(
1224
+ focus_target=self.active_pane,
1225
+ scroll_to_selection=scroll_to_selection,
1226
+ )
1227
+ # Drop the files filter when switching to the full patch.
1228
+ if self.show_full_patch and self._files_filter_input().display:
1229
+ files_filter = self._files_filter_input()
1230
+ files_filter.value = ""
1231
+ files_filter.display = False
1232
+ self._files_query = ""
1233
+ if self._search_input().display:
1234
+ self._search_input().placeholder = (
1235
+ "Search patch" if self.show_full_patch else "Search diff"
1236
+ )
1237
+
1238
+ def action_close_screen(self) -> None:
1239
+ if self._search_input().display:
1240
+ self._close_search()
1241
+ return
1242
+ if self._files_filter_input().display:
1243
+ self._close_files_filter()
1244
+ return
1245
+ self.dismiss(None)
1246
+
1247
+ def handle_repo_context_refresh(
1248
+ self,
1249
+ previous_context: RepoContext,
1250
+ new_context: RepoContext,
1251
+ previous_snapshot: RepoScanSnapshot,
1252
+ new_snapshot: RepoScanSnapshot,
1253
+ ) -> None:
1254
+ self.sub_title = self._build_repo_stats_sub_title()
1255
+ if self.source_branch is None:
1256
+ return
1257
+ old_heads = dict(previous_snapshot.branch_heads)
1258
+ new_heads = dict(new_snapshot.branch_heads)
1259
+ old_branch_head = old_heads.get(self.source_branch)
1260
+ new_branch_head = new_heads.get(self.source_branch)
1261
+ if (
1262
+ old_branch_head is None
1263
+ or new_branch_head is None
1264
+ or old_branch_head == new_branch_head
1265
+ or self.commit_hash != old_branch_head
1266
+ ):
1267
+ return
1268
+ self._show_commit(new_branch_head)