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,216 @@
1
+ """Branch picker modal."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Optional
6
+
7
+ from rich.text import Text
8
+ from textual.app import ComposeResult
9
+ from textual.binding import Binding
10
+ from textual.screen import ModalScreen
11
+ from textual.widgets import Footer, Input, OptionList, Static
12
+ from textual.widgets.option_list import Option
13
+
14
+ from ..theme import make_style, resolve_theme_palette
15
+ from ..utils import fuzzy_score, truncate_middle
16
+ from .shared import BACK_GROUP, MOVE_GROUP
17
+
18
+
19
+ class BranchPickerScreen(ModalScreen[Optional[str]]):
20
+ """Modal branch picker."""
21
+
22
+ CSS = """
23
+ BranchPickerScreen {
24
+ align: center middle;
25
+ }
26
+
27
+ #branch-picker {
28
+ width: 56;
29
+ height: 20;
30
+ padding: 1 1;
31
+ background: $panel;
32
+ color: $text;
33
+ border: round $primary;
34
+ }
35
+
36
+ #branch-picker-title {
37
+ text-style: bold;
38
+ margin: 0 1 1 1;
39
+ }
40
+
41
+ #branch-picker-filter {
42
+ margin: 0 1 1 1;
43
+ border: solid $border;
44
+ background: $surface;
45
+ color: $text;
46
+ outline: none;
47
+ }
48
+
49
+ #branch-picker-filter:focus {
50
+ border: solid $primary;
51
+ outline: none;
52
+ }
53
+
54
+ #branch-picker-options {
55
+ margin: 0 1;
56
+ border: solid $border;
57
+ height: 1fr;
58
+ background: $surface;
59
+ }
60
+
61
+ #branch-picker-options:focus {
62
+ border: solid $primary;
63
+ }
64
+
65
+ """
66
+
67
+ BINDINGS = [
68
+ Binding("down", "cursor_down", "Down", priority=True, group=MOVE_GROUP),
69
+ Binding("up", "cursor_up", "Up", priority=True, group=MOVE_GROUP),
70
+ Binding("enter", "choose_branch", "Choose", priority=True),
71
+ Binding("escape", "cancel", "Back", group=BACK_GROUP),
72
+ Binding("q", "cancel", "Back", show=False, group=BACK_GROUP),
73
+ ]
74
+
75
+ def __init__(
76
+ self,
77
+ branches: list[str],
78
+ current_branch: str,
79
+ divergence: dict[str, tuple[int, int]] | None = None,
80
+ base_branch: str | None = None,
81
+ ) -> None:
82
+ super().__init__()
83
+ self.branches = branches
84
+ self.current_branch = current_branch
85
+ # {branch: (ahead, behind)} relative to base_branch, for the ahead/behind marks.
86
+ self._divergence = divergence or {}
87
+ self._base_branch = base_branch
88
+ self.filtered_branches = branches[:]
89
+ self.highlighted_branch = self._default_highlight(branches)
90
+ self._theme_palette = resolve_theme_palette(self.app.current_theme)
91
+
92
+ def _default_highlight(self, branches: list[str]) -> str:
93
+ """The current branch when listed, else the first branch."""
94
+ if self.current_branch in branches:
95
+ return self.current_branch
96
+ return branches[0] if branches else ""
97
+
98
+ def compose(self) -> ComposeResult:
99
+ with Static(id="branch-picker"):
100
+ yield Static("Select Branch", id="branch-picker-title")
101
+ yield Input(placeholder="Filter branches", id="branch-picker-filter")
102
+ yield OptionList(id="branch-picker-options")
103
+ yield Footer()
104
+
105
+ def on_mount(self) -> None:
106
+ self.watch(self.app, "theme", self._on_theme_change, init=False)
107
+ self._apply_theme()
108
+ self._render_title()
109
+ self._refresh_options()
110
+ self.query_one("#branch-picker-filter", Input).focus()
111
+
112
+ def _render_title(self) -> None:
113
+ title = Text("Select Branch", style=make_style(self._theme_palette.foreground, bold=True))
114
+ if self._base_branch and any(a or b for a, b in self._divergence.values()):
115
+ title.append(
116
+ f" ↑ ahead / ↓ behind {self._base_branch}",
117
+ style=make_style(self._theme_palette.muted),
118
+ )
119
+ self.query_one("#branch-picker-title", Static).update(title)
120
+
121
+ def _on_theme_change(self, old_value: str, new_value: str) -> None:
122
+ self._apply_theme()
123
+ self._refresh_options()
124
+
125
+ def _apply_theme(self) -> None:
126
+ # Frame colors come from the registered theme via CSS $vars with only the
127
+ # screen dim needing the palette's alpha-blended overlay.
128
+ self._theme_palette = resolve_theme_palette(self.app.current_theme)
129
+ self.styles.background = self._theme_palette.overlay
130
+
131
+ def _build_option(self, branch: str) -> Option:
132
+ palette = self._theme_palette
133
+ prompt = Text()
134
+ prompt.append(truncate_middle(branch, 30), style=make_style(palette.foreground))
135
+ if branch == self.current_branch:
136
+ prompt.append(" current", style=make_style(palette.primary, bold=True))
137
+ ahead, behind = self._divergence.get(branch, (0, 0))
138
+ if ahead:
139
+ prompt.append(f" ↑{ahead}", style=make_style(palette.success))
140
+ if behind:
141
+ prompt.append(f" ↓{behind}", style=make_style(palette.error))
142
+ return Option(prompt, id=branch)
143
+
144
+ def _refresh_options(self) -> None:
145
+ options = self.query_one("#branch-picker-options", OptionList)
146
+ if not self.filtered_branches:
147
+ palette = self._theme_palette
148
+ options.set_options(
149
+ [Option(Text("No matching branches", style=make_style(palette.muted)), disabled=True)]
150
+ )
151
+ options.highlighted = None
152
+ self.highlighted_branch = ""
153
+ return
154
+
155
+ if self.highlighted_branch not in self.filtered_branches:
156
+ self.highlighted_branch = self.filtered_branches[0]
157
+
158
+ options.set_options([self._build_option(branch) for branch in self.filtered_branches])
159
+ options.highlighted = self.filtered_branches.index(self.highlighted_branch)
160
+ options.scroll_to_highlight(top=False)
161
+
162
+ def on_input_changed(self, event: Input.Changed) -> None:
163
+ if event.input.id != "branch-picker-filter":
164
+ return
165
+ query = event.value.strip()
166
+ if not query:
167
+ self.filtered_branches = self.branches[:]
168
+ self.highlighted_branch = self._default_highlight(self.filtered_branches)
169
+ self._refresh_options()
170
+ return
171
+
172
+ ranked: list[tuple[int, str]] = []
173
+ for branch in self.branches:
174
+ score = fuzzy_score(query, branch)
175
+ if score is not None:
176
+ ranked.append((score, branch))
177
+ ranked.sort(key=lambda item: (item[0], item[1]))
178
+ self.filtered_branches = [branch for _score, branch in ranked]
179
+ self._refresh_options()
180
+
181
+ def on_input_submitted(self, event: Input.Submitted) -> None:
182
+ if event.input.id != "branch-picker-filter":
183
+ return
184
+ if self.highlighted_branch:
185
+ self.dismiss(self.highlighted_branch)
186
+
187
+ def on_option_list_option_highlighted(
188
+ self,
189
+ event: OptionList.OptionHighlighted,
190
+ ) -> None:
191
+ if event.option_list.id != "branch-picker-options":
192
+ return
193
+ if event.option_id is not None:
194
+ self.highlighted_branch = str(event.option_id)
195
+
196
+ def on_option_list_option_selected(
197
+ self,
198
+ event: OptionList.OptionSelected,
199
+ ) -> None:
200
+ if event.option_list.id != "branch-picker-options":
201
+ return
202
+ if event.option_id is not None:
203
+ self.dismiss(str(event.option_id))
204
+
205
+ def action_cursor_up(self) -> None:
206
+ self.query_one("#branch-picker-options", OptionList).action_cursor_up()
207
+
208
+ def action_cursor_down(self) -> None:
209
+ self.query_one("#branch-picker-options", OptionList).action_cursor_down()
210
+
211
+ def action_choose_branch(self) -> None:
212
+ if self.highlighted_branch:
213
+ self.dismiss(self.highlighted_branch)
214
+
215
+ def action_cancel(self) -> None:
216
+ self.dismiss(None)