pi-odd-worktree 2.1.0 → 3.0.1

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.
Files changed (160) hide show
  1. package/README.md +54 -319
  2. package/extensions/odd-worktree-tools.ts +4182 -2539
  3. package/lib/bash-activity.js +1166 -0
  4. package/lib/bash-design.js +684 -0
  5. package/lib/bash-destructive.js +559 -0
  6. package/lib/bash-frame.js +205 -0
  7. package/lib/card-content.js +23 -0
  8. package/lib/card-style.js +175 -0
  9. package/lib/checkout-facts.js +30 -70
  10. package/lib/codegraph-card-preview.js +628 -0
  11. package/lib/codegraph-card.js +515 -0
  12. package/lib/codegraph-compaction.js +76 -0
  13. package/lib/codegraph-daemon.js +246 -0
  14. package/lib/codegraph-edit-sync.js +182 -0
  15. package/lib/codegraph-guard-target.js +337 -0
  16. package/lib/codegraph-guard.js +211 -0
  17. package/lib/codegraph-index.js +1020 -0
  18. package/lib/codegraph-lock.js +607 -0
  19. package/lib/codegraph-mcp-config.js +134 -0
  20. package/lib/codegraph-mcp.js +123 -0
  21. package/lib/codegraph-paths.js +69 -0
  22. package/lib/codegraph-prompt/explore.md +11 -0
  23. package/lib/codegraph-prompt/fix.md +6 -0
  24. package/lib/codegraph-prompt/generic.md +8 -0
  25. package/lib/codegraph-prompt/judge.md +9 -0
  26. package/lib/codegraph-prompt/orchestrator.md +17 -0
  27. package/lib/codegraph-prompt/verify.md +10 -0
  28. package/lib/codegraph-prompt/worker.md +7 -0
  29. package/lib/codegraph-role.js +55 -0
  30. package/lib/codegraph-rules.js +146 -0
  31. package/lib/codegraph-session.js +303 -383
  32. package/lib/codegraph-warmup.js +139 -0
  33. package/lib/file-card-markdown.js +141 -0
  34. package/lib/file-card-resolver.js +245 -0
  35. package/lib/file-card.js +659 -0
  36. package/lib/files-index.js +387 -0
  37. package/lib/files-insert.js +81 -0
  38. package/lib/files-orders.js +326 -0
  39. package/lib/files-picker-view.js +1421 -0
  40. package/lib/files-preview.js +338 -0
  41. package/lib/files-scope-detail.js +171 -0
  42. package/lib/files-scope-view.js +507 -0
  43. package/lib/gh-issues.js +323 -15
  44. package/lib/issue-preview.js +26 -7
  45. package/lib/issues-bash-mutation.js +206 -0
  46. package/lib/issues-cache-store.js +261 -0
  47. package/lib/issues-section.js +710 -0
  48. package/lib/issues-view.js +649 -52
  49. package/lib/odd-prompt/odd-worktrees.md +21 -0
  50. package/lib/odd-worktrees-prompt.js +10 -28
  51. package/lib/related-issues-cache.js +210 -0
  52. package/lib/shell-card.js +825 -0
  53. package/lib/skill-odd-card.js +183 -0
  54. package/lib/tui-paint.js +4 -5
  55. package/lib/warp-create-panel.js +538 -250
  56. package/lib/warp-create-picker.js +56 -9
  57. package/lib/warp-delete-confirm.js +19 -28
  58. package/lib/warp-selector-view.js +160 -82
  59. package/lib/which-command.js +90 -0
  60. package/lib/worktree-card.js +267 -897
  61. package/lib/worktree-cards.js +383 -0
  62. package/lib/worktree-create.js +287 -0
  63. package/lib/worktree-git.js +3 -3
  64. package/lib/worktree-guard.js +548 -0
  65. package/lib/worktree-hooks.js +421 -712
  66. package/lib/worktree-list.js +120 -0
  67. package/lib/worktree-registry.js +162 -879
  68. package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
  69. package/lib/worktree-setup-runner.js +376 -0
  70. package/lib/worktree-setup.js +140 -0
  71. package/lib/worktree-warp-busy.js +462 -85
  72. package/lib/worktree-warp-sessions.js +1 -2
  73. package/package.json +2 -2
  74. package/tools/bash-activity.test.js +1420 -0
  75. package/tools/bash-design.test.js +1093 -0
  76. package/tools/bash-destructive.test.js +139 -0
  77. package/tools/card-content.test.js +44 -0
  78. package/tools/card-style.test.js +298 -0
  79. package/tools/codegraph-card-preview.test.js +254 -0
  80. package/tools/codegraph-card.test.js +1080 -0
  81. package/tools/codegraph-compaction.test.js +378 -0
  82. package/tools/codegraph-daemon.test.js +248 -0
  83. package/tools/codegraph-edit-sync.test.js +295 -0
  84. package/tools/codegraph-guard-target.test.js +174 -0
  85. package/tools/codegraph-guard.test.js +238 -0
  86. package/tools/codegraph-index.test.js +1948 -0
  87. package/tools/codegraph-lock.test.js +932 -0
  88. package/tools/codegraph-mcp-config.test.js +181 -0
  89. package/tools/codegraph-mcp.test.js +194 -0
  90. package/tools/codegraph-paths.test.js +44 -0
  91. package/tools/codegraph-role.test.js +30 -0
  92. package/tools/codegraph-rules.test.js +250 -0
  93. package/tools/codegraph-session.test.js +660 -391
  94. package/tools/codegraph-warmup.test.js +316 -0
  95. package/tools/commands.test.js +510 -31
  96. package/tools/file-card-markdown.test.js +188 -0
  97. package/tools/file-card.test.js +1289 -0
  98. package/tools/files-bridge.test.js +575 -0
  99. package/tools/files-command.test.js +613 -0
  100. package/tools/files-index.test.js +473 -0
  101. package/tools/files-insert.test.js +139 -0
  102. package/tools/files-orders.test.js +597 -0
  103. package/tools/files-picker-view.test.js +1889 -0
  104. package/tools/files-preview.test.js +452 -0
  105. package/tools/files-scope-detail.test.js +264 -0
  106. package/tools/files-scope-view.test.js +614 -0
  107. package/tools/fixtures/card-codegraph-outputs.js +110 -0
  108. package/tools/fixtures/codegraph-cli-stub.js +16 -0
  109. package/tools/fixtures/codegraph-init-process.js +28 -0
  110. package/tools/fixtures/codegraph-lock-worker.js +117 -0
  111. package/tools/fixtures/daemon-fake.js +98 -0
  112. package/tools/fixtures/daemon-mcp-server.js +39 -0
  113. package/tools/fixtures/daemon-warmup-worker.js +12 -0
  114. package/tools/fixtures/load-extension.js +136 -3
  115. package/tools/gh-issues-feedback.test.js +31 -7
  116. package/tools/gh-issues.test.js +417 -1
  117. package/tools/issue-preview.test.js +44 -7
  118. package/tools/issues-bash-mutation.test.js +101 -0
  119. package/tools/issues-cache-store.test.js +222 -0
  120. package/tools/issues-command.test.js +1517 -67
  121. package/tools/issues-open-guard.test.js +1096 -0
  122. package/tools/issues-section.test.js +4235 -0
  123. package/tools/issues-seed-query-regression.test.js +67 -0
  124. package/tools/issues-view.test.js +1489 -60
  125. package/tools/odd-worktrees-prompt.test.js +98 -65
  126. package/tools/package-surface.test.js +431 -36
  127. package/tools/related-issues-cache.test.js +324 -0
  128. package/tools/shell-card.test.js +834 -0
  129. package/tools/skill-odd-card.test.js +200 -0
  130. package/tools/t5-session-identity.test.js +44 -22
  131. package/tools/warp-busy-guard.test.js +2565 -309
  132. package/tools/warp-create-panel.test.js +499 -74
  133. package/tools/warp-create-picker.test.js +101 -5
  134. package/tools/warp-delete-confirm.test.js +49 -15
  135. package/tools/warp-empty-session.test.js +0 -1
  136. package/tools/warp-fallback-regression.test.js +533 -0
  137. package/tools/warp-selector-view.test.js +460 -196
  138. package/tools/which-command.test.js +126 -0
  139. package/tools/worktree-card.test.js +551 -0
  140. package/tools/worktree-cards.test.js +845 -0
  141. package/tools/worktree-changes.test.js +4 -5
  142. package/tools/worktree-create.test.js +489 -0
  143. package/tools/worktree-guard.test.js +823 -0
  144. package/tools/worktree-hooks.test.js +905 -0
  145. package/tools/worktree-list.test.js +170 -0
  146. package/tools/worktree-registry.test.js +89 -230
  147. package/tools/worktree-remove-async.test.js +318 -0
  148. package/tools/worktree-setup-runner.test.js +276 -0
  149. package/tools/worktree-setup.test.js +140 -0
  150. package/tools/worktree-tools.test.js +419 -199
  151. package/lib/codegraph-commands.js +0 -165
  152. package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
  153. package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
  154. package/lib/worktree-model-result.js +0 -291
  155. package/tools/codegraph-commands.test.js +0 -371
  156. package/tools/herdr-free-create.test.js +0 -161
  157. package/tools/worktree-close-herdr-async.test.js +0 -204
  158. package/tools/worktree-odd-migration.test.js +0 -224
  159. package/tools/worktree-registry-cache.test.js +0 -163
  160. package/tools/worktree-registry-odd.test.js +0 -231
@@ -0,0 +1,507 @@
1
+ 'use strict';
2
+ // S9: the worktree scope step — the single-choice card `/files` opens behind.
3
+ // S20: it opens on EVERY invocation, and `a` toggles its rows between the
4
+ // checkouts the session registered (the default rows) and every worktree of the
5
+ // clone. It knows only about scopes, never about Git or the Pi session: T7
6
+ // resolves the repository, its checkouts, their uncommitted counts and their Git
7
+ // detail, and this component orders, paints and routes them.
8
+ //
9
+ // Ported from the read-only reference (`lib/file-scope-view.ts`), on this
10
+ // package's substrate instead of gentle-shell's palette card: `lib/shell-card.js`
11
+ // draws the top and bottom rules and the one-role rows, `lib/tui-paint.js` paints
12
+ // the label/count pair, the focused band and the footer legend, and
13
+ // `lib/view-layout.js` owns the measurement and the list window.
14
+ //
15
+ // PAINT LAST (AGENTS.md invariant 17): every row is measured and padded as PLAIN
16
+ // text and painted afterwards, so the helpers never measure an escape sequence
17
+ // this repo composed. The scope rows and the footer are the two rows carrying
18
+ // more than one tone, and they are framed here with the card's own `│ ` / ` │`
19
+ // chrome, exactly the cells `cardLine` draws, because a row with a label, a
20
+ // right-aligned count and a selection band cannot be one role.
21
+
22
+ const { CARD_TONE, cardBottom, cardInnerWidth, cardLine, cardTop } = require('./shell-card.js');
23
+ const { highlight, key, label, legendRow, paint, value } = require('./tui-paint.js');
24
+ const { measure, scrollWindow } = require('./view-layout.js');
25
+ const { countCell, orderScopes, scopeDetailLine } = require('./files-scope-detail.js');
26
+
27
+ const TITLE = 'Files';
28
+ const SUBTITLE = 'Choose a scope';
29
+ const EMPTY_TEXT = 'No checkouts to choose from.';
30
+ // The tone every informational card uses; its frame role is the theme's plain
31
+ // `border` (lib/shell-card.js FRAME_ROLE), which is why the side cells below are
32
+ // painted with that one role.
33
+ const TONE = CARD_TONE.INFO;
34
+ const FRAME_ROLE = 'border';
35
+ const FRAME_SIDE = '│';
36
+ // The rows the card spends on chrome: the top rule, one blank row, the detail
37
+ // line, the footer legend and the bottom rule. One scope row needs one more.
38
+ const CHROME_ROWS = 5;
39
+ const MIN_CARD_HEIGHT = CHROME_ROWS + 1;
40
+ // Narrower than this and the card cannot hold a label and its count.
41
+ const MIN_WIDTH = 20;
42
+ const MAX_CARD_HEIGHT = 24;
43
+ // 0-based row of the first scope: the top rule and the blank row precede it.
44
+ const BODY_TOP = 2;
45
+ const MARKER_WIDTH = 2;
46
+ const KEY_ROLE = 'accent';
47
+
48
+ // S9, one option per key (the package's own footer rule): arrows move, enter
49
+ // opens, esc closes. `ctrl+j`/`ctrl+k` and `ctrl+c` keep working unadvertised.
50
+ const FOOTER_ENTRIES = Object.freeze([
51
+ { key: '↑/↓', text: '↑/↓ move', short: '↑/↓', priority: 100 },
52
+ { key: 'enter', text: 'enter open', short: 'enter', priority: 80 },
53
+ { key: 'esc', text: 'esc close', short: 'esc', priority: 60 },
54
+ ]);
55
+ // S20: the toggle's own entry, spliced after the move keys by `footerEntries`.
56
+ // Its words name the list the key switches TO, so the footer always says where
57
+ // the key goes, never where it is.
58
+ const TOGGLE_PRIORITY = 90;
59
+ const LIST_TOGGLE = 'a';
60
+
61
+ // The card substrate and the palette both call the injected theme directly, and
62
+ // only the palette guards a throwing one. The view wraps it once here, so a
63
+ // missing, partial or hostile theme leaves the text plain instead of breaking a
64
+ // render — the same guarantee `lib/tui-paint.js` gives on its own.
65
+ function guardedTheme(theme) {
66
+ const source = theme !== null && typeof theme === 'object' ? theme : {};
67
+ const call = (name, role, text) => {
68
+ try {
69
+ if (typeof source[name] === 'function') {
70
+ const painted = name === 'bold' ? source[name](text) : source[name](role, text);
71
+ if (typeof painted === 'string') return painted;
72
+ }
73
+ } catch {
74
+ // Fall through to the plain text.
75
+ }
76
+ return String(text);
77
+ };
78
+ return {
79
+ fg: (role, text) => call('fg', role, text),
80
+ bg: (role, text) => call('bg', role, text),
81
+ bold: (text) => call('bold', undefined, text),
82
+ };
83
+ }
84
+
85
+ // The sanitizer a terminal value passes before it is measured: no escape
86
+ // sequence and no control character reaches a painted row, so a branch name can
87
+ // never move the cursor or break the frame.
88
+ function sanitize(value) {
89
+ const text = typeof value === 'string' ? value : (value == null ? '' : String(value));
90
+ return text
91
+ .replace(/\x1b\[[0-9;]*m/g, '')
92
+ .replace(/\x1b\][^\x07]*\x07/g, '')
93
+ .replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ' ');
94
+ }
95
+
96
+ /**
97
+ * Mount the scope step (S9).
98
+ *
99
+ * @param {object} options
100
+ * @param {Array<{root: string, label: string, kind: 'main'|'worktree', lastModifiedMs: number, detail?: object}>} options.scopes
101
+ * the candidate scopes, in any order; `main` always leads and the rest follow
102
+ * by recency.
103
+ * @param {Array<object>} [options.allScopes] S20: every worktree of the clone, the
104
+ * list `a` toggles to. Built before the step opens, so the view stays
105
+ * synchronous; absent means the key is not offered at all.
106
+ * @param {object} [options.theme] the host theme; a missing or hostile one leaves text plain.
107
+ * @param {number|Function} [options.rowsAvailable] the terminal rows the host offers, or a reader for them.
108
+ * @param {object} [options.helpers] `visibleWidth`/`truncateToWidth`/`matchesKey`/`isKeyRelease`, injected.
109
+ * @param {number} [options.now] the clock the detail line's age reads once; defaults to `Date.now()`.
110
+ * @param {Function} [options.onSelect] called with the chosen scope.
111
+ * @param {Function} [options.onClose] called when the step closes without a choice.
112
+ * @param {Function} [options.requestRender] called when a key or mouse event changed what is painted.
113
+ * @returns {{render: Function, handleInput: Function, handleMouse: Function, invalidate: Function, dispose: Function}}
114
+ */
115
+ function createFilesScopeView(options = {}) {
116
+ const opts = options !== null && typeof options === 'object' ? options : {};
117
+ const theme = guardedTheme(opts.theme);
118
+ const helpers = opts.helpers !== null && typeof opts.helpers === 'object' ? opts.helpers : {};
119
+ const rowsAvailable = opts.rowsAvailable;
120
+ const onSelect = typeof opts.onSelect === 'function' ? opts.onSelect : null;
121
+ const onClose = typeof opts.onClose === 'function' ? opts.onClose : null;
122
+ const requestRender = typeof opts.requestRender === 'function' ? opts.requestRender : null;
123
+ // S20: the two lists. `registered` is the default (the session's own checkout
124
+ // plus the worktrees it registered) and `all` is every worktree of the clone,
125
+ // which is null when the wiring had no second list to offer.
126
+ const lists = {
127
+ registered: orderScopes(Array.isArray(opts.scopes) ? opts.scopes.slice() : []),
128
+ all: Array.isArray(opts.allScopes) ? orderScopes(opts.allScopes.slice()) : null,
129
+ };
130
+ let active = 'registered';
131
+ const scopes = () => (active === 'all' && lists.all !== null ? lists.all : lists.registered);
132
+ // The age is computed once when the step opens and never refreshed live (S6).
133
+ const now = Number.isFinite(Number(opts.now)) ? Number(opts.now) : Date.now();
134
+
135
+ let selected = 0;
136
+ let hovered;
137
+ let pointerFrame;
138
+ let completed = false;
139
+ let disposed = false;
140
+
141
+ const CARD = Object.freeze({ title: TITLE, subtitle: SUBTITLE, tone: TONE });
142
+
143
+ function repaint() {
144
+ try {
145
+ if (requestRender !== null) requestRender();
146
+ } catch {
147
+ // A hostile host callback never breaks the step.
148
+ }
149
+ }
150
+
151
+ function clampedSelected() {
152
+ const rows = scopes();
153
+ if (rows.length === 0) return 0;
154
+ return Math.max(0, Math.min(rows.length - 1, selected));
155
+ }
156
+
157
+ function moveSelection(delta) {
158
+ const rows = scopes();
159
+ if (rows.length === 0) return false;
160
+ const next = Math.max(0, Math.min(rows.length - 1, selected + delta));
161
+ if (next === selected) return false;
162
+ selected = next;
163
+ return true;
164
+ }
165
+
166
+ // S20: `a` switches the rows between the two lists. A scope both lists carry
167
+ // (matched by its root) keeps the cursor; otherwise the cursor goes back to the
168
+ // first row, so the step never leaves the focus on a row nobody can see.
169
+ function toggleList() {
170
+ if (lists.all === null) return false;
171
+ const focused = scopes()[clampedSelected()];
172
+ const root = focused !== null && typeof focused === 'object' ? focused.root : undefined;
173
+ active = active === 'registered' ? 'all' : 'registered';
174
+ const rows = scopes();
175
+ const at = typeof root === 'string' ? rows.findIndex((scope) => scope !== null && typeof scope === 'object' && scope.root === root) : -1;
176
+ selected = at >= 0 ? at : 0;
177
+ return true;
178
+ }
179
+
180
+ function setHover(index) {
181
+ if (hovered === index) return false;
182
+ hovered = index;
183
+ return true;
184
+ }
185
+
186
+ function cardHeight() {
187
+ let available = rowsAvailable;
188
+ if (typeof available === 'function') {
189
+ try {
190
+ available = available();
191
+ } catch {
192
+ return 0;
193
+ }
194
+ }
195
+ const value = Number(available);
196
+ if (!Number.isFinite(value)) return 0;
197
+ return Math.max(0, Math.min(MAX_CARD_HEIGHT, Math.floor(value)));
198
+ }
199
+
200
+ function clipPlain(text, room) {
201
+ const value = sanitize(text);
202
+ if (room <= 0) return '';
203
+ if (measure(helpers, value) <= room) return value;
204
+ try {
205
+ if (typeof helpers.truncateToWidth === 'function') {
206
+ const cut = helpers.truncateToWidth(value, room, '…');
207
+ if (typeof cut === 'string') return cut;
208
+ }
209
+ } catch {
210
+ // Fall through to the plain codepoint slice.
211
+ }
212
+ const chars = [...value];
213
+ return chars.length <= room ? value : `${chars.slice(0, Math.max(0, room - 1)).join('')}…`;
214
+ }
215
+
216
+ function padPlain(text, width) {
217
+ return text + ' '.repeat(Math.max(0, width - measure(helpers, text)));
218
+ }
219
+
220
+ // One row of the card's body, framed with the same chrome `cardLine` draws.
221
+ function frameRow(paintedContent) {
222
+ const side = paint(theme, FRAME_ROLE, FRAME_SIDE);
223
+ return `${side} ${paintedContent} ${side}`;
224
+ }
225
+
226
+ function scopeRow(index, focusedIndex, inner) {
227
+ const scope = scopes()[index] || {};
228
+ const focused = index === focusedIndex;
229
+ const marker = focused ? '▸ ' : ' ';
230
+ const changed = Number(scope.detail && scope.detail.changedFiles);
231
+ const countText = Number.isFinite(changed) && changed > 0 ? countCell(changed) : '';
232
+ const countWidth = countText === '' ? 0 : measure(helpers, countText);
233
+ const showCount = countWidth > 0 && inner > MARKER_WIDTH + countWidth + 1;
234
+ const room = Math.max(0, inner - MARKER_WIDTH - (showCount ? countWidth + 1 : 0));
235
+ const shownLabel = clipPlain(scope.label !== undefined ? scope.label : scope.root, room);
236
+ const labelWidth = measure(helpers, shownLabel);
237
+ const gap = Math.max(showCount ? 1 : 0, inner - MARKER_WIDTH - labelWidth - (showCount ? countWidth : 0));
238
+ const shownCount = showCount ? countText : '';
239
+ if (focused) return frameRow(highlight(theme, `${marker}${shownLabel}${' '.repeat(gap)}${shownCount}`, true));
240
+ const paintedMarker = paint(theme, KEY_ROLE, marker);
241
+ const paintedLabel = index === hovered ? key(theme, shownLabel) : value(theme, shownLabel);
242
+ const paintedCount = showCount ? paint(theme, KEY_ROLE, countText) : '';
243
+ return frameRow(`${paintedMarker}${paintedLabel}${' '.repeat(gap)}${paintedCount}`);
244
+ }
245
+
246
+ function indicatorRow(width, text) {
247
+ return cardLine(text, TONE, theme, width, metrics, 'muted');
248
+ }
249
+
250
+ function scopeRows(width, inner, bodyRows, hits) {
251
+ const rows = scopes();
252
+ const lines = [];
253
+ if (rows.length === 0) {
254
+ if (bodyRows > 0) lines.push(frameRow(label(theme, padPlain(clipPlain(EMPTY_TEXT, inner), inner))));
255
+ } else if (bodyRows > 0) {
256
+ const focused = clampedSelected();
257
+ const costs = rows.map(() => 1);
258
+ let window = scrollWindow(costs, { budget: bodyRows, index: focused });
259
+ // Indicators cost two rows; reserve them only when there is something to
260
+ // point at AND enough body left for them to be honest.
261
+ if ((window.above || window.below) && bodyRows >= 4) {
262
+ window = scrollWindow(costs, { budget: bodyRows - 2, index: focused });
263
+ }
264
+ if (window.above && lines.length < bodyRows) {
265
+ lines.push(indicatorRow(width, ` ↑ ${window.start} more`));
266
+ }
267
+ for (let index = window.start; index < window.end; index += 1) {
268
+ hits.push({ y: BODY_TOP + lines.length, index });
269
+ lines.push(scopeRow(index, focused, inner));
270
+ }
271
+ if (window.below && lines.length < bodyRows) {
272
+ lines.push(indicatorRow(width, ` ↓ ${rows.length - window.end} more`));
273
+ }
274
+ }
275
+ while (lines.length < bodyRows) lines.push(cardLine('', TONE, theme, width, metrics));
276
+ return lines.slice(0, bodyRows);
277
+ }
278
+
279
+ // S9: the focused scope's data directly above the footer. An empty row keeps
280
+ // the card's geometry stable when nothing was measured.
281
+ function detailRow(width) {
282
+ const scope = scopes()[clampedSelected()];
283
+ const detail = scope && scope.detail;
284
+ const text = detail === undefined ? '' : scopeDetailLine(detail, now);
285
+ if (text === '') return cardLine('', TONE, theme, width, metrics);
286
+ return cardLine(sanitize(text), TONE, theme, width, metrics, 'muted');
287
+ }
288
+
289
+ // S20: the legend gains the toggle only when there IS a second list, and its
290
+ // words name the destination. Same shape as every other entry: the key, then
291
+ // its description, one row, one option per key.
292
+ function footerEntries() {
293
+ if (lists.all === null) return FOOTER_ENTRIES;
294
+ const entries = FOOTER_ENTRIES.slice();
295
+ const destination = active === 'all' ? 'registered' : 'all';
296
+ entries.splice(1, 0, {
297
+ key: LIST_TOGGLE,
298
+ text: `${LIST_TOGGLE} ${destination}`,
299
+ short: `${LIST_TOGGLE} ${destination}`,
300
+ priority: TOGGLE_PRIORITY,
301
+ });
302
+ return entries;
303
+ }
304
+
305
+ function footerRow(inner) {
306
+ const legend = legendRow(theme, footerEntries(), inner, (text) => measure(helpers, text));
307
+ // The legend is already painted and already fits `inner`; the padding only
308
+ // fills the frame, and the host helpers measure it ANSI-aware.
309
+ return frameRow(padPlain(legend, inner));
310
+ }
311
+
312
+ function metricsWidth(text) {
313
+ return measure(helpers, text);
314
+ }
315
+
316
+ // The measurement seam the substrate takes: the same injected helpers the rows
317
+ // are measured with, so a paint and a clip can never disagree.
318
+ const metrics = {
319
+ visibleWidth: (text) => metricsWidth(text),
320
+ truncateToWidth(text, width, ellipsis) {
321
+ try {
322
+ if (typeof helpers.truncateToWidth === 'function') {
323
+ const cut = helpers.truncateToWidth(text, width, ellipsis);
324
+ if (typeof cut === 'string') return cut;
325
+ }
326
+ } catch {
327
+ // Fall through to the plain codepoint slice.
328
+ }
329
+ const chars = [...String(text)];
330
+ return chars.length > width
331
+ ? `${chars.slice(0, Math.max(0, width - 1)).join('')}${ellipsis === undefined ? '' : ellipsis}`
332
+ : String(text);
333
+ },
334
+ wrapTextWithAnsi(text, width) {
335
+ try {
336
+ if (typeof helpers.wrapTextWithAnsi === 'function') {
337
+ const rows = helpers.wrapTextWithAnsi(text, width);
338
+ if (Array.isArray(rows)) return rows;
339
+ }
340
+ } catch {
341
+ // Fall through to the plain codepoint split.
342
+ }
343
+ const chars = [...String(text)];
344
+ const rows = [];
345
+ for (let at = 0; at < chars.length; at += Math.max(1, width)) rows.push(chars.slice(at, at + Math.max(1, width)).join(''));
346
+ return rows.length > 0 ? rows : [''];
347
+ },
348
+ };
349
+
350
+ function render(width) {
351
+ const w = Number.isFinite(Number(width)) ? Math.floor(Number(width)) : 0;
352
+ if (w <= 0) {
353
+ pointerFrame = undefined;
354
+ return [];
355
+ }
356
+ const height = cardHeight();
357
+ if (height <= 0) {
358
+ pointerFrame = undefined;
359
+ return [];
360
+ }
361
+ if (w < MIN_WIDTH || height < MIN_CARD_HEIGHT) {
362
+ pointerFrame = undefined;
363
+ return [padPlain(clipPlain(`${TITLE} — enlarge the terminal`, w), w)];
364
+ }
365
+ const inner = cardInnerWidth(w);
366
+ const bodyRows = height - CHROME_ROWS;
367
+ const hits = [];
368
+ const body = scopeRows(w, inner, bodyRows, hits);
369
+ const lines = [
370
+ cardTop(CARD, theme, w, undefined, metrics),
371
+ cardLine('', TONE, theme, w, metrics),
372
+ ...body,
373
+ detailRow(w),
374
+ footerRow(inner),
375
+ cardBottom(TONE, theme, w, undefined, metrics),
376
+ ];
377
+ pointerFrame = completed || disposed ? undefined : {
378
+ width: w,
379
+ height: lines.length,
380
+ bodyTop: BODY_TOP,
381
+ bodyRows: body.length,
382
+ hits,
383
+ };
384
+ return lines;
385
+ }
386
+
387
+ function keyMatches(data, names) {
388
+ for (const name of names) {
389
+ try {
390
+ if (typeof helpers.matchesKey === 'function' && helpers.matchesKey(data, name)) return true;
391
+ } catch {
392
+ // A throwing matcher never matches; the string fallback decides below.
393
+ }
394
+ }
395
+ const text = typeof data === 'string' ? data : (data && (data.name || data.key)) || '';
396
+ return names.some((name) => text === name || (text === '\n' && name === 'enter'));
397
+ }
398
+
399
+ function isRelease(data) {
400
+ try {
401
+ return typeof helpers.isKeyRelease === 'function' && helpers.isKeyRelease(data) === true;
402
+ } catch {
403
+ return false;
404
+ }
405
+ }
406
+
407
+ function finish(action) {
408
+ if (completed || disposed) return;
409
+ const scope = action === 'select' ? scopes()[clampedSelected()] : undefined;
410
+ if (action === 'select' && !scope) return;
411
+ completed = true;
412
+ pointerFrame = undefined;
413
+ hovered = undefined;
414
+ if (scope === undefined) {
415
+ repaint();
416
+ if (onClose !== null) onClose();
417
+ return;
418
+ }
419
+ repaint();
420
+ // A throwing handoff must not leave a completed step with dead input.
421
+ try {
422
+ if (onSelect !== null) onSelect(scope);
423
+ } catch (error) {
424
+ if (onClose !== null) onClose();
425
+ throw error;
426
+ }
427
+ }
428
+
429
+ function handleInput(data) {
430
+ if (completed || disposed || isRelease(data)) return;
431
+ pointerFrame = undefined;
432
+ hovered = undefined;
433
+ // A bare line feed is both ctrl+j and, on non-Kitty terminals, a synonym for
434
+ // enter: the move keys are checked first so ctrl+j moves instead of choosing.
435
+ if (keyMatches(data, ['down', 'ctrl+j'])) {
436
+ if (moveSelection(1)) repaint();
437
+ return;
438
+ }
439
+ if (keyMatches(data, ['up', 'ctrl+k'])) {
440
+ if (moveSelection(-1)) repaint();
441
+ return;
442
+ }
443
+ // S20: the list toggle sits with the other navigation keys; a plain letter is
444
+ // free here because the step has no query to type into.
445
+ if (keyMatches(data, [LIST_TOGGLE])) {
446
+ if (toggleList()) repaint();
447
+ return;
448
+ }
449
+ if (keyMatches(data, ['escape', 'ctrl+c'])) {
450
+ finish('close');
451
+ return;
452
+ }
453
+ if (keyMatches(data, ['enter', 'return'])) finish('select');
454
+ }
455
+
456
+ // Mouse events are local to the last painted card, including its borders.
457
+ function handleMouse(event) {
458
+ if (completed || disposed || event === null || typeof event !== 'object') return undefined;
459
+ const moving = event.type === 'move' && event.button === 'none';
460
+ const frame = pointerFrame;
461
+ if (!frame || event.width !== frame.width || event.height !== frame.height) {
462
+ pointerFrame = undefined;
463
+ const changed = setHover(undefined);
464
+ if (changed) repaint();
465
+ return changed && moving ? { handled: true, render: true } : undefined;
466
+ }
467
+ const inside = event.x >= 2 && event.x < frame.width - 2 && event.y >= 0 && event.y < frame.height;
468
+ const inBody = inside && event.y >= frame.bodyTop && event.y < frame.bodyTop + frame.bodyRows;
469
+ const hit = inBody ? frame.hits.find((candidate) => candidate.y === event.y) : undefined;
470
+ if (moving) {
471
+ const changed = setHover(hit === undefined ? undefined : hit.index);
472
+ if (changed) repaint();
473
+ return { handled: true, render: changed };
474
+ }
475
+ if (event.type === 'wheel' && event.wheelDelta) {
476
+ if (!inside) return undefined;
477
+ const changed = moveSelection(Math.sign(Number(event.wheelDelta)));
478
+ const hoverChanged = setHover(undefined);
479
+ if (changed || hoverChanged) {
480
+ pointerFrame = undefined;
481
+ repaint();
482
+ }
483
+ return { handled: true, render: changed || hoverChanged };
484
+ }
485
+ if (event.button !== 'left' || hit === undefined) return undefined;
486
+ if (event.type === 'press') return { handled: true, render: false };
487
+ if (event.type !== 'click') return undefined;
488
+ selected = hit.index;
489
+ finish('select');
490
+ return { handled: true, render: true };
491
+ }
492
+
493
+ function invalidate() {
494
+ pointerFrame = undefined;
495
+ hovered = undefined;
496
+ }
497
+
498
+ function dispose() {
499
+ disposed = true;
500
+ pointerFrame = undefined;
501
+ hovered = undefined;
502
+ }
503
+
504
+ return { render, handleInput, handleMouse, invalidate, dispose };
505
+ }
506
+
507
+ module.exports = { createFilesScopeView, FOOTER_ENTRIES };