claudeup 4.17.0 → 4.18.0

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 (76) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/alias-parser.test.ts +317 -0
  3. package/src/__tests__/alias-shell-writer.test.ts +661 -0
  4. package/src/__tests__/alias-store.test.ts +86 -0
  5. package/src/__tests__/gitignore-fixer.test.ts +64 -1
  6. package/src/__tests__/gitignore-prerun.test.ts +2 -2
  7. package/src/__tests__/gitignore-service.test.ts +42 -0
  8. package/src/__tests__/marketplaces.test.ts +40 -0
  9. package/src/__tests__/plugin-manager-fallback.test.ts +120 -0
  10. package/src/__tests__/useGitignoreModal.test.ts +2 -2
  11. package/src/data/alias-flags.js +196 -0
  12. package/src/data/alias-flags.ts +291 -0
  13. package/src/data/gitignore-reasons.js +97 -0
  14. package/src/data/gitignore-reasons.ts +103 -0
  15. package/src/data/marketplaces.js +5 -3
  16. package/src/data/marketplaces.ts +5 -4
  17. package/src/services/alias-settings.js +51 -0
  18. package/src/services/alias-settings.ts +63 -0
  19. package/src/services/alias-shell-writer.js +764 -0
  20. package/src/services/alias-shell-writer.ts +873 -0
  21. package/src/services/alias-store.js +77 -0
  22. package/src/services/alias-store.ts +112 -0
  23. package/src/services/gitignore-fixer.js +70 -10
  24. package/src/services/gitignore-fixer.ts +76 -9
  25. package/src/services/gitignore-prerun.js +3 -3
  26. package/src/services/gitignore-prerun.ts +3 -3
  27. package/src/services/gitignore-service.js +20 -2
  28. package/src/services/gitignore-service.ts +23 -1
  29. package/src/services/marketplace-fetcher.js +96 -0
  30. package/src/services/marketplace-fetcher.ts +137 -0
  31. package/src/services/plugin-manager.js +6 -59
  32. package/src/services/plugin-manager.ts +16 -91
  33. package/src/services/skillsmp-client.js +29 -9
  34. package/src/services/skillsmp-client.ts +38 -8
  35. package/src/types/gitignore.ts +1 -1
  36. package/src/ui/App.js +10 -4
  37. package/src/ui/App.tsx +9 -3
  38. package/src/ui/components/TabBar.js +2 -1
  39. package/src/ui/components/TabBar.tsx +2 -1
  40. package/src/ui/components/layout/FooterHints.js +29 -0
  41. package/src/ui/components/layout/FooterHints.tsx +52 -0
  42. package/src/ui/components/layout/ScreenLayout.js +2 -1
  43. package/src/ui/components/layout/ScreenLayout.tsx +12 -3
  44. package/src/ui/components/layout/index.js +1 -0
  45. package/src/ui/components/layout/index.ts +5 -0
  46. package/src/ui/components/modals/SelectModal.js +8 -1
  47. package/src/ui/components/modals/SelectModal.tsx +12 -1
  48. package/src/ui/hooks/useGitignoreModal.js +7 -8
  49. package/src/ui/hooks/useGitignoreModal.ts +8 -9
  50. package/src/ui/renderers/gitignoreRenderers.js +36 -23
  51. package/src/ui/renderers/gitignoreRenderers.tsx +50 -41
  52. package/src/ui/screens/AliasScreen.js +1008 -0
  53. package/src/ui/screens/AliasScreen.tsx +1402 -0
  54. package/src/ui/screens/CliToolsScreen.js +6 -1
  55. package/src/ui/screens/CliToolsScreen.tsx +6 -1
  56. package/src/ui/screens/EnvVarsScreen.js +6 -1
  57. package/src/ui/screens/EnvVarsScreen.tsx +6 -1
  58. package/src/ui/screens/GitignoreScreen.js +189 -88
  59. package/src/ui/screens/GitignoreScreen.tsx +312 -132
  60. package/src/ui/screens/McpRegistryScreen.js +13 -2
  61. package/src/ui/screens/McpRegistryScreen.tsx +13 -2
  62. package/src/ui/screens/McpScreen.js +6 -1
  63. package/src/ui/screens/McpScreen.tsx +6 -1
  64. package/src/ui/screens/ModelSelectorScreen.js +8 -2
  65. package/src/ui/screens/ModelSelectorScreen.tsx +8 -2
  66. package/src/ui/screens/PluginsScreen.js +13 -2
  67. package/src/ui/screens/PluginsScreen.tsx +13 -2
  68. package/src/ui/screens/ProfilesScreen.js +8 -1
  69. package/src/ui/screens/ProfilesScreen.tsx +8 -1
  70. package/src/ui/screens/SkillsScreen.js +21 -4
  71. package/src/ui/screens/SkillsScreen.tsx +39 -5
  72. package/src/ui/screens/StatusLineScreen.js +7 -1
  73. package/src/ui/screens/StatusLineScreen.tsx +7 -1
  74. package/src/ui/screens/index.js +1 -0
  75. package/src/ui/screens/index.ts +1 -0
  76. package/src/ui/state/types.ts +4 -2
@@ -0,0 +1,1402 @@
1
+ import React, { useEffect, useState, useCallback, useMemo, useRef } from "react";
2
+ import type { ScrollBoxRenderable } from "@opentui/core";
3
+ import { useApp, useModal } from "../state/AppContext.js";
4
+ import { useKeyboardHandler } from "../hooks/useKeyboardHandler.js";
5
+ import { ScreenLayout } from "../components/layout/index.js";
6
+ import {
7
+ ALIAS_FLAGS,
8
+ FLAG_GROUPS,
9
+ type AliasFlag,
10
+ } from "../../data/alias-flags.js";
11
+ import {
12
+ defaultAliasConfig,
13
+ defaultValueFor,
14
+ validateAliasName,
15
+ DEFAULT_ALIAS_NAME,
16
+ type AliasConfig,
17
+ type FlagValue,
18
+ } from "../../services/alias-store.js";
19
+ import { loadAliasName, saveAliasName } from "../../services/alias-settings.js";
20
+ import {
21
+ detectShells,
22
+ renderArgs,
23
+ renderAlias,
24
+ validateConfig,
25
+ writeAliasToShell,
26
+ parseAliasFromRc,
27
+ type ShellTarget,
28
+ type FlagValidationIssue,
29
+ } from "../../services/alias-shell-writer.js";
30
+ import { readFile } from "node:fs/promises";
31
+ import { existsSync } from "node:fs";
32
+
33
+ interface FlagRow {
34
+ kind: "flag";
35
+ flag: AliasFlag;
36
+ value: FlagValue;
37
+ /** Pre-computed validation issue for this row, if any. */
38
+ issue?: FlagValidationIssue;
39
+ }
40
+
41
+ interface AliasNameRow {
42
+ kind: "alias-name";
43
+ value: string;
44
+ }
45
+
46
+ type Row = AliasNameRow | FlagRow;
47
+
48
+ /**
49
+ * Flat-list items for the windowed `<ScrollableList>`. Selectable rows
50
+ * (`alias-name`, `flag`) accept the cursor; presentation rows (`header`)
51
+ * render in-line so groups scroll with their flags, but `j`/`k` skip them.
52
+ *
53
+ * The shells legend is NOT in this list — it lives outside the scroll area
54
+ * as a fixed footer so it's always visible. Keeping non-selectable items
55
+ * trailing the scrollable list would let `↓ N more` lie about reachable
56
+ * content.
57
+ */
58
+ type Item =
59
+ | { kind: "alias-name"; row: AliasNameRow }
60
+ | { kind: "flag"; row: FlagRow }
61
+ | { kind: "header"; label: string };
62
+
63
+ function isSelectableItem(item: Item): boolean {
64
+ return item.kind === "alias-name" || item.kind === "flag";
65
+ }
66
+
67
+ /**
68
+ * View modes for the Alias screen.
69
+ *
70
+ * - `main`: the default list of all flags grouped by section.
71
+ * - `flag-detail`: a dedicated sub-screen for editing a single `text-list` or
72
+ * `multi-with-custom` flag's value. Has its own keybindings (`a` add, `d`
73
+ * delete, `space`/`enter` toggle, `h` back to main) so each row's primary
74
+ * action is unambiguous. Esc still exits the screen via the global handler.
75
+ *
76
+ * Other flag kinds (boolean, tri-state, select, text, optional-text) use
77
+ * modals because they're single-action — no benefit from a sub-screen.
78
+ */
79
+ type ViewMode = { kind: "main" } | { kind: "flag-detail"; flagId: string };
80
+
81
+ /**
82
+ * Items shown in the flag-detail sub-screen. Distinct from `Item` because the
83
+ * navigation context is different (one flag, not the whole catalog).
84
+ */
85
+ type DetailItem =
86
+ | { kind: "toggle"; enabled: boolean }
87
+ | { kind: "value"; index: number; text: string }
88
+ | { kind: "picklist"; token: string; picked: boolean }
89
+ | { kind: "custom"; index: number; text: string }
90
+ | { kind: "add" }
91
+ | { kind: "header"; label: string };
92
+
93
+ function isSelectableDetailItem(item: DetailItem): boolean {
94
+ return item.kind !== "header";
95
+ }
96
+
97
+ export function AliasScreen() {
98
+ const { state } = useApp();
99
+ const modal = useModal();
100
+
101
+ const [config, setConfig] = useState<AliasConfig | null>(null);
102
+ const [shells, setShells] = useState<ShellTarget[]>([]);
103
+ const [selectedIdx, setSelectedIdx] = useState(0);
104
+ const [error, setError] = useState<string | null>(null);
105
+ const [viewMode, setViewMode] = useState<ViewMode>({ kind: "main" });
106
+ const [detailSelectedIdx, setDetailSelectedIdx] = useState(0);
107
+ // Snapshot of the rc file's flag values as parsed on mount / write. Used
108
+ // to compute the dirty indicator: in-memory != rcSnapshot ⇒ unsaved.
109
+ const [rcSnapshot, setRcSnapshot] = useState<Record<string, FlagValue> | null>(
110
+ null,
111
+ );
112
+
113
+ // ── Load alias name + parse rc file + detect shells on mount ─────────
114
+ // The alias name is computer-wide and persisted in ~/.claude/settings.json.
115
+ // Flag values are read directly from the shell rc file's managed block —
116
+ // the rc file IS the source of truth so what the user sees matches what
117
+ // they're actually running.
118
+ useEffect(() => {
119
+ let cancelled = false;
120
+ (async () => {
121
+ try {
122
+ const [name, s] = await Promise.all([loadAliasName(), detectShells()]);
123
+ if (cancelled) return;
124
+
125
+ // Pick the same rc file we'd write to with `w` (default shell that
126
+ // exists; else first existing). If none exists, start from defaults.
127
+ const readTarget =
128
+ s.find((sh) => sh.isDefault && sh.exists) ??
129
+ s.find((sh) => sh.exists);
130
+
131
+ const base = defaultAliasConfig();
132
+ base.aliasName = name;
133
+
134
+ let parsedFlags: Record<string, FlagValue> | null = null;
135
+ if (readTarget && existsSync(readTarget.path)) {
136
+ try {
137
+ const rcText = await readFile(readTarget.path, "utf8");
138
+ const parsed = parseAliasFromRc(rcText);
139
+ if (parsed) {
140
+ parsedFlags = { ...base.flags, ...parsed.flags };
141
+ base.flags = parsedFlags;
142
+ // Honour an existing alias name from the rc if settings is at default.
143
+ // But the user's stored preference wins when set explicitly.
144
+ // (No-op here — `name` already loaded above.)
145
+ }
146
+ } catch {
147
+ // rc unreadable — fall back to defaults; not a hard error.
148
+ }
149
+ }
150
+
151
+ if (cancelled) return;
152
+ setConfig(base);
153
+ setShells(s);
154
+ setRcSnapshot(parsedFlags ?? base.flags);
155
+ } catch (err) {
156
+ if (cancelled) return;
157
+ setError(err instanceof Error ? err.message : String(err));
158
+ }
159
+ })();
160
+ return () => {
161
+ cancelled = true;
162
+ };
163
+ }, []);
164
+
165
+ // ── Build the flat list of items ──────────────────────────────────────
166
+ // Group headers and the shell legend live in the same flat array as
167
+ // alias-name + flag rows so that ScrollableList can window them together.
168
+ // Navigation skips non-selectable kinds.
169
+ const items: Item[] = useMemo(() => {
170
+ if (!config) return [];
171
+ const issues = validateConfig(config);
172
+ const issueMap = new Map(issues.map((i) => [i.flagId, i]));
173
+
174
+ const out: Item[] = [
175
+ {
176
+ kind: "alias-name",
177
+ row: { kind: "alias-name", value: config.aliasName },
178
+ },
179
+ ];
180
+
181
+ for (const group of FLAG_GROUPS) {
182
+ const groupFlags = ALIAS_FLAGS.filter((f) => f.group === group.id);
183
+ if (groupFlags.length === 0) continue;
184
+ out.push({ kind: "header", label: group.label });
185
+ for (const flag of groupFlags) {
186
+ out.push({
187
+ kind: "flag",
188
+ row: {
189
+ kind: "flag",
190
+ flag,
191
+ value: config.flags[flag.id] ?? defaultValueFor(flag),
192
+ issue: issueMap.get(flag.id),
193
+ },
194
+ });
195
+ }
196
+ }
197
+
198
+ return out;
199
+ }, [config]);
200
+
201
+ /** Indices in `items` that the cursor may land on. */
202
+ const selectableIndices = useMemo(
203
+ () => items.flatMap((it, i) => (isSelectableItem(it) ? [i] : [])),
204
+ [items],
205
+ );
206
+
207
+ // ── Flag-detail sub-screen items ──────────────────────────────────────
208
+ // Computed only when viewMode is flag-detail. Built off the flag's current
209
+ // value in `config.flags[flagId]`. Kinds vary by flag.kind (text-list vs
210
+ // multi-with-custom).
211
+ const detailFlag: AliasFlag | null =
212
+ viewMode.kind === "flag-detail"
213
+ ? ALIAS_FLAGS.find((f) => f.id === viewMode.flagId) ?? null
214
+ : null;
215
+ const detailValue: FlagValue | null =
216
+ detailFlag && config ? config.flags[detailFlag.id] ?? null : null;
217
+
218
+ const detailItems: DetailItem[] = useMemo(() => {
219
+ if (!detailFlag || !detailValue) return [];
220
+ if (detailValue.kind === "text-list") {
221
+ const out: DetailItem[] = [
222
+ { kind: "toggle", enabled: detailValue.enabled },
223
+ ];
224
+ if (detailValue.values.length > 0) {
225
+ out.push({ kind: "header", label: "Values" });
226
+ detailValue.values.forEach((v, i) =>
227
+ out.push({ kind: "value", index: i, text: v }),
228
+ );
229
+ }
230
+ out.push({ kind: "add" });
231
+ return out;
232
+ }
233
+ if (detailValue.kind === "multi-with-custom") {
234
+ const pickedSet = new Set(detailValue.picked);
235
+ const out: DetailItem[] = [
236
+ { kind: "toggle", enabled: detailValue.enabled },
237
+ ];
238
+ if ((detailFlag.picklist?.length ?? 0) > 0) {
239
+ out.push({ kind: "header", label: "Common filters" });
240
+ for (const tok of detailFlag.picklist!) {
241
+ out.push({ kind: "picklist", token: tok, picked: pickedSet.has(tok) });
242
+ }
243
+ }
244
+ if (detailValue.custom.length > 0) {
245
+ out.push({ kind: "header", label: "Custom tokens" });
246
+ detailValue.custom.forEach((v, i) =>
247
+ out.push({ kind: "custom", index: i, text: v }),
248
+ );
249
+ }
250
+ out.push({ kind: "add" });
251
+ return out;
252
+ }
253
+ return [];
254
+ }, [detailFlag, detailValue]);
255
+
256
+ const detailSelectableIndices = useMemo(
257
+ () => detailItems.flatMap((it, i) => (isSelectableDetailItem(it) ? [i] : [])),
258
+ [detailItems],
259
+ );
260
+
261
+ // Reset detail selection on entering / on items shape change.
262
+ useEffect(() => {
263
+ if (viewMode.kind !== "flag-detail") return;
264
+ if (detailSelectableIndices.length === 0) return;
265
+ if (!detailItems[detailSelectedIdx] || !isSelectableDetailItem(detailItems[detailSelectedIdx])) {
266
+ setDetailSelectedIdx(detailSelectableIndices[0]);
267
+ }
268
+ }, [viewMode, detailItems, detailSelectableIndices, detailSelectedIdx]);
269
+
270
+ // ── Helper: check whether a row is gated (requires unmet) ─────────────
271
+ const isGated = useCallback(
272
+ (flag: AliasFlag): boolean => {
273
+ if (!flag.requires || !config) return false;
274
+ return !isFlagEnabled(config.flags[flag.requires]);
275
+ },
276
+ [config],
277
+ );
278
+
279
+ const selectedItem = items[selectedIdx];
280
+ const selectedRow: Row | undefined =
281
+ selectedItem &&
282
+ (selectedItem.kind === "alias-name" || selectedItem.kind === "flag")
283
+ ? selectedItem.row
284
+ : undefined;
285
+
286
+ // Clamp selection: snap to the nearest selectable index whenever items shift.
287
+ useEffect(() => {
288
+ if (selectableIndices.length === 0) return;
289
+ if (!isSelectableItem(items[selectedIdx])) {
290
+ // Find the nearest selectable index <= selectedIdx (or the first one)
291
+ const fallback =
292
+ [...selectableIndices].reverse().find((i) => i <= selectedIdx) ??
293
+ selectableIndices[0];
294
+ setSelectedIdx(fallback);
295
+ } else if (selectedIdx >= items.length) {
296
+ setSelectedIdx(selectableIndices[selectableIndices.length - 1]);
297
+ }
298
+ }, [items, selectableIndices, selectedIdx]);
299
+
300
+ // ── Mutators ──────────────────────────────────────────────────────────
301
+ // Flag mutations stay in memory. Only the alias name is persisted (to
302
+ // ~/.claude/settings.json) — flag values reach disk only when the user
303
+ // explicitly presses `w` to write the shell rc file.
304
+ const persistAliasName = useCallback(
305
+ async (name: string) => {
306
+ try {
307
+ await saveAliasName(name);
308
+ } catch (err) {
309
+ await modal.message(
310
+ "Save failed",
311
+ err instanceof Error ? err.message : String(err),
312
+ "error",
313
+ );
314
+ }
315
+ },
316
+ [modal],
317
+ );
318
+
319
+ // Cascade dependencies on every mutation:
320
+ // - Enabling a flag → enable its `requires` chain (no modal nagging).
321
+ // - Disabling a flag → disable anything whose `requires === this flag id`,
322
+ // because the renderer would silently drop them otherwise and the UI
323
+ // would lie about what's going to be written.
324
+ const updateFlag = useCallback(
325
+ (id: string, next: FlagValue) => {
326
+ setConfig((prev) => {
327
+ if (!prev) return prev;
328
+ const flags = { ...prev.flags, [id]: next };
329
+ const target = ALIAS_FLAGS.find((f) => f.id === id);
330
+ if (!target) return { ...prev, flags };
331
+
332
+ const becomingEnabled = isFlagEnabled(next);
333
+
334
+ if (becomingEnabled) {
335
+ // Walk `requires` chain and force-enable each ancestor to its
336
+ // catalog-default-enabled state if it's currently off.
337
+ let cursor = target.requires;
338
+ const seen = new Set<string>();
339
+ while (cursor && !seen.has(cursor)) {
340
+ seen.add(cursor);
341
+ const dep = ALIAS_FLAGS.find((f) => f.id === cursor);
342
+ if (!dep) break;
343
+ if (!isFlagEnabled(flags[dep.id])) {
344
+ flags[dep.id] = enableDefault(dep);
345
+ }
346
+ cursor = dep.requires;
347
+ }
348
+ } else {
349
+ // Cascade-disable any flag that requires this one (one level deep is
350
+ // enough today; recurse defensively in case a chain emerges).
351
+ const queue = [id];
352
+ const seen = new Set<string>();
353
+ while (queue.length > 0) {
354
+ const cur = queue.shift()!;
355
+ if (seen.has(cur)) continue;
356
+ seen.add(cur);
357
+ for (const dependent of ALIAS_FLAGS) {
358
+ if (dependent.requires !== cur) continue;
359
+ if (!isFlagEnabled(flags[dependent.id])) continue;
360
+ flags[dependent.id] = disableValue(flags[dependent.id]);
361
+ queue.push(dependent.id);
362
+ }
363
+ }
364
+ }
365
+
366
+ return { ...prev, flags };
367
+ });
368
+ },
369
+ [],
370
+ );
371
+
372
+ const editAliasName = useCallback(async () => {
373
+ if (!config) return;
374
+ const v = await modal.input(
375
+ "Alias name",
376
+ `Shell alias name (default: "${DEFAULT_ALIAS_NAME}"). Letters, digits, _ or - only.`,
377
+ config.aliasName,
378
+ );
379
+ if (v === null) return;
380
+ const reason = validateAliasName(v);
381
+ if (reason) {
382
+ await modal.message("Invalid alias name", reason, "error");
383
+ return;
384
+ }
385
+ setConfig((prev) => (prev ? { ...prev, aliasName: v } : prev));
386
+ void persistAliasName(v);
387
+ }, [config, modal, persistAliasName]);
388
+
389
+ const editSelected = useCallback(async () => {
390
+ if (!selectedRow) return;
391
+ if (selectedRow.kind === "alias-name") {
392
+ await editAliasName();
393
+ return;
394
+ }
395
+ // No more "Requires another flag" modal — `updateFlag` auto-enables the
396
+ // `requires` chain when the user activates a gated flag.
397
+ //
398
+ // Collection editors live in a dedicated sub-screen, not a modal —
399
+ // each row gets its own keybindings (space toggle, a add, d delete)
400
+ // so there's no "what does enter do here?" overloading.
401
+ const k = selectedRow.value.kind;
402
+ if (k === "text-list" || k === "multi-with-custom") {
403
+ setDetailSelectedIdx(0);
404
+ setViewMode({ kind: "flag-detail", flagId: selectedRow.flag.id });
405
+ return;
406
+ }
407
+ const next = await editFlagValue(selectedRow.flag, selectedRow.value, modal);
408
+ if (next) updateFlag(selectedRow.flag.id, next);
409
+ }, [selectedRow, modal, updateFlag, editAliasName]);
410
+
411
+ const toggleSelected = useCallback(async () => {
412
+ if (!selectedRow) return;
413
+ if (selectedRow.kind === "alias-name") {
414
+ // No "toggle" semantic for the name — fall through to edit.
415
+ await editAliasName();
416
+ return;
417
+ }
418
+ // Only booleans and tri-states have a meaningful single-press cycle.
419
+ // Everything else (select, text, list, multi-with-custom) needs a value
420
+ // chooser; falling back to edit prevents the "I pressed space and got the
421
+ // first option without seeing the choices" footgun.
422
+ const k = selectedRow.value.kind;
423
+ if (k !== "boolean" && k !== "tri-state") {
424
+ await editSelected();
425
+ return;
426
+ }
427
+ const next = quickToggle(selectedRow.flag, selectedRow.value);
428
+ if (next) updateFlag(selectedRow.flag.id, next);
429
+ }, [selectedRow, updateFlag, editSelected, editAliasName]);
430
+
431
+ const handleWrite = useCallback(async () => {
432
+ if (!config) return;
433
+
434
+ // Pick the target shell automatically: default shell (matches $SHELL)
435
+ // if its rc file exists; otherwise the first existing rc file. Users
436
+ // who care about other shells edit them directly — the alias config
437
+ // itself is stored in ~/.claude and the user can run `w` again from
438
+ // a different $SHELL session if they want to mirror it.
439
+ const target =
440
+ shells.find((s) => s.isDefault && s.exists) ??
441
+ shells.find((s) => s.exists);
442
+
443
+ if (!target) {
444
+ await modal.message(
445
+ "No shells detected",
446
+ "No supported rc files found. Create ~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish first.",
447
+ "info",
448
+ );
449
+ return;
450
+ }
451
+
452
+ try {
453
+ const r = await writeAliasToShell(config, target);
454
+ await modal.message(
455
+ "Alias written",
456
+ `${r.action === "created" ? "Created" : "Updated"} ${r.path}\n\nRun \`source ${r.path}\` or open a new shell to use it.`,
457
+ "success",
458
+ );
459
+ setShells(await detectShells());
460
+ // We just wrote the rc file with current in-memory flag values, so
461
+ // the snapshot now matches in-memory state. Dirty indicator clears.
462
+ setRcSnapshot(config.flags);
463
+ } catch (err) {
464
+ await modal.message(
465
+ "Write failed",
466
+ err instanceof Error ? err.message : String(err),
467
+ "error",
468
+ );
469
+ }
470
+ }, [config, shells, modal]);
471
+
472
+ // ── Keyboard ──────────────────────────────────────────────────────────
473
+ // j/k navigate the selectable subset, jumping over headers/legend lines.
474
+ const moveSelection = useCallback(
475
+ (dir: 1 | -1) => {
476
+ if (selectableIndices.length === 0) return;
477
+ const cursorPos = selectableIndices.indexOf(selectedIdx);
478
+ const nextPos =
479
+ cursorPos === -1
480
+ ? // Currently parked on a non-selectable; move to first/last selectable.
481
+ dir === 1
482
+ ? 0
483
+ : selectableIndices.length - 1
484
+ : Math.min(
485
+ selectableIndices.length - 1,
486
+ Math.max(0, cursorPos + dir),
487
+ );
488
+ setSelectedIdx(selectableIndices[nextPos]);
489
+ },
490
+ [selectableIndices, selectedIdx],
491
+ );
492
+
493
+ // ── Flag-detail sub-screen mutators ───────────────────────────────────
494
+ const moveDetailSelection = useCallback(
495
+ (dir: 1 | -1) => {
496
+ if (detailSelectableIndices.length === 0) return;
497
+ const cursorPos = detailSelectableIndices.indexOf(detailSelectedIdx);
498
+ const nextPos =
499
+ cursorPos === -1
500
+ ? dir === 1
501
+ ? 0
502
+ : detailSelectableIndices.length - 1
503
+ : Math.min(
504
+ detailSelectableIndices.length - 1,
505
+ Math.max(0, cursorPos + dir),
506
+ );
507
+ setDetailSelectedIdx(detailSelectableIndices[nextPos]);
508
+ },
509
+ [detailSelectableIndices, detailSelectedIdx],
510
+ );
511
+
512
+ const handleDetailToggle = useCallback(() => {
513
+ if (!detailFlag || !detailValue) return;
514
+ const item = detailItems[detailSelectedIdx];
515
+ if (!item) return;
516
+ if (item.kind === "toggle") {
517
+ if (detailValue.kind === "text-list") {
518
+ updateFlag(detailFlag.id, { ...detailValue, enabled: !detailValue.enabled });
519
+ } else if (detailValue.kind === "multi-with-custom") {
520
+ updateFlag(detailFlag.id, { ...detailValue, enabled: !detailValue.enabled });
521
+ }
522
+ return;
523
+ }
524
+ if (item.kind === "picklist" && detailValue.kind === "multi-with-custom") {
525
+ const pickedSet = new Set(detailValue.picked);
526
+ const next = pickedSet.has(item.token)
527
+ ? detailValue.picked.filter((p) => p !== item.token)
528
+ : [...detailValue.picked, item.token];
529
+ updateFlag(detailFlag.id, {
530
+ ...detailValue,
531
+ enabled: true,
532
+ picked: next,
533
+ });
534
+ return;
535
+ }
536
+ if (item.kind === "add") {
537
+ void handleDetailAdd();
538
+ return;
539
+ }
540
+ // value / custom rows: enter does nothing destructive; use `d` to delete.
541
+ }, [detailFlag, detailValue, detailItems, detailSelectedIdx, updateFlag]);
542
+
543
+ const handleDetailAdd = useCallback(async () => {
544
+ if (!detailFlag || !detailValue) return;
545
+ if (detailValue.kind === "text-list") {
546
+ const v = await modal.input(`Add to ${detailFlag.flag}`, "Value");
547
+ if (v === null || v.length === 0) return;
548
+ updateFlag(detailFlag.id, {
549
+ ...detailValue,
550
+ enabled: true,
551
+ values: [...detailValue.values, v],
552
+ });
553
+ return;
554
+ }
555
+ if (detailValue.kind === "multi-with-custom") {
556
+ const v = await modal.input(
557
+ `Add custom token`,
558
+ 'Token (e.g. "router" or "!file" for negation)',
559
+ );
560
+ if (v === null || v.length === 0) return;
561
+ updateFlag(detailFlag.id, {
562
+ ...detailValue,
563
+ enabled: true,
564
+ custom: [...detailValue.custom, v],
565
+ });
566
+ return;
567
+ }
568
+ }, [detailFlag, detailValue, modal, updateFlag]);
569
+
570
+ const handleDetailDelete = useCallback(async () => {
571
+ if (!detailFlag || !detailValue) return;
572
+ const item = detailItems[detailSelectedIdx];
573
+ if (!item) return;
574
+ if (item.kind === "value" && detailValue.kind === "text-list") {
575
+ const target = detailValue.values[item.index];
576
+ const ok = await modal.confirm(
577
+ "Remove item?",
578
+ `Remove "${truncate(target, 60)}" from ${detailFlag.flag}?`,
579
+ );
580
+ if (!ok) return;
581
+ updateFlag(detailFlag.id, {
582
+ ...detailValue,
583
+ values: detailValue.values.filter((_, i) => i !== item.index),
584
+ });
585
+ return;
586
+ }
587
+ if (item.kind === "custom" && detailValue.kind === "multi-with-custom") {
588
+ const target = detailValue.custom[item.index];
589
+ const ok = await modal.confirm(
590
+ "Remove custom token?",
591
+ `Remove "${truncate(target, 60)}" from ${detailFlag.flag}?`,
592
+ );
593
+ if (!ok) return;
594
+ updateFlag(detailFlag.id, {
595
+ ...detailValue,
596
+ custom: detailValue.custom.filter((_, i) => i !== item.index),
597
+ });
598
+ return;
599
+ }
600
+ }, [detailFlag, detailValue, detailItems, detailSelectedIdx, modal, updateFlag]);
601
+
602
+ const exitDetail = useCallback(() => {
603
+ setViewMode({ kind: "main" });
604
+ setDetailSelectedIdx(0);
605
+ }, []);
606
+
607
+ // ── Keyboard handler ──────────────────────────────────────────────────
608
+ // Dispatches on viewMode so sub-screen keybindings don't leak into main.
609
+ useKeyboardHandler((input, key) => {
610
+ if (state.modal) return;
611
+
612
+ if (viewMode.kind === "flag-detail") {
613
+ if (input === "j" || key.downArrow) {
614
+ moveDetailSelection(1);
615
+ return;
616
+ }
617
+ if (input === "k" || key.upArrow) {
618
+ moveDetailSelection(-1);
619
+ return;
620
+ }
621
+ if (key.return || input === " " || key.name === "space") {
622
+ handleDetailToggle();
623
+ return;
624
+ }
625
+ if (input === "a") {
626
+ void handleDetailAdd();
627
+ return;
628
+ }
629
+ if (input === "d") {
630
+ void handleDetailDelete();
631
+ return;
632
+ }
633
+ if (input === "h") {
634
+ exitDetail();
635
+ return;
636
+ }
637
+ // Esc is handled by the global handler — it'll exit the entire screen
638
+ // back to plugins, which is fine. `h` is the documented "back to main".
639
+ return;
640
+ }
641
+
642
+ // Main mode.
643
+ if (input === "j" || key.downArrow) {
644
+ moveSelection(1);
645
+ return;
646
+ }
647
+ if (input === "k" || key.upArrow) {
648
+ moveSelection(-1);
649
+ return;
650
+ }
651
+ if (key.return || input === " " || key.name === "space") {
652
+ void toggleSelected();
653
+ return;
654
+ }
655
+ if (input === "e") {
656
+ void editSelected();
657
+ return;
658
+ }
659
+ if (input === "w") {
660
+ void handleWrite();
661
+ return;
662
+ }
663
+ if (input === "r") {
664
+ // Reset the selected row to its default.
665
+ if (!selectedRow) return;
666
+ if (selectedRow.kind === "alias-name") {
667
+ setConfig((prev) =>
668
+ prev ? { ...prev, aliasName: DEFAULT_ALIAS_NAME } : prev,
669
+ );
670
+ void persistAliasName(DEFAULT_ALIAS_NAME);
671
+ return;
672
+ }
673
+ // Flag reset stays in memory until `w`.
674
+ updateFlag(selectedRow.flag.id, defaultValueFor(selectedRow.flag));
675
+ }
676
+ });
677
+
678
+ // ── Render ────────────────────────────────────────────────────────────
679
+ const previewBlock = config ? renderAlias(config, "zsh").block : "";
680
+
681
+ // Native OpenTUI scrollboxes own the scroll math. We render every item
682
+ // into the scrollbox's content area and ask the renderable to scroll the
683
+ // selected child into view whenever the cursor moves. The previous JS-
684
+ // windowed approach (ScrollableList) overstrike-rendered rows when its
685
+ // height computation drifted off the actual panel size.
686
+ const mainScrollRef = useRef<ScrollBoxRenderable | null>(null);
687
+ const detailScrollRef = useRef<ScrollBoxRenderable | null>(null);
688
+
689
+ useEffect(() => {
690
+ mainScrollRef.current?.scrollChildIntoView(`alias-row-${selectedIdx}`);
691
+ }, [selectedIdx]);
692
+
693
+ useEffect(() => {
694
+ if (viewMode.kind !== "flag-detail") return;
695
+ detailScrollRef.current?.scrollChildIntoView(
696
+ `alias-detail-row-${detailSelectedIdx}`,
697
+ );
698
+ }, [detailSelectedIdx, viewMode]);
699
+
700
+ const inDetail = viewMode.kind === "flag-detail" && detailFlag && detailValue;
701
+
702
+ const mainListPanel = !config ? (
703
+ <text fg="gray">{error ? `Error: ${error}` : "Loading..."}</text>
704
+ ) : (
705
+ <box flexDirection="column" height="100%">
706
+ <scrollbox
707
+ ref={mainScrollRef}
708
+ scrollY={true}
709
+ scrollX={false}
710
+ flexGrow={1}
711
+ >
712
+ <box flexDirection="column">
713
+ {items.map((item, idx) => (
714
+ <box key={getItemKey(item)} id={`alias-row-${idx}`}>
715
+ {renderItem(item, idx === selectedIdx, isGated)}
716
+ </box>
717
+ ))}
718
+ </box>
719
+ </scrollbox>
720
+ </box>
721
+ );
722
+
723
+ const detailListPanel = inDetail ? (
724
+ <box flexDirection="column" height="100%">
725
+ <text fg="#7e57c2">
726
+ <strong>{detailFlag.flag}</strong>
727
+ </text>
728
+ <text fg="gray">{`(h to go back to all flags)`}</text>
729
+ <text> </text>
730
+ <scrollbox
731
+ ref={detailScrollRef}
732
+ scrollY={true}
733
+ scrollX={false}
734
+ flexGrow={1}
735
+ >
736
+ <box flexDirection="column">
737
+ {detailItems.map((item, idx) => (
738
+ <box key={getDetailItemKey(item, idx)} id={`alias-detail-row-${idx}`}>
739
+ {renderDetailItem(item, idx === detailSelectedIdx)}
740
+ </box>
741
+ ))}
742
+ </box>
743
+ </scrollbox>
744
+ </box>
745
+ ) : null;
746
+
747
+ const listPanel = inDetail ? detailListPanel : mainListPanel;
748
+
749
+ const mainDetailPanel = renderDetail(selectedRow, isGated, previewBlock);
750
+ const detailDetailPanel = inDetail
751
+ ? renderFlagDetailRightPane(detailFlag, detailValue, previewBlock)
752
+ : null;
753
+ const detailPanel = inDetail ? detailDetailPanel : mainDetailPanel;
754
+
755
+ const enabledCount = config
756
+ ? Object.values(config.flags).filter(isFlagEnabled).length
757
+ : 0;
758
+ const subtitle = inDetail
759
+ ? `editing ${detailFlag.flag}`
760
+ : config
761
+ ? `${enabledCount} flag${enabledCount === 1 ? "" : "s"} enabled`
762
+ : "";
763
+
764
+ // Resolve the rc-file name we'd actually write to (basename of the chosen
765
+ // shell's path). Falls back to "shell" if no shells were detected — UI
766
+ // surface mirrors the runtime behavior of `handleWrite`.
767
+ const writeTarget =
768
+ shells.find((s) => s.isDefault && s.exists) ??
769
+ shells.find((s) => s.exists);
770
+ const writeLabel = writeTarget
771
+ ? writeTarget.path.split("/").pop() || "shell"
772
+ : "shell";
773
+
774
+ // Dirty when current in-memory flags differ from the rc-file snapshot.
775
+ // Initial mount sets rcSnapshot from the parsed rc, so this is false
776
+ // until the user edits.
777
+ const dirty = config
778
+ ? rcSnapshot !== null && !shallowFlagsEqual(config.flags, rcSnapshot)
779
+ : false;
780
+
781
+ const footerHints = inDetail
782
+ ? renderFooterHints([
783
+ { keys: ["↑", "↓"], label: "move" },
784
+ { keys: ["space"], label: "toggle" },
785
+ { keys: ["a"], label: "add" },
786
+ { keys: ["d"], label: "delete" },
787
+ { keys: ["h"], label: "back" },
788
+ ])
789
+ : renderFooterHints([
790
+ { keys: ["↑", "↓"], label: "move" },
791
+ { keys: ["space"], label: "toggle" },
792
+ { keys: ["e"], label: "edit" },
793
+ { keys: ["r"], label: "reset" },
794
+ {
795
+ keys: ["w"],
796
+ label: `${dirty ? "*" : ""}write to ${writeLabel}`,
797
+ },
798
+ ]);
799
+
800
+ return (
801
+ <ScreenLayout
802
+ title="Claude Alias"
803
+ subtitle={subtitle}
804
+ currentScreen="alias"
805
+ footerHints={footerHints}
806
+ listPanel={listPanel}
807
+ detailPanel={detailPanel}
808
+ />
809
+ );
810
+ }
811
+
812
+ // ─── Helpers ────────────────────────────────────────────────────────────
813
+
814
+ /**
815
+ * Compare two flag maps for value equality. We render-then-diff via JSON
816
+ * because FlagValues are small, fully serialisable, and order-insensitive
817
+ * inside the maps. A property-by-property deep-equal would be more efficient
818
+ * but the maps have ~14 entries — diff cost is negligible.
819
+ */
820
+ function shallowFlagsEqual(
821
+ a: Record<string, FlagValue>,
822
+ b: Record<string, FlagValue>,
823
+ ): boolean {
824
+ const aKeys = Object.keys(a).sort();
825
+ const bKeys = Object.keys(b).sort();
826
+ if (aKeys.length !== bKeys.length) return false;
827
+ for (let i = 0; i < aKeys.length; i++) {
828
+ if (aKeys[i] !== bKeys[i]) return false;
829
+ }
830
+ for (const k of aKeys) {
831
+ if (JSON.stringify(a[k]) !== JSON.stringify(b[k])) return false;
832
+ }
833
+ return true;
834
+ }
835
+
836
+ function isFlagEnabled(value: FlagValue | undefined): boolean {
837
+ if (!value) return false;
838
+ switch (value.kind) {
839
+ case "boolean":
840
+ return value.enabled;
841
+ case "tri-state":
842
+ return value.state !== "unset";
843
+ case "select":
844
+ case "text":
845
+ case "optional-text":
846
+ case "text-list":
847
+ case "multi-with-custom":
848
+ return value.enabled;
849
+ }
850
+ }
851
+
852
+ /**
853
+ * Produce an "enabled by default" value for a flag — what we'd give it when
854
+ * the user activates a dependent that needs this one on. For booleans this
855
+ * is just `enabled: true`; for kinds that carry a value (select/text/etc.)
856
+ * we set `enabled: true` with the catalog default value, leaving the user
857
+ * free to refine it after.
858
+ */
859
+ function enableDefault(flag: AliasFlag): FlagValue {
860
+ switch (flag.kind) {
861
+ case "boolean":
862
+ return { kind: "boolean", enabled: true };
863
+ case "tri-state":
864
+ return { kind: "tri-state", state: "on" };
865
+ case "select":
866
+ return {
867
+ kind: "select",
868
+ enabled: true,
869
+ value: flag.options?.[0]?.value ?? "",
870
+ };
871
+ case "text":
872
+ return { kind: "text", enabled: true, value: "" };
873
+ case "optional-text":
874
+ return { kind: "optional-text", enabled: true, value: "" };
875
+ case "text-list":
876
+ return {
877
+ kind: "text-list",
878
+ enabled: true,
879
+ values: [...(flag.defaultValues ?? [])],
880
+ };
881
+ case "multi-with-custom":
882
+ return {
883
+ kind: "multi-with-custom",
884
+ enabled: true,
885
+ picked: [],
886
+ custom: [...(flag.defaultValues ?? [])],
887
+ };
888
+ }
889
+ }
890
+
891
+ /**
892
+ * Disable a flag while preserving any values the user has already entered.
893
+ * Used by the cascade-disable path: turning off `--worktree` shouldn't lose
894
+ * the user's `--tmux=classic` choice — only re-enable the dependency and
895
+ * the value reappears.
896
+ */
897
+ function disableValue(value: FlagValue): FlagValue {
898
+ switch (value.kind) {
899
+ case "boolean":
900
+ return { kind: "boolean", enabled: false };
901
+ case "tri-state":
902
+ return { kind: "tri-state", state: "unset" };
903
+ case "select":
904
+ case "text":
905
+ case "optional-text":
906
+ case "text-list":
907
+ case "multi-with-custom":
908
+ return { ...value, enabled: false };
909
+ }
910
+ }
911
+
912
+ function summarizeValue(value: FlagValue): string {
913
+ switch (value.kind) {
914
+ case "boolean":
915
+ return value.enabled ? "on" : "off";
916
+ case "tri-state":
917
+ return value.state;
918
+ case "select":
919
+ return value.enabled ? value.value || "(bare)" : "off";
920
+ case "text":
921
+ case "optional-text":
922
+ return value.enabled
923
+ ? value.value
924
+ ? truncate(value.value, 32)
925
+ : "(empty)"
926
+ : "off";
927
+ case "text-list":
928
+ return value.enabled ? `${value.values.length} item(s)` : "off";
929
+ case "multi-with-custom": {
930
+ if (!value.enabled) return "off";
931
+ const total = new Set([...value.picked, ...value.custom]).size;
932
+ return total === 0 ? "(no filter)" : `${total} token(s)`;
933
+ }
934
+ }
935
+ }
936
+
937
+ function truncate(s: string, n: number): string {
938
+ return s.length <= n ? s : s.slice(0, n - 1) + "…";
939
+ }
940
+
941
+ /**
942
+ * Quick toggle (enter/space). For boolean → flip. For tri-state → cycle
943
+ * unset → on → off → unset. For others, just flip the `enabled` field; the
944
+ * user must press `e` to set the value.
945
+ */
946
+ function quickToggle(_flag: AliasFlag, value: FlagValue): FlagValue | null {
947
+ switch (value.kind) {
948
+ case "boolean":
949
+ return { kind: "boolean", enabled: !value.enabled };
950
+ case "tri-state": {
951
+ const next: "unset" | "on" | "off" =
952
+ value.state === "unset" ? "on" : value.state === "on" ? "off" : "unset";
953
+ return { kind: "tri-state", state: next };
954
+ }
955
+ case "select":
956
+ return { ...value, enabled: !value.enabled };
957
+ case "text":
958
+ case "optional-text":
959
+ return { ...value, enabled: !value.enabled };
960
+ case "text-list":
961
+ return { ...value, enabled: !value.enabled };
962
+ case "multi-with-custom":
963
+ return { ...value, enabled: !value.enabled };
964
+ }
965
+ }
966
+
967
+ /**
968
+ * `e` — edit the value. Modal flow varies by flag kind.
969
+ */
970
+ async function editFlagValue(
971
+ flag: AliasFlag,
972
+ value: FlagValue,
973
+ modal: ReturnType<typeof useModal>,
974
+ ): Promise<FlagValue | null> {
975
+ switch (value.kind) {
976
+ case "boolean":
977
+ return { kind: "boolean", enabled: !value.enabled };
978
+
979
+ case "tri-state": {
980
+ const choice = await modal.select(
981
+ flag.label,
982
+ flag.description,
983
+ [
984
+ { label: "Unset (no opinion)", value: "unset" },
985
+ { label: `On (${flag.flag})`, value: "on" },
986
+ ...(flag.triStateOff
987
+ ? [{ label: `Off (${flag.triStateOff})`, value: "off" }]
988
+ : []),
989
+ ],
990
+ ["unset", "on", "off"].indexOf(value.state),
991
+ );
992
+ if (!choice) return null;
993
+ return { kind: "tri-state", state: choice as "unset" | "on" | "off" };
994
+ }
995
+
996
+ case "select": {
997
+ const opts = flag.options ?? [];
998
+ const choice = await modal.select(
999
+ flag.label,
1000
+ flag.description,
1001
+ [
1002
+ { label: "(disable)", value: "__off__" },
1003
+ ...opts.map((o) => ({ label: o.label, value: `v:${o.value}` })),
1004
+ ],
1005
+ value.enabled
1006
+ ? Math.max(0, opts.findIndex((o) => o.value === value.value)) + 1
1007
+ : 0,
1008
+ );
1009
+ if (choice === null) return null;
1010
+ if (choice === "__off__") return { ...value, enabled: false };
1011
+ return {
1012
+ kind: "select",
1013
+ enabled: true,
1014
+ value: choice.replace(/^v:/, ""),
1015
+ };
1016
+ }
1017
+
1018
+ case "text": {
1019
+ // Lead with a 2-option chooser so the user can disable without going
1020
+ // through `r` reset. Selecting "Set value" jumps to the input modal.
1021
+ const action = await modal.select(
1022
+ flag.label,
1023
+ flag.description,
1024
+ value.enabled
1025
+ ? [
1026
+ { label: `Edit value (current: "${truncate(value.value, 32)}")`, value: "__edit__" },
1027
+ { label: "Disable flag", value: "__off__" },
1028
+ ]
1029
+ : [
1030
+ { label: "Enable with value...", value: "__edit__" },
1031
+ { label: "Keep disabled", value: "__off__" },
1032
+ ],
1033
+ 0,
1034
+ );
1035
+ if (action === null) return null;
1036
+ if (action === "__off__") {
1037
+ return { kind: "text", enabled: false, value: value.value };
1038
+ }
1039
+ const v = await modal.input(flag.label, flag.description, value.value);
1040
+ if (v === null) return null;
1041
+ return { kind: "text", enabled: v.length > 0, value: v };
1042
+ }
1043
+
1044
+ case "optional-text": {
1045
+ // Three-option lead: edit value, bare (no value), or disable.
1046
+ const action = await modal.select(
1047
+ flag.label,
1048
+ flag.description,
1049
+ [
1050
+ {
1051
+ label: value.enabled && value.value
1052
+ ? `Edit value (current: "${truncate(value.value, 32)}")`
1053
+ : "Set value...",
1054
+ value: "__edit__",
1055
+ },
1056
+ { label: "Bare flag (no value)", value: "__bare__" },
1057
+ { label: "Disable flag", value: "__off__" },
1058
+ ],
1059
+ value.enabled ? (value.value ? 0 : 1) : 2,
1060
+ );
1061
+ if (action === null) return null;
1062
+ if (action === "__off__") {
1063
+ return { kind: "optional-text", enabled: false, value: value.value };
1064
+ }
1065
+ if (action === "__bare__") {
1066
+ return { kind: "optional-text", enabled: true, value: "" };
1067
+ }
1068
+ const v = await modal.input(
1069
+ flag.label,
1070
+ flag.description,
1071
+ value.value,
1072
+ );
1073
+ if (v === null) return null;
1074
+ return { kind: "optional-text", enabled: true, value: v };
1075
+ }
1076
+
1077
+ case "text-list":
1078
+ case "multi-with-custom":
1079
+ // These kinds are routed to the flag-detail sub-screen by
1080
+ // `editSelected`, never to this modal-based editor. If you see this
1081
+ // throw, the caller bypassed the routing in editSelected — fix that
1082
+ // instead of re-introducing the modal cascade.
1083
+ throw new Error(
1084
+ `${flag.kind} flags use the flag-detail sub-screen, not the modal editor`,
1085
+ );
1086
+ }
1087
+ }
1088
+
1089
+
1090
+ // ─── Render helpers ─────────────────────────────────────────────────────
1091
+
1092
+ /**
1093
+ * The alias-name row sits above all flag groups. We render it with a clear
1094
+ * "Alias:" label and the value styled as the editable field, plus an `[edit]`
1095
+ * hint, so it doesn't blend in with the flag rows below.
1096
+ */
1097
+ function renderAliasNameRow(name: string, selected: boolean): React.ReactNode {
1098
+ if (selected) {
1099
+ return (
1100
+ <box key="alias-name-row" flexDirection="row">
1101
+ <text bg="#7e57c2" fg="white">
1102
+ {`▶ Alias name: ${name} [enter to edit]`}
1103
+ </text>
1104
+ </box>
1105
+ );
1106
+ }
1107
+ return (
1108
+ <text key="alias-name-row">
1109
+ <span fg="gray">{" Alias name: "}</span>
1110
+ <span fg="cyan">
1111
+ <strong>{name}</strong>
1112
+ </span>
1113
+ <span fg="gray">{" [enter to edit]"}</span>
1114
+ </text>
1115
+ );
1116
+ }
1117
+
1118
+ /** Stable key per item for the windowed list. */
1119
+ function getItemKey(item: Item): string {
1120
+ switch (item.kind) {
1121
+ case "alias-name":
1122
+ return "alias-name";
1123
+ case "flag":
1124
+ return `flag:${item.row.flag.id}`;
1125
+ case "header":
1126
+ return `header:${item.label}`;
1127
+ }
1128
+ }
1129
+
1130
+ /** Dispatch render for one flat-list item. */
1131
+ function renderItem(
1132
+ item: Item,
1133
+ isSelected: boolean,
1134
+ isGated: (f: AliasFlag) => boolean,
1135
+ ): React.ReactNode {
1136
+ switch (item.kind) {
1137
+ case "alias-name":
1138
+ return renderAliasNameRow(item.row.value, isSelected);
1139
+ case "flag":
1140
+ return renderRow(item.row, isSelected, isGated(item.row.flag));
1141
+ case "header":
1142
+ return <text fg="#7e57c2">{item.label}</text>;
1143
+ }
1144
+ }
1145
+
1146
+
1147
+ function renderRow(
1148
+ row: FlagRow,
1149
+ selected: boolean,
1150
+ gated: boolean,
1151
+ ): React.ReactNode {
1152
+ const enabled = isFlagEnabled(row.value);
1153
+ const marker = enabled ? "[x]" : "[ ]";
1154
+ const issueMark = row.issue ? " ⚠" : "";
1155
+ const fg = gated ? "gray" : enabled ? "white" : "gray";
1156
+ // Only show a value suffix when the flag carries non-redundant detail.
1157
+ // Boolean / tri-state state is fully conveyed by the checkbox.
1158
+ const detail = enabledDetail(row.value);
1159
+ const detailSuffix = detail ? ` ${detail}` : "";
1160
+ const line = `${selected ? "▶ " : " "}${marker} ${row.flag.flag}${detailSuffix}${issueMark}`;
1161
+ if (selected) {
1162
+ return (
1163
+ <text key={row.flag.id} bg="#7e57c2" fg="white">
1164
+ {line}
1165
+ </text>
1166
+ );
1167
+ }
1168
+ return (
1169
+ <text key={row.flag.id} fg={fg}>
1170
+ {line}
1171
+ </text>
1172
+ );
1173
+ }
1174
+
1175
+ /**
1176
+ * Per-row trailing detail. Returns "" for booleans and tri-states (their state
1177
+ * is already in the checkbox) and for disabled flags (no need to advertise an
1178
+ * unset value). Non-empty for flags that carry an extra value the checkbox
1179
+ * doesn't capture.
1180
+ */
1181
+ function enabledDetail(value: FlagValue): string {
1182
+ switch (value.kind) {
1183
+ case "boolean":
1184
+ case "tri-state":
1185
+ return "";
1186
+ case "select":
1187
+ if (!value.enabled) return "";
1188
+ return value.value === "" ? "(bare)" : value.value;
1189
+ case "text":
1190
+ case "optional-text":
1191
+ if (!value.enabled) return "";
1192
+ return value.value ? truncate(value.value, 24) : "(empty)";
1193
+ case "text-list":
1194
+ if (!value.enabled) return "";
1195
+ return value.values.length === 0 ? "(empty)" : `${value.values.length}`;
1196
+ case "multi-with-custom": {
1197
+ if (!value.enabled) return "";
1198
+ const total = new Set([...value.picked, ...value.custom]).size;
1199
+ return total === 0 ? "(no filter)" : `${total} token(s)`;
1200
+ }
1201
+ }
1202
+ }
1203
+
1204
+ function renderDetail(
1205
+ row: Row | undefined,
1206
+ isGated: (f: AliasFlag) => boolean,
1207
+ previewBlock: string,
1208
+ ): React.ReactNode {
1209
+ if (!row) {
1210
+ return <text fg="gray">No row selected.</text>;
1211
+ }
1212
+
1213
+ const headerAndBody =
1214
+ row.kind === "alias-name"
1215
+ ? renderAliasNameDetail(row)
1216
+ : renderFlagDetail(row, isGated);
1217
+
1218
+ // Preview is the only "global" element kept in the detail panel — it
1219
+ // updates as the user toggles, which is the strongest visual feedback for
1220
+ // "what alias am I building". Shells live in the list-panel legend.
1221
+ return (
1222
+ <box flexDirection="column">
1223
+ {headerAndBody}
1224
+ <text> </text>
1225
+ <text fg="gray">Preview (zsh/bash):</text>
1226
+ <text fg="white">{firstLine(previewBlock, 1)}</text>
1227
+ </box>
1228
+ );
1229
+ }
1230
+
1231
+ function renderAliasNameDetail(row: AliasNameRow): React.ReactNode {
1232
+ return (
1233
+ <box flexDirection="column">
1234
+ <text fg="white">
1235
+ <strong>Alias name</strong>
1236
+ </text>
1237
+ <text fg="gray">
1238
+ Shell name for the wrapped claude command. Default: "c". Letters,
1239
+ digits, _ or - only.
1240
+ </text>
1241
+ <text> </text>
1242
+ <text>
1243
+ <span fg="gray">Current: </span>
1244
+ <span fg="white">
1245
+ <strong>{row.value}</strong>
1246
+ </span>
1247
+ </text>
1248
+ <text> </text>
1249
+ <text fg="gray">Enter to rename. r resets to "c".</text>
1250
+ </box>
1251
+ );
1252
+ }
1253
+
1254
+ function renderFlagDetail(
1255
+ row: FlagRow,
1256
+ isGated: (f: AliasFlag) => boolean,
1257
+ ): React.ReactNode {
1258
+ const gated = isGated(row.flag);
1259
+ return (
1260
+ <box flexDirection="column">
1261
+ <text fg="white">
1262
+ <strong>{row.flag.flag}</strong>
1263
+ </text>
1264
+ <text fg="gray">{row.flag.description}</text>
1265
+ <text> </text>
1266
+ <text>
1267
+ <span fg="gray">State: </span>
1268
+ <span fg="white">{summarizeValue(row.value)}</span>
1269
+ </text>
1270
+ {gated && (
1271
+ <text fg="gray">
1272
+ {`Depends on --${row.flag.requires}. Enabling this will turn that on too.`}
1273
+ </text>
1274
+ )}
1275
+ {row.issue && (
1276
+ <text fg="yellow">{`Conflict: ${row.issue.reason}`}</text>
1277
+ )}
1278
+ </box>
1279
+ );
1280
+ }
1281
+
1282
+ function firstLine(block: string, skip: number): string {
1283
+ const lines = block.split("\n");
1284
+ return lines[skip] ?? "";
1285
+ }
1286
+
1287
+ /**
1288
+ * Render footer hints as keycap badges + labels. Keys are rendered with an
1289
+ * inverted color (white bg, dark fg) so they read as visual chips rather
1290
+ * than letters dropped into prose. Multi-key bindings (`↑↓`) render as one
1291
+ * combined badge.
1292
+ */
1293
+ function renderFooterHints(
1294
+ hints: Array<{ keys: string[]; label: string }>,
1295
+ ): React.ReactNode {
1296
+ const nodes: React.ReactNode[] = [];
1297
+ hints.forEach((hint, i) => {
1298
+ if (i > 0) {
1299
+ nodes.push(
1300
+ <span key={`gap-${i}`} fg="gray">
1301
+ {" "}
1302
+ </span>,
1303
+ );
1304
+ }
1305
+ nodes.push(
1306
+ <span key={`k-${i}`} bg="#3f3f46" fg="#fafafa">
1307
+ {` ${hint.keys.join("")} `}
1308
+ </span>,
1309
+ );
1310
+ nodes.push(
1311
+ <span key={`l-${i}`} fg="gray">
1312
+ {` ${hint.label}`}
1313
+ </span>,
1314
+ );
1315
+ });
1316
+ return <text>{nodes}</text>;
1317
+ }
1318
+
1319
+ // ─── Flag-detail sub-screen render helpers ─────────────────────────────
1320
+
1321
+ function getDetailItemKey(item: DetailItem, idx: number): string {
1322
+ switch (item.kind) {
1323
+ case "toggle":
1324
+ return "toggle";
1325
+ case "value":
1326
+ return `value:${item.index}`;
1327
+ case "picklist":
1328
+ return `pick:${item.token}`;
1329
+ case "custom":
1330
+ return `custom:${item.index}`;
1331
+ case "add":
1332
+ return "add";
1333
+ case "header":
1334
+ return `header:${item.label}:${idx}`;
1335
+ }
1336
+ }
1337
+
1338
+ function renderDetailItem(
1339
+ item: DetailItem,
1340
+ isSelected: boolean,
1341
+ ): React.ReactNode {
1342
+ if (item.kind === "header") {
1343
+ return <text fg="#7e57c2">{item.label}</text>;
1344
+ }
1345
+ const cursor = isSelected ? "▶ " : " ";
1346
+ let body: string;
1347
+ switch (item.kind) {
1348
+ case "toggle":
1349
+ body = `[${item.enabled ? "x" : " "}] Enable flag`;
1350
+ break;
1351
+ case "value":
1352
+ body = item.text;
1353
+ break;
1354
+ case "picklist":
1355
+ body = `[${item.picked ? "x" : " "}] ${item.token}`;
1356
+ break;
1357
+ case "custom":
1358
+ body = item.text;
1359
+ break;
1360
+ case "add":
1361
+ body = "+ Add new...";
1362
+ break;
1363
+ }
1364
+ const line = `${cursor}${body}`;
1365
+ if (isSelected) {
1366
+ return (
1367
+ <text bg="#7e57c2" fg="white">
1368
+ {line}
1369
+ </text>
1370
+ );
1371
+ }
1372
+ return <text fg="white">{line}</text>;
1373
+ }
1374
+
1375
+ function renderFlagDetailRightPane(
1376
+ flag: AliasFlag,
1377
+ value: FlagValue,
1378
+ previewBlock: string,
1379
+ ): React.ReactNode {
1380
+ return (
1381
+ <box flexDirection="column">
1382
+ <text fg="white">
1383
+ <strong>{flag.flag}</strong>
1384
+ </text>
1385
+ <text fg="gray">{flag.description}</text>
1386
+ <text> </text>
1387
+ <text>
1388
+ <span fg="gray">State: </span>
1389
+ <span fg="white">{summarizeValue(value)}</span>
1390
+ </text>
1391
+ <text> </text>
1392
+ <text fg="gray">Preview (zsh/bash):</text>
1393
+ <text fg="white">{firstLine(previewBlock, 1)}</text>
1394
+ <text> </text>
1395
+ <text fg="gray">Keys:</text>
1396
+ <text fg="white"> space/enter toggle the highlighted row</text>
1397
+ <text fg="white"> a add a new value</text>
1398
+ <text fg="white"> d delete the highlighted item</text>
1399
+ <text fg="white"> h back to flag list</text>
1400
+ </box>
1401
+ );
1402
+ }