claudeup 4.17.0 → 4.19.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 (86) hide show
  1. package/package.json +1 -1
  2. package/src/__tests__/alias-adopt.test.ts +364 -0
  3. package/src/__tests__/alias-parser.test.ts +409 -0
  4. package/src/__tests__/alias-shell-writer.test.ts +668 -0
  5. package/src/__tests__/alias-store.test.ts +163 -0
  6. package/src/__tests__/gitignore-fixer.test.ts +64 -1
  7. package/src/__tests__/gitignore-prerun.test.ts +2 -2
  8. package/src/__tests__/gitignore-service.test.ts +42 -0
  9. package/src/__tests__/marketplaces.test.ts +40 -0
  10. package/src/__tests__/plugin-manager-fallback.test.ts +120 -0
  11. package/src/__tests__/plugin-setup.test.ts +111 -0
  12. package/src/__tests__/useGitignoreModal.test.ts +2 -2
  13. package/src/data/alias-flags.js +205 -0
  14. package/src/data/alias-flags.ts +301 -0
  15. package/src/data/gitignore-reasons.js +97 -0
  16. package/src/data/gitignore-reasons.ts +103 -0
  17. package/src/data/marketplaces.js +5 -3
  18. package/src/data/marketplaces.ts +5 -4
  19. package/src/services/alias-settings.js +51 -0
  20. package/src/services/alias-settings.ts +63 -0
  21. package/src/services/alias-shell-writer.js +1018 -0
  22. package/src/services/alias-shell-writer.ts +1247 -0
  23. package/src/services/alias-store.js +129 -0
  24. package/src/services/alias-store.ts +172 -0
  25. package/src/services/gitignore-fixer.js +70 -10
  26. package/src/services/gitignore-fixer.ts +76 -9
  27. package/src/services/gitignore-prerun.js +3 -3
  28. package/src/services/gitignore-prerun.ts +3 -3
  29. package/src/services/gitignore-service.js +20 -2
  30. package/src/services/gitignore-service.ts +23 -1
  31. package/src/services/marketplace-fetcher.js +96 -0
  32. package/src/services/marketplace-fetcher.ts +137 -0
  33. package/src/services/plugin-manager.js +6 -59
  34. package/src/services/plugin-manager.ts +16 -91
  35. package/src/services/plugin-setup.js +59 -4
  36. package/src/services/plugin-setup.ts +61 -4
  37. package/src/services/skillsmp-client.js +29 -9
  38. package/src/services/skillsmp-client.ts +38 -8
  39. package/src/types/gitignore.ts +1 -1
  40. package/src/ui/App.js +26 -11
  41. package/src/ui/App.tsx +25 -10
  42. package/src/ui/components/FlagDetailEditor.js +0 -0
  43. package/src/ui/components/FlagDetailEditor.tsx +0 -0
  44. package/src/ui/components/TabBar.js +2 -1
  45. package/src/ui/components/TabBar.tsx +2 -1
  46. package/src/ui/components/layout/FooterHints.js +29 -0
  47. package/src/ui/components/layout/FooterHints.tsx +52 -0
  48. package/src/ui/components/layout/ScreenLayout.js +2 -1
  49. package/src/ui/components/layout/ScreenLayout.tsx +12 -3
  50. package/src/ui/components/layout/index.js +1 -0
  51. package/src/ui/components/layout/index.ts +5 -0
  52. package/src/ui/components/modals/ConfirmModal.js +1 -1
  53. package/src/ui/components/modals/ConfirmModal.tsx +1 -1
  54. package/src/ui/components/modals/SelectModal.js +8 -1
  55. package/src/ui/components/modals/SelectModal.tsx +12 -1
  56. package/src/ui/hooks/useGitignoreModal.js +7 -8
  57. package/src/ui/hooks/useGitignoreModal.ts +8 -9
  58. package/src/ui/renderers/gitignoreRenderers.js +36 -23
  59. package/src/ui/renderers/gitignoreRenderers.tsx +50 -41
  60. package/src/ui/screens/AliasScreen.js +1111 -0
  61. package/src/ui/screens/AliasScreen.tsx +1534 -0
  62. package/src/ui/screens/CliToolsScreen.js +6 -1
  63. package/src/ui/screens/CliToolsScreen.tsx +6 -1
  64. package/src/ui/screens/EnvVarsScreen.js +6 -1
  65. package/src/ui/screens/EnvVarsScreen.tsx +6 -1
  66. package/src/ui/screens/GitignoreScreen.js +189 -88
  67. package/src/ui/screens/GitignoreScreen.tsx +312 -132
  68. package/src/ui/screens/McpRegistryScreen.js +13 -2
  69. package/src/ui/screens/McpRegistryScreen.tsx +13 -2
  70. package/src/ui/screens/McpScreen.js +6 -1
  71. package/src/ui/screens/McpScreen.tsx +6 -1
  72. package/src/ui/screens/ModelSelectorScreen.js +8 -2
  73. package/src/ui/screens/ModelSelectorScreen.tsx +8 -2
  74. package/src/ui/screens/PluginsScreen.js +17 -3
  75. package/src/ui/screens/PluginsScreen.tsx +16 -3
  76. package/src/ui/screens/ProfilesScreen.js +8 -1
  77. package/src/ui/screens/ProfilesScreen.tsx +8 -1
  78. package/src/ui/screens/SkillsScreen.js +21 -4
  79. package/src/ui/screens/SkillsScreen.tsx +39 -5
  80. package/src/ui/screens/StatusLineScreen.js +7 -1
  81. package/src/ui/screens/StatusLineScreen.tsx +7 -1
  82. package/src/ui/screens/index.js +1 -0
  83. package/src/ui/screens/index.ts +1 -0
  84. package/src/ui/state/reducer.js +5 -1
  85. package/src/ui/state/reducer.ts +6 -1
  86. package/src/ui/state/types.ts +12 -2
@@ -0,0 +1,1534 @@
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, type FooterHint } from "../components/layout/index.js";
6
+ import { FlagDetailEditor } from "../components/FlagDetailEditor.js";
7
+ import {
8
+ ALIAS_FLAGS,
9
+ FLAG_GROUPS,
10
+ type AliasFlag,
11
+ } from "../../data/alias-flags.js";
12
+ import {
13
+ defaultAliasConfig,
14
+ defaultValueFor,
15
+ validateAliasName,
16
+ derivedChannelValues,
17
+ withoutDerivedChannel,
18
+ CHANNELS_FLAG_ID,
19
+ DEFAULT_ALIAS_NAME,
20
+ type AliasConfig,
21
+ type FlagValue,
22
+ } from "../../services/alias-store.js";
23
+ import { loadAliasName, saveAliasName } from "../../services/alias-settings.js";
24
+ import {
25
+ detectShells,
26
+ renderArgs,
27
+ validateConfig,
28
+ writeAliasToShell,
29
+ parseAliasFromRc,
30
+ findAdoptableAlias,
31
+ type ShellTarget,
32
+ type FlagValidationIssue,
33
+ type Segment,
34
+ type AdoptableAlias,
35
+ } from "../../services/alias-shell-writer.js";
36
+ import { readFile } from "node:fs/promises";
37
+ import { existsSync } from "node:fs";
38
+
39
+ interface FlagRow {
40
+ kind: "flag";
41
+ flag: AliasFlag;
42
+ value: FlagValue;
43
+ /** Pre-computed validation issue for this row, if any. */
44
+ issue?: FlagValidationIssue;
45
+ /**
46
+ * Override for the checkbox's enabled state when the rendered alias would
47
+ * carry this flag even though its OWN value is disabled. Used by --channels:
48
+ * a dev-loaded channel forces `--channels` to emit (the render-time union),
49
+ * so the row must show `[x]` to match what gets written — even when the
50
+ * user's own channels list is off. Undefined → use the flag's own enabled.
51
+ */
52
+ effectiveEnabled?: boolean;
53
+ /**
54
+ * Override for the trailing detail (e.g. the --channels union count: own +
55
+ * derived). Undefined → derive from the flag's own value. Empty string is a
56
+ * valid override meaning "show nothing".
57
+ */
58
+ effectiveDetail?: string;
59
+ /**
60
+ * Override for the detail-pane `State:` line. For --channels this is the
61
+ * effective channel-list summary (the union of own + derived), so the detail
62
+ * agrees with the list count and the Preview. Undefined → summarizeValue.
63
+ */
64
+ stateSummary?: string;
65
+ }
66
+
67
+ interface AliasNameRow {
68
+ kind: "alias-name";
69
+ value: string;
70
+ }
71
+
72
+ type Row = AliasNameRow | FlagRow;
73
+
74
+ /**
75
+ * Flat-list items for the windowed `<ScrollableList>`. Selectable rows
76
+ * (`alias-name`, `flag`) accept the cursor; presentation rows (`header`)
77
+ * render in-line so groups scroll with their flags, but `j`/`k` skip them.
78
+ *
79
+ * The shells legend is NOT in this list — it lives outside the scroll area
80
+ * as a fixed footer so it's always visible. Keeping non-selectable items
81
+ * trailing the scrollable list would let `↓ N more` lie about reachable
82
+ * content.
83
+ */
84
+ type Item =
85
+ | { kind: "alias-name"; row: AliasNameRow }
86
+ | { kind: "flag"; row: FlagRow }
87
+ | { kind: "header"; label: string };
88
+
89
+ function isSelectableItem(item: Item): boolean {
90
+ return item.kind === "alias-name" || item.kind === "flag";
91
+ }
92
+
93
+ /**
94
+ * View modes for the Alias screen.
95
+ *
96
+ * - `main`: the default list of all flags grouped by section.
97
+ * - `flag-detail`: a dedicated sub-screen for editing a single `text-list` or
98
+ * `multi-with-custom` flag's value. Has its own keybindings (`a` add, `d`
99
+ * delete, `space`/`enter` toggle, `h` back to main) so each row's primary
100
+ * action is unambiguous. Esc still exits the screen via the global handler.
101
+ *
102
+ * Other flag kinds (boolean, tri-state, select, text, optional-text) use
103
+ * modals because they're single-action — no benefit from a sub-screen.
104
+ */
105
+ type ViewMode = { kind: "main" } | { kind: "flag-detail"; flagId: string };
106
+
107
+ export function AliasScreen() {
108
+ const { state } = useApp();
109
+ const modal = useModal();
110
+
111
+ const [config, setConfig] = useState<AliasConfig | null>(null);
112
+ const [shells, setShells] = useState<ShellTarget[]>([]);
113
+ const [selectedIdx, setSelectedIdx] = useState(0);
114
+ const [error, setError] = useState<string | null>(null);
115
+ const [viewMode, setViewMode] = useState<ViewMode>({ kind: "main" });
116
+ // Snapshot of the rc file's flag values as parsed on mount / write. Used
117
+ // to compute the dirty indicator: in-memory != rcSnapshot ⇒ unsaved.
118
+ const [rcSnapshot, setRcSnapshot] = useState<Record<string, FlagValue> | null>(
119
+ null,
120
+ );
121
+ // Transient write feedback shown on the header status line (not a blocking
122
+ // modal). Auto-clears after a few seconds. Errors still use a modal since
123
+ // they need to be read and acknowledged.
124
+ const [writeStatus, setWriteStatus] = useState<string | null>(null);
125
+
126
+ // A hand-written `claude` alias found OUTSIDE any managed block on mount.
127
+ // Set when the rc file has no managed block but a stray alias we could
128
+ // adopt — drives the one-time "import it?" offer below. Cleared once the
129
+ // offer has been shown (whether accepted or declined).
130
+ const [adoptable, setAdoptable] = useState<AdoptableAlias | null>(null);
131
+ // When the user accepts a LOSSLESS adoption, we remember the line range of
132
+ // their original alias so the next write replaces it in place (no duplicate
133
+ // alias). Cleared after that write absorbs it into the managed block.
134
+ const [pendingAdoptRange, setPendingAdoptRange] = useState<
135
+ [number, number] | null
136
+ >(null);
137
+
138
+ // Clear the write status line a few seconds after it appears.
139
+ useEffect(() => {
140
+ if (!writeStatus) return;
141
+ const t = setTimeout(() => setWriteStatus(null), 4000);
142
+ return () => clearTimeout(t);
143
+ }, [writeStatus]);
144
+
145
+ // One-time "adopt a hand-written alias?" offer. Fires when the mount load
146
+ // found a stray `claude` alias outside any managed block. We import only the
147
+ // flags we recognized; whether we also TAKE OWNERSHIP (remove the original
148
+ // line on the next write) depends on `lossless`:
149
+ // - lossless: every token round-trips → safe to absorb. We stash the line
150
+ // range so the next write replaces it in place (no duplicate alias).
151
+ // - not lossless: we couldn't parse some tokens (e.g. an uncatalogued
152
+ // flag like --model) → import the known flags for convenience but LEAVE
153
+ // the original line untouched. CRITICAL: we must NOT adopt its name here.
154
+ // Shell alias resolution is last-definition-wins, so a managed block
155
+ // sharing the name would shadow the surviving original at `source` time —
156
+ // silently killing the very tokens that made it not-lossless. Keeping the
157
+ // tool's own name lets both coexist: the original keeps working, the
158
+ // managed block lives under a different name.
159
+ // True once we've shown the offer. A ref (not state) so it survives re-render
160
+ // without being a dependency — critical because this effect mutates its own
161
+ // `adoptable` dep, and tying "fire once" to state/deps would let the dep
162
+ // change abort the in-flight modal mid-await (the cleanup would fire before
163
+ // the user answered). The ref decouples one-shot from the dependency array.
164
+ const didOfferAdoptRef = useRef(false);
165
+ useEffect(() => {
166
+ if (!adoptable || !config || didOfferAdoptRef.current) return;
167
+ didOfferAdoptRef.current = true;
168
+ (async () => {
169
+ const a = adoptable;
170
+
171
+ const body = a.rawLine.trim();
172
+ const others =
173
+ a.others.length > 0
174
+ ? `\n\n(Also found ${a.others.length} other claude alias${
175
+ a.others.length === 1 ? "" : "es"
176
+ } — leaving those alone.)`
177
+ : "";
178
+
179
+ // On a not-lossless adopt the managed block must NOT share the original's
180
+ // name — same name + last-wins shell resolution = the block shadows the
181
+ // surviving original, killing its uncatalogued tokens. Compute a managed
182
+ // name distinct from `a.name`: keep the tool's current name if it already
183
+ // differs, else derive a suffixed variant that's free in the rc file.
184
+ const managedName = a.lossless
185
+ ? a.name
186
+ : pickDistinctName(config.aliasName, a.name, a.others);
187
+
188
+ const message = a.lossless
189
+ ? `Found a hand-written alias:\n\n ${body}\n\nImport its flags into the editor? On the next write it'll be absorbed into a managed block (your original line is replaced — no duplicate).${others}`
190
+ : `Found a hand-written alias:\n\n ${body}\n\nI can import the flags I recognize, but it uses ${
191
+ a.leftovers.length
192
+ } token${a.leftovers.length === 1 ? "" : "s"} I don't manage (${a.leftovers
193
+ .map((t) => JSON.stringify(t))
194
+ .join(", ")}). I'll import the known flags and LEAVE your original "${a.name}" line untouched so it keeps working. The managed alias will use the name "${managedName}" — distinct from "${a.name}" so it can't override it. Import the known flags?${others}`;
195
+
196
+ const accepted = await modal.confirm("Adopt existing alias?", message);
197
+ if (accepted) {
198
+ // Lossless: adopt the name AND take ownership (replace the line on
199
+ // write). Not-lossless: import flags under a distinct managed name and
200
+ // never touch the original line — so it survives AND keeps resolving.
201
+ setConfig((prev) =>
202
+ prev
203
+ ? {
204
+ ...prev,
205
+ aliasName: managedName,
206
+ flags: { ...prev.flags, ...a.flags },
207
+ }
208
+ : prev,
209
+ );
210
+ if (a.lossless) {
211
+ // Persist the adopted name and stash the line range so the next
212
+ // write replaces the original in place (no duplicate, no shadow).
213
+ void persistAliasName(a.name);
214
+ setPendingAdoptRange(a.lineRange);
215
+ } else {
216
+ // Persist the distinct managed name so it survives a reopen.
217
+ void persistAliasName(managedName);
218
+ }
219
+ }
220
+ // Clear the candidate now that the decision is made — after the await,
221
+ // never before (clearing before would abort this very async via the
222
+ // dep-change cleanup, the bug this effect previously had).
223
+ setAdoptable(null);
224
+ })();
225
+ // persistAliasName is stable (useCallback); excluded to keep this one-shot.
226
+ // eslint-disable-next-line react-hooks/exhaustive-deps
227
+ }, [adoptable, config, modal]);
228
+
229
+ // ── Load alias name + parse rc file + detect shells on mount ─────────
230
+ // The alias name is computer-wide and persisted in ~/.claude/settings.json.
231
+ // Flag values are read directly from the shell rc file's managed block —
232
+ // the rc file IS the source of truth so what the user sees matches what
233
+ // they're actually running.
234
+ useEffect(() => {
235
+ let cancelled = false;
236
+ (async () => {
237
+ try {
238
+ const [name, s] = await Promise.all([loadAliasName(), detectShells()]);
239
+ if (cancelled) return;
240
+
241
+ // Pick the same rc file we'd write to with `w` (default shell that
242
+ // exists; else first existing). If none exists, start from defaults.
243
+ const readTarget =
244
+ s.find((sh) => sh.isDefault && sh.exists) ??
245
+ s.find((sh) => sh.exists);
246
+
247
+ const base = defaultAliasConfig();
248
+ base.aliasName = name;
249
+
250
+ let parsedFlags: Record<string, FlagValue> | null = null;
251
+ let foundAdoptable: AdoptableAlias | null = null;
252
+ if (readTarget && existsSync(readTarget.path)) {
253
+ try {
254
+ const rcText = await readFile(readTarget.path, "utf8");
255
+ const parsed = parseAliasFromRc(rcText);
256
+ if (parsed) {
257
+ parsedFlags = { ...base.flags, ...parsed.flags };
258
+ base.flags = parsedFlags;
259
+ // Honour an existing alias name from the rc if settings is at default.
260
+ // But the user's stored preference wins when set explicitly.
261
+ // (No-op here — `name` already loaded above.)
262
+ } else {
263
+ // No managed block on disk. The user may still have a hand-written
264
+ // `claude` alias elsewhere — detect it so we can offer to adopt it
265
+ // instead of looking broken. Non-destructive: just a candidate.
266
+ foundAdoptable = findAdoptableAlias(rcText);
267
+ }
268
+ } catch {
269
+ // rc unreadable — fall back to defaults; not a hard error.
270
+ }
271
+ }
272
+
273
+ if (cancelled) return;
274
+ // No mirror normalization: channels.values holds OWN channels only;
275
+ // derived channels come from the dev-load flag at display/render time.
276
+ // The parser already subtracts derived values from the parsed channels
277
+ // list, so `base.flags` is correct as-is.
278
+ setConfig(base);
279
+ setShells(s);
280
+ setRcSnapshot(parsedFlags ?? { ...base.flags });
281
+ if (foundAdoptable) setAdoptable(foundAdoptable);
282
+ } catch (err) {
283
+ if (cancelled) return;
284
+ setError(err instanceof Error ? err.message : String(err));
285
+ }
286
+ })();
287
+ return () => {
288
+ cancelled = true;
289
+ };
290
+ }, []);
291
+
292
+ // ── Build the flat list of items ──────────────────────────────────────
293
+ // Group headers and the shell legend live in the same flat array as
294
+ // alias-name + flag rows so that ScrollableList can window them together.
295
+ // Navigation skips non-selectable kinds.
296
+ const items: Item[] = useMemo(() => {
297
+ if (!config) return [];
298
+ const issues = validateConfig(config);
299
+ const issueMap = new Map(issues.map((i) => [i.flagId, i]));
300
+
301
+ const out: Item[] = [
302
+ {
303
+ kind: "alias-name",
304
+ row: { kind: "alias-name", value: config.aliasName },
305
+ },
306
+ ];
307
+
308
+ for (const group of FLAG_GROUPS) {
309
+ const groupFlags = ALIAS_FLAGS.filter((f) => f.group === group.id);
310
+ if (groupFlags.length === 0) continue;
311
+ out.push({ kind: "header", label: group.label });
312
+ for (const flag of groupFlags) {
313
+ // --channels shows enabled when the rendered alias would emit it —
314
+ // i.e. own channels are on OR a dev-loaded channel is derived in. This
315
+ // keeps the checkbox honest about what gets written without mutating
316
+ // the own-channels enabled flag (which would silently re-emit own
317
+ // values the user disabled).
318
+ let effectiveEnabled: boolean | undefined;
319
+ let effectiveDetail: string | undefined;
320
+ let stateSummary: string | undefined;
321
+ if (flag.id === CHANNELS_FLAG_ID) {
322
+ const own = config.flags[flag.id];
323
+ const ownValues =
324
+ own && own.kind === "text-list" && own.enabled
325
+ ? own.values.filter((v) => v.length > 0)
326
+ : [];
327
+ const derived = derivedChannelValues(config.flags);
328
+ // Deduped union — matches the writer's render-time union and the
329
+ // detail-pane summary, so the count is consistent everywhere.
330
+ const total = new Set([...ownValues, ...derived]).size;
331
+ effectiveEnabled = total > 0;
332
+ // Show the union count so the row matches what's written. When the
333
+ // count is purely derived, this reads e.g. `[x] --channels 1`.
334
+ effectiveDetail = total > 0 ? `${total}` : "";
335
+ // The detail-pane State line shows the same final channel list.
336
+ stateSummary = channelsStateSummary(config.flags);
337
+ }
338
+ out.push({
339
+ kind: "flag",
340
+ row: {
341
+ kind: "flag",
342
+ flag,
343
+ value: config.flags[flag.id] ?? defaultValueFor(flag),
344
+ issue: issueMap.get(flag.id),
345
+ effectiveEnabled,
346
+ effectiveDetail,
347
+ stateSummary,
348
+ },
349
+ });
350
+ }
351
+ }
352
+
353
+ return out;
354
+ }, [config]);
355
+
356
+ /** Indices in `items` that the cursor may land on. */
357
+ const selectableIndices = useMemo(
358
+ () => items.flatMap((it, i) => (isSelectableItem(it) ? [i] : [])),
359
+ [items],
360
+ );
361
+
362
+ // ── Flag-detail sub-screen items ──────────────────────────────────────
363
+ // Computed only when viewMode is flag-detail. Built off the flag's current
364
+ // The flag-detail sub-screen is now self-contained in <FlagDetailEditor>.
365
+ // We just resolve the flag + value pair here so the parent can render
366
+ // the editor and the right-pane preview.
367
+ const detailFlag: AliasFlag | null =
368
+ viewMode.kind === "flag-detail"
369
+ ? ALIAS_FLAGS.find((f) => f.id === viewMode.flagId) ?? null
370
+ : null;
371
+ const detailValue: FlagValue | null =
372
+ detailFlag && config ? config.flags[detailFlag.id] ?? null : null;
373
+
374
+ // ── Helper: check whether a row is gated (requires unmet) ─────────────
375
+ const isGated = useCallback(
376
+ (flag: AliasFlag): boolean => {
377
+ if (!flag.requires || !config) return false;
378
+ return !isFlagEnabled(config.flags[flag.requires]);
379
+ },
380
+ [config],
381
+ );
382
+
383
+ const selectedItem = items[selectedIdx];
384
+ const selectedRow: Row | undefined =
385
+ selectedItem &&
386
+ (selectedItem.kind === "alias-name" || selectedItem.kind === "flag")
387
+ ? selectedItem.row
388
+ : undefined;
389
+
390
+ // Clamp selection: snap to the nearest selectable index whenever items shift.
391
+ useEffect(() => {
392
+ if (selectableIndices.length === 0) return;
393
+ if (!isSelectableItem(items[selectedIdx])) {
394
+ // Find the nearest selectable index <= selectedIdx (or the first one)
395
+ const fallback =
396
+ [...selectableIndices].reverse().find((i) => i <= selectedIdx) ??
397
+ selectableIndices[0];
398
+ setSelectedIdx(fallback);
399
+ } else if (selectedIdx >= items.length) {
400
+ setSelectedIdx(selectableIndices[selectableIndices.length - 1]);
401
+ }
402
+ }, [items, selectableIndices, selectedIdx]);
403
+
404
+ // ── Mutators ──────────────────────────────────────────────────────────
405
+ // Flag mutations stay in memory. Only the alias name is persisted (to
406
+ // ~/.claude/settings.json) — flag values reach disk only when the user
407
+ // explicitly presses `w` to write the shell rc file.
408
+ const persistAliasName = useCallback(
409
+ async (name: string) => {
410
+ try {
411
+ await saveAliasName(name);
412
+ } catch (err) {
413
+ await modal.message(
414
+ "Save failed",
415
+ err instanceof Error ? err.message : String(err),
416
+ "error",
417
+ );
418
+ }
419
+ },
420
+ [modal],
421
+ );
422
+
423
+ // Cascade dependencies on every mutation:
424
+ // - Enabling a flag → enable its `requires` chain (no modal nagging).
425
+ // - Disabling a flag → disable anything whose `requires === this flag id`,
426
+ // because the renderer would silently drop them otherwise and the UI
427
+ // would lie about what's going to be written.
428
+ const updateFlag = useCallback(
429
+ (id: string, next: FlagValue) => {
430
+ setConfig((prev) => {
431
+ if (!prev) return prev;
432
+ const flags = { ...prev.flags, [id]: next };
433
+ const target = ALIAS_FLAGS.find((f) => f.id === id);
434
+ if (!target) return { ...prev, flags };
435
+
436
+ const becomingEnabled = isFlagEnabled(next);
437
+
438
+ if (becomingEnabled) {
439
+ // Walk `requires` chain and force-enable each ancestor to its
440
+ // catalog-default-enabled state if it's currently off.
441
+ let cursor = target.requires;
442
+ const seen = new Set<string>();
443
+ while (cursor && !seen.has(cursor)) {
444
+ seen.add(cursor);
445
+ const dep = ALIAS_FLAGS.find((f) => f.id === cursor);
446
+ if (!dep) break;
447
+ if (!isFlagEnabled(flags[dep.id])) {
448
+ flags[dep.id] = enableDefault(dep);
449
+ }
450
+ cursor = dep.requires;
451
+ }
452
+ } else {
453
+ // Cascade-disable any flag that requires this one (one level deep is
454
+ // enough today; recurse defensively in case a chain emerges).
455
+ const queue = [id];
456
+ const seen = new Set<string>();
457
+ while (queue.length > 0) {
458
+ const cur = queue.shift()!;
459
+ if (seen.has(cur)) continue;
460
+ seen.add(cur);
461
+ for (const dependent of ALIAS_FLAGS) {
462
+ if (dependent.requires !== cur) continue;
463
+ if (!isFlagEnabled(flags[dependent.id])) continue;
464
+ flags[dependent.id] = disableValue(flags[dependent.id]);
465
+ queue.push(dependent.id);
466
+ }
467
+ }
468
+ }
469
+
470
+ return { ...prev, flags };
471
+ });
472
+ },
473
+ [],
474
+ );
475
+
476
+ // Remove a value from the dev-load flag — invoked when the user deletes a
477
+ // "Dev-loaded channels" row inside the --channels editor. The derived
478
+ // section is sourced from this flag, so the deletion lands here, not in
479
+ // channels.values. updateFlag keeps enabled-state and cascades consistent.
480
+ const removeDevloadChannel = useCallback((val: string) => {
481
+ setConfig((prev) =>
482
+ prev ? { ...prev, flags: withoutDerivedChannel(prev.flags, val) } : prev,
483
+ );
484
+ }, []);
485
+
486
+ const editAliasName = useCallback(async () => {
487
+ if (!config) return;
488
+ const v = await modal.input(
489
+ "Alias name",
490
+ `Shell alias name (default: "${DEFAULT_ALIAS_NAME}"). Letters, digits, _ or - only.`,
491
+ config.aliasName,
492
+ );
493
+ if (v === null) return;
494
+ const reason = validateAliasName(v);
495
+ if (reason) {
496
+ await modal.message("Invalid alias name", reason, "error");
497
+ return;
498
+ }
499
+ setConfig((prev) => (prev ? { ...prev, aliasName: v } : prev));
500
+ void persistAliasName(v);
501
+ }, [config, modal, persistAliasName]);
502
+
503
+ const editSelected = useCallback(async () => {
504
+ if (!selectedRow) return;
505
+ if (selectedRow.kind === "alias-name") {
506
+ await editAliasName();
507
+ return;
508
+ }
509
+ // No more "Requires another flag" modal — `updateFlag` auto-enables the
510
+ // `requires` chain when the user activates a gated flag.
511
+ //
512
+ // Collection editors live in a dedicated sub-screen, not a modal —
513
+ // each row gets its own keybindings (space toggle, a add, d delete)
514
+ // so there's no "what does enter do here?" overloading.
515
+ const k = selectedRow.value.kind;
516
+ if (k === "text-list" || k === "multi-with-custom") {
517
+ // FlagDetailEditor manages its own cursor — parent just flips mode.
518
+ setViewMode({ kind: "flag-detail", flagId: selectedRow.flag.id });
519
+ return;
520
+ }
521
+ const next = await editFlagValue(selectedRow.flag, selectedRow.value, modal);
522
+ if (next) updateFlag(selectedRow.flag.id, next);
523
+ }, [selectedRow, modal, updateFlag, editAliasName]);
524
+
525
+ const toggleSelected = useCallback(async () => {
526
+ if (!selectedRow) return;
527
+ if (selectedRow.kind === "alias-name") {
528
+ // No "toggle" semantic for the name — fall through to edit.
529
+ await editAliasName();
530
+ return;
531
+ }
532
+ // Only booleans and tri-states have a meaningful single-press cycle.
533
+ // Everything else (select, text, list, multi-with-custom) needs a value
534
+ // chooser; falling back to edit prevents the "I pressed space and got the
535
+ // first option without seeing the choices" footgun.
536
+ const k = selectedRow.value.kind;
537
+ if (k !== "boolean" && k !== "tri-state") {
538
+ await editSelected();
539
+ return;
540
+ }
541
+ const next = quickToggle(selectedRow.flag, selectedRow.value);
542
+ if (next) updateFlag(selectedRow.flag.id, next);
543
+ }, [selectedRow, updateFlag, editSelected, editAliasName]);
544
+
545
+ const handleWrite = useCallback(async () => {
546
+ if (!config) return;
547
+
548
+ // Pick the target shell automatically: default shell (matches $SHELL)
549
+ // if its rc file exists; otherwise the first existing rc file. Users
550
+ // who care about other shells edit them directly — the alias config
551
+ // itself is stored in ~/.claude and the user can run `w` again from
552
+ // a different $SHELL session if they want to mirror it.
553
+ const target =
554
+ shells.find((s) => s.isDefault && s.exists) ??
555
+ shells.find((s) => s.exists);
556
+
557
+ if (!target) {
558
+ await modal.message(
559
+ "No shells detected",
560
+ "No supported rc files found. Create ~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish first.",
561
+ "info",
562
+ );
563
+ return;
564
+ }
565
+
566
+ try {
567
+ // If we adopted a lossless hand-written alias, pass its line range so
568
+ // the FIRST write replaces that line in place (absorbing it into the
569
+ // managed block) rather than appending a second alias. Once the block
570
+ // exists, the writer ignores the range and splices by markers.
571
+ const r = await writeAliasToShell(
572
+ config,
573
+ target,
574
+ pendingAdoptRange ? { adoptLineRange: pendingAdoptRange } : {},
575
+ );
576
+ // The adopted line is now gone (replaced by the block); drop the range
577
+ // so subsequent writes don't try to re-target a line that moved.
578
+ if (pendingAdoptRange) setPendingAdoptRange(null);
579
+ // Non-blocking confirmation on the status line — no "press any key".
580
+ const name = r.path.split("/").pop() || r.path;
581
+ setWriteStatus(
582
+ `✓ ${r.action === "created" ? "Created" : "Updated"} ${name} — source it or open a new shell`,
583
+ );
584
+ setShells(await detectShells());
585
+ // We just wrote the rc file with current in-memory flag values, so
586
+ // the snapshot now matches in-memory state. Dirty indicator clears.
587
+ setRcSnapshot(config.flags);
588
+ } catch (err) {
589
+ await modal.message(
590
+ "Write failed",
591
+ err instanceof Error ? err.message : String(err),
592
+ "error",
593
+ );
594
+ }
595
+ }, [config, shells, modal, pendingAdoptRange]);
596
+
597
+ // ── Keyboard ──────────────────────────────────────────────────────────
598
+ // j/k navigate the selectable subset, jumping over headers/legend lines.
599
+ const moveSelection = useCallback(
600
+ (dir: 1 | -1) => {
601
+ if (selectableIndices.length === 0) return;
602
+ const cursorPos = selectableIndices.indexOf(selectedIdx);
603
+ const nextPos =
604
+ cursorPos === -1
605
+ ? // Currently parked on a non-selectable; move to first/last selectable.
606
+ dir === 1
607
+ ? 0
608
+ : selectableIndices.length - 1
609
+ : Math.min(
610
+ selectableIndices.length - 1,
611
+ Math.max(0, cursorPos + dir),
612
+ );
613
+ setSelectedIdx(selectableIndices[nextPos]);
614
+ },
615
+ [selectableIndices, selectedIdx],
616
+ );
617
+
618
+ // The flag-detail sub-screen handles its own cursor + key + render
619
+ // (see FlagDetailEditor component). The parent only needs to know how
620
+ // to leave it.
621
+ const exitDetail = useCallback(() => {
622
+ setViewMode({ kind: "main" });
623
+ }, []);
624
+
625
+ // ── Keyboard handler ──────────────────────────────────────────────────
626
+ // When the sub-screen is mounted, FlagDetailEditor installs its own
627
+ // keyboard handler. Both handlers fire (OpenTUI's useKeyboard delivers
628
+ // to every subscriber), so this handler must SKIP the keys the child
629
+ // owns to avoid double-dispatch. We bail entirely while in detail mode.
630
+ useKeyboardHandler((input, key) => {
631
+ if (state.modal) return;
632
+ if (viewMode.kind === "flag-detail") return;
633
+
634
+ // Main mode.
635
+ if (input === "j" || key.downArrow) {
636
+ moveSelection(1);
637
+ return;
638
+ }
639
+ if (input === "k" || key.upArrow) {
640
+ moveSelection(-1);
641
+ return;
642
+ }
643
+ if (key.return || input === " " || key.name === "space") {
644
+ void toggleSelected();
645
+ return;
646
+ }
647
+ if (input === "e") {
648
+ void editSelected();
649
+ return;
650
+ }
651
+ if (input === "w") {
652
+ void handleWrite();
653
+ return;
654
+ }
655
+ if (input === "r") {
656
+ // Reset the selected row to its default.
657
+ if (!selectedRow) return;
658
+ if (selectedRow.kind === "alias-name") {
659
+ setConfig((prev) =>
660
+ prev ? { ...prev, aliasName: DEFAULT_ALIAS_NAME } : prev,
661
+ );
662
+ void persistAliasName(DEFAULT_ALIAS_NAME);
663
+ return;
664
+ }
665
+ // Flag reset stays in memory until `w`.
666
+ updateFlag(selectedRow.flag.id, defaultValueFor(selectedRow.flag));
667
+ }
668
+ });
669
+
670
+ // ── Render ────────────────────────────────────────────────────────────
671
+ const previewSegments = config ? renderArgs(config) : [];
672
+ const previewAliasName = config?.aliasName ?? "";
673
+
674
+ // Native OpenTUI scrollboxes own the scroll math. We render every item
675
+ // into the scrollbox's content area and ask the renderable to scroll the
676
+ // selected child into view whenever the cursor moves. The previous JS-
677
+ // windowed approach (ScrollableList) overstrike-rendered rows when its
678
+ // height computation drifted off the actual panel size.
679
+ const mainScrollRef = useRef<ScrollBoxRenderable | null>(null);
680
+
681
+ useEffect(() => {
682
+ const item = items[selectedIdx];
683
+ if (!item) return;
684
+ mainScrollRef.current?.scrollChildIntoView(`alias-row-${getItemKey(item)}`);
685
+ }, [items, selectedIdx]);
686
+
687
+ const inDetail = viewMode.kind === "flag-detail" && detailFlag && detailValue;
688
+ // FlagDetailEditor only handles text-list and multi-with-custom values;
689
+ // narrow the type for the JSX below.
690
+ const editableDetailValue =
691
+ inDetail &&
692
+ (detailValue.kind === "text-list" ||
693
+ detailValue.kind === "multi-with-custom")
694
+ ? detailValue
695
+ : null;
696
+
697
+ const mainListPanel = !config ? (
698
+ <text fg="gray">{error ? `Error: ${error}` : "Loading..."}</text>
699
+ ) : (
700
+ <box flexDirection="column" height="100%">
701
+ <scrollbox
702
+ ref={mainScrollRef}
703
+ scrollY={true}
704
+ scrollX={false}
705
+ flexGrow={1}
706
+ >
707
+ <box flexDirection="column">
708
+ {items.map((item, idx) => (
709
+ // id is content-derived (not positional `${idx}`) so OpenTUI's
710
+ // reconciler stays in lockstep with React's key-based reconcile.
711
+ <box key={getItemKey(item)} id={`alias-row-${getItemKey(item)}`}>
712
+ {renderItem(item, idx === selectedIdx, isGated)}
713
+ </box>
714
+ ))}
715
+ </box>
716
+ </scrollbox>
717
+ </box>
718
+ );
719
+
720
+ const listPanel =
721
+ inDetail && editableDetailValue ? (
722
+ <FlagDetailEditor
723
+ flag={detailFlag}
724
+ value={editableDetailValue}
725
+ onChange={(next) => updateFlag(detailFlag.id, next)}
726
+ onBack={exitDetail}
727
+ derivedValues={
728
+ detailFlag.id === CHANNELS_FLAG_ID && config
729
+ ? derivedChannelValues(config.flags)
730
+ : undefined
731
+ }
732
+ derivedLabel="From --dangerously-load:"
733
+ toggleLabel={
734
+ detailFlag.id === CHANNELS_FLAG_ID
735
+ ? "Enable own channels"
736
+ : undefined
737
+ }
738
+ onDerivedDelete={(v) => removeDevloadChannel(v)}
739
+ />
740
+ ) : (
741
+ mainListPanel
742
+ );
743
+
744
+ const mainDetailPanel = renderDetail(
745
+ selectedRow,
746
+ isGated,
747
+ previewAliasName,
748
+ previewSegments,
749
+ );
750
+ // When editing --channels and there are derived rows, the right pane carries
751
+ // the contextual instruction about what deleting a derived row does — that
752
+ // explanation belongs here, not crammed into the left list.
753
+ const channelsHasDerived =
754
+ inDetail &&
755
+ detailFlag.id === CHANNELS_FLAG_ID &&
756
+ config &&
757
+ derivedChannelValues(config.flags).length > 0;
758
+ const derivedNote = channelsHasDerived
759
+ ? "Rows under “From --dangerously-load:” are derived from the --dangerously-load-development-channels flag. Deleting one here removes it from that flag."
760
+ : undefined;
761
+ // The right-pane `State:` line is informational — it describes what the alias
762
+ // actually runs. For --channels that's the FINAL channel list: the union of
763
+ // the user's own channels and the derived (dev-loaded) ones. We show the
764
+ // union count so it agrees with the list row's count and the Preview, and
765
+ // annotate how many are derived so the source is clear. Computed only when
766
+ // editing --channels; other flags fall back to their own summary.
767
+ const stateOverride =
768
+ inDetail && detailFlag.id === CHANNELS_FLAG_ID && config
769
+ ? channelsStateSummary(config.flags)
770
+ : undefined;
771
+ const detailDetailPanel = inDetail
772
+ ? renderFlagDetailRightPane(
773
+ detailFlag,
774
+ detailValue,
775
+ previewAliasName,
776
+ previewSegments,
777
+ derivedNote,
778
+ stateOverride,
779
+ )
780
+ : null;
781
+ const detailPanel = inDetail ? detailDetailPanel : mainDetailPanel;
782
+
783
+ const enabledCount = config
784
+ ? Object.values(config.flags).filter(isFlagEnabled).length
785
+ : 0;
786
+ const subtitle = inDetail
787
+ ? `editing ${detailFlag.flag}`
788
+ : writeStatus
789
+ ? writeStatus
790
+ : config
791
+ ? `${enabledCount} flag${enabledCount === 1 ? "" : "s"} enabled`
792
+ : "";
793
+
794
+ // Resolve the rc-file name we'd actually write to (basename of the chosen
795
+ // shell's path). Falls back to "shell" if no shells were detected — UI
796
+ // surface mirrors the runtime behavior of `handleWrite`.
797
+ const writeTarget =
798
+ shells.find((s) => s.isDefault && s.exists) ??
799
+ shells.find((s) => s.exists);
800
+ const writeLabel = writeTarget
801
+ ? writeTarget.path.split("/").pop() || "shell"
802
+ : "shell";
803
+
804
+ // Dirty when current in-memory flags differ from the rc-file snapshot.
805
+ // Initial mount sets rcSnapshot from the parsed rc, so this is false
806
+ // until the user edits.
807
+ const dirty = config
808
+ ? rcSnapshot !== null && !shallowFlagsEqual(config.flags, rcSnapshot)
809
+ : false;
810
+
811
+ const footerHints: FooterHint[] = inDetail
812
+ ? [
813
+ { keys: ["↑", "↓"], label: "move" },
814
+ { keys: ["space"], label: "toggle" },
815
+ { keys: ["a"], label: "add" },
816
+ { keys: ["d"], label: "delete" },
817
+ { keys: ["h"], label: "back" },
818
+ ]
819
+ : [
820
+ { keys: ["↑", "↓"], label: "move" },
821
+ { keys: ["space"], label: "toggle" },
822
+ { keys: ["e"], label: "edit" },
823
+ { keys: ["r"], label: "reset" },
824
+ {
825
+ keys: ["w"],
826
+ label: `${dirty ? "*" : ""}write to ${writeLabel}`,
827
+ },
828
+ ];
829
+
830
+ return (
831
+ <ScreenLayout
832
+ title="Claude Alias"
833
+ subtitle={subtitle}
834
+ currentScreen="alias"
835
+ footerHints={footerHints}
836
+ listPanel={listPanel}
837
+ detailPanel={detailPanel}
838
+ />
839
+ );
840
+ }
841
+
842
+ // ─── Helpers ────────────────────────────────────────────────────────────
843
+
844
+ /**
845
+ * Summarize the EFFECTIVE state of `--channels` for the detail `State:` line:
846
+ * the final channel list that the alias will actually run, i.e. the union of
847
+ * the user's own (enabled) channels and the derived dev-loaded ones. This must
848
+ * agree with the list row's union count and the Preview.
849
+ *
850
+ * Returns "off" when nothing is emitted, or e.g.:
851
+ * - "2 channels" (all own, no derived)
852
+ * - "1 channel (from --dangerously-load)" (purely derived, own off/empty)
853
+ * - "2 channels (1 from --dangerously-load)" (mixed)
854
+ */
855
+ function channelsStateSummary(flags: Record<string, FlagValue>): string {
856
+ const ownValue = flags[CHANNELS_FLAG_ID];
857
+ const ownValues =
858
+ ownValue && ownValue.kind === "text-list" && ownValue.enabled
859
+ ? ownValue.values.filter((v) => v.length > 0)
860
+ : [];
861
+ const derived = derivedChannelValues(flags);
862
+ // Union, deduped, matching the writer's render-time union.
863
+ const total = new Set([...ownValues, ...derived]).size;
864
+ if (total === 0) return "off";
865
+ const noun = total === 1 ? "channel" : "channels";
866
+ if (derived.length === 0) return `${total} ${noun}`;
867
+ // How many of the union come from the derived flag (after dedup with own).
868
+ const derivedInUnion = new Set(derived).size;
869
+ const derivedNote =
870
+ derivedInUnion === total
871
+ ? "from --dangerously-load"
872
+ : `${derivedInUnion} from --dangerously-load`;
873
+ return `${total} ${noun} (${derivedNote})`;
874
+ }
875
+
876
+ /**
877
+ * Compare two flag maps for value equality. We render-then-diff via JSON
878
+ * because FlagValues are small, fully serialisable, and order-insensitive
879
+ * inside the maps. A property-by-property deep-equal would be more efficient
880
+ * but the maps have ~14 entries — diff cost is negligible.
881
+ */
882
+ function shallowFlagsEqual(
883
+ a: Record<string, FlagValue>,
884
+ b: Record<string, FlagValue>,
885
+ ): boolean {
886
+ const aKeys = Object.keys(a).sort();
887
+ const bKeys = Object.keys(b).sort();
888
+ if (aKeys.length !== bKeys.length) return false;
889
+ for (let i = 0; i < aKeys.length; i++) {
890
+ if (aKeys[i] !== bKeys[i]) return false;
891
+ }
892
+ for (const k of aKeys) {
893
+ if (JSON.stringify(a[k]) !== JSON.stringify(b[k])) return false;
894
+ }
895
+ return true;
896
+ }
897
+
898
+ function isFlagEnabled(value: FlagValue | undefined): boolean {
899
+ if (!value) return false;
900
+ switch (value.kind) {
901
+ case "boolean":
902
+ return value.enabled;
903
+ case "tri-state":
904
+ return value.state !== "unset";
905
+ case "select":
906
+ case "text":
907
+ case "optional-text":
908
+ case "text-list":
909
+ case "multi-with-custom":
910
+ return value.enabled;
911
+ }
912
+ }
913
+
914
+ /**
915
+ * Pick a managed-alias name that can't shadow a surviving hand-written alias.
916
+ *
917
+ * On a not-lossless adoption we keep the user's original `<original>` line in
918
+ * the rc file, so the managed block must NOT share its name (shell aliases are
919
+ * last-definition-wins — a same-named block would override the original and
920
+ * silently drop the very tokens that made it not-lossless).
921
+ *
922
+ * Strategy: prefer the tool's `current` name if it already differs from the
923
+ * original. Otherwise derive `<original>m`, `<original>m2`, … until we find one
924
+ * that is valid AND collides with neither the original nor the other detected
925
+ * claude aliases. Falls back to a timestamp-free deterministic suffix.
926
+ */
927
+ function pickDistinctName(
928
+ current: string,
929
+ original: string,
930
+ others: string[],
931
+ ): string {
932
+ // Names already taken by aliases we're leaving in place.
933
+ const taken = new Set<string>([original]);
934
+ for (const line of others) {
935
+ const m = /^alias\s+([A-Za-z_][A-Za-z0-9_-]*)\s*=/.exec(line.trim());
936
+ if (m) taken.add(m[1]);
937
+ }
938
+
939
+ // If our current name is already distinct + valid, just use it.
940
+ if (current !== original && !taken.has(current) && !validateAliasName(current)) {
941
+ return current;
942
+ }
943
+
944
+ // Derive a suffixed variant from the original.
945
+ const base = `${original}m`;
946
+ if (!taken.has(base) && !validateAliasName(base)) return base;
947
+ for (let n = 2; n < 100; n++) {
948
+ const cand = `${original}m${n}`;
949
+ if (!taken.has(cand) && !validateAliasName(cand)) return cand;
950
+ }
951
+ // Pathological fallback — should never be reached for sane inputs.
952
+ return `${original}managed`;
953
+ }
954
+
955
+ /**
956
+ * Produce an "enabled by default" value for a flag — what we'd give it when
957
+ * the user activates a dependent that needs this one on. For booleans this
958
+ * is just `enabled: true`; for kinds that carry a value (select/text/etc.)
959
+ * we set `enabled: true` with the catalog default value, leaving the user
960
+ * free to refine it after.
961
+ */
962
+ function enableDefault(flag: AliasFlag): FlagValue {
963
+ switch (flag.kind) {
964
+ case "boolean":
965
+ return { kind: "boolean", enabled: true };
966
+ case "tri-state":
967
+ return { kind: "tri-state", state: "on" };
968
+ case "select":
969
+ return {
970
+ kind: "select",
971
+ enabled: true,
972
+ value: flag.options?.[0]?.value ?? "",
973
+ };
974
+ case "text":
975
+ return { kind: "text", enabled: true, value: "" };
976
+ case "optional-text":
977
+ return { kind: "optional-text", enabled: true, value: "" };
978
+ case "text-list":
979
+ return {
980
+ kind: "text-list",
981
+ enabled: true,
982
+ values: [...(flag.defaultValues ?? [])],
983
+ };
984
+ case "multi-with-custom":
985
+ return {
986
+ kind: "multi-with-custom",
987
+ enabled: true,
988
+ picked: [],
989
+ custom: [...(flag.defaultValues ?? [])],
990
+ };
991
+ }
992
+ }
993
+
994
+ /**
995
+ * Disable a flag while preserving any values the user has already entered.
996
+ * Used by the cascade-disable path: turning off `--worktree` shouldn't lose
997
+ * the user's `--tmux=classic` choice — only re-enable the dependency and
998
+ * the value reappears.
999
+ */
1000
+ function disableValue(value: FlagValue): FlagValue {
1001
+ switch (value.kind) {
1002
+ case "boolean":
1003
+ return { kind: "boolean", enabled: false };
1004
+ case "tri-state":
1005
+ return { kind: "tri-state", state: "unset" };
1006
+ case "select":
1007
+ case "text":
1008
+ case "optional-text":
1009
+ case "text-list":
1010
+ case "multi-with-custom":
1011
+ return { ...value, enabled: false };
1012
+ }
1013
+ }
1014
+
1015
+ function summarizeValue(value: FlagValue): string {
1016
+ switch (value.kind) {
1017
+ case "boolean":
1018
+ return value.enabled ? "on" : "off";
1019
+ case "tri-state":
1020
+ return value.state;
1021
+ case "select":
1022
+ return value.enabled ? value.value || "(bare)" : "off";
1023
+ case "text":
1024
+ case "optional-text":
1025
+ return value.enabled
1026
+ ? value.value
1027
+ ? truncate(value.value, 32)
1028
+ : "(empty)"
1029
+ : "off";
1030
+ case "text-list":
1031
+ return value.enabled ? `${value.values.length} item(s)` : "off";
1032
+ case "multi-with-custom": {
1033
+ if (!value.enabled) return "off";
1034
+ const total = new Set([...value.picked, ...value.custom]).size;
1035
+ return total === 0 ? "(no filter)" : `${total} token(s)`;
1036
+ }
1037
+ }
1038
+ }
1039
+
1040
+ function truncate(s: string, n: number): string {
1041
+ return s.length <= n ? s : s.slice(0, n - 1) + "…";
1042
+ }
1043
+
1044
+ /**
1045
+ * Quick toggle (enter/space). For boolean → flip. For tri-state → cycle
1046
+ * unset → on → off → unset. For others, just flip the `enabled` field; the
1047
+ * user must press `e` to set the value.
1048
+ */
1049
+ function quickToggle(_flag: AliasFlag, value: FlagValue): FlagValue | null {
1050
+ switch (value.kind) {
1051
+ case "boolean":
1052
+ return { kind: "boolean", enabled: !value.enabled };
1053
+ case "tri-state": {
1054
+ const next: "unset" | "on" | "off" =
1055
+ value.state === "unset" ? "on" : value.state === "on" ? "off" : "unset";
1056
+ return { kind: "tri-state", state: next };
1057
+ }
1058
+ case "select":
1059
+ return { ...value, enabled: !value.enabled };
1060
+ case "text":
1061
+ case "optional-text":
1062
+ return { ...value, enabled: !value.enabled };
1063
+ case "text-list":
1064
+ return { ...value, enabled: !value.enabled };
1065
+ case "multi-with-custom":
1066
+ return { ...value, enabled: !value.enabled };
1067
+ }
1068
+ }
1069
+
1070
+ /**
1071
+ * `e` — edit the value. Modal flow varies by flag kind.
1072
+ */
1073
+ async function editFlagValue(
1074
+ flag: AliasFlag,
1075
+ value: FlagValue,
1076
+ modal: ReturnType<typeof useModal>,
1077
+ ): Promise<FlagValue | null> {
1078
+ switch (value.kind) {
1079
+ case "boolean":
1080
+ return { kind: "boolean", enabled: !value.enabled };
1081
+
1082
+ case "tri-state": {
1083
+ const choice = await modal.select(
1084
+ flag.label,
1085
+ flag.description,
1086
+ [
1087
+ { label: "Unset (no opinion)", value: "unset" },
1088
+ { label: `On (${flag.flag})`, value: "on" },
1089
+ ...(flag.triStateOff
1090
+ ? [{ label: `Off (${flag.triStateOff})`, value: "off" }]
1091
+ : []),
1092
+ ],
1093
+ ["unset", "on", "off"].indexOf(value.state),
1094
+ );
1095
+ if (!choice) return null;
1096
+ return { kind: "tri-state", state: choice as "unset" | "on" | "off" };
1097
+ }
1098
+
1099
+ case "select": {
1100
+ const opts = flag.options ?? [];
1101
+ const choice = await modal.select(
1102
+ flag.label,
1103
+ flag.description,
1104
+ [
1105
+ { label: "(disable)", value: "__off__" },
1106
+ ...opts.map((o) => ({ label: o.label, value: `v:${o.value}` })),
1107
+ ],
1108
+ value.enabled
1109
+ ? Math.max(0, opts.findIndex((o) => o.value === value.value)) + 1
1110
+ : 0,
1111
+ );
1112
+ if (choice === null) return null;
1113
+ if (choice === "__off__") return { ...value, enabled: false };
1114
+ return {
1115
+ kind: "select",
1116
+ enabled: true,
1117
+ value: choice.replace(/^v:/, ""),
1118
+ };
1119
+ }
1120
+
1121
+ case "text": {
1122
+ // Lead with a 2-option chooser so the user can disable without going
1123
+ // through `r` reset. Selecting "Set value" jumps to the input modal.
1124
+ const action = await modal.select(
1125
+ flag.label,
1126
+ flag.description,
1127
+ value.enabled
1128
+ ? [
1129
+ { label: `Edit value (current: "${truncate(value.value, 32)}")`, value: "__edit__" },
1130
+ { label: "Disable flag", value: "__off__" },
1131
+ ]
1132
+ : [
1133
+ { label: "Enable with value...", value: "__edit__" },
1134
+ { label: "Keep disabled", value: "__off__" },
1135
+ ],
1136
+ 0,
1137
+ );
1138
+ if (action === null) return null;
1139
+ if (action === "__off__") {
1140
+ return { kind: "text", enabled: false, value: value.value };
1141
+ }
1142
+ const v = await modal.input(flag.label, flag.description, value.value);
1143
+ if (v === null) return null;
1144
+ return { kind: "text", enabled: v.length > 0, value: v };
1145
+ }
1146
+
1147
+ case "optional-text": {
1148
+ // Three-option lead: edit value, bare (no value), or disable.
1149
+ const action = await modal.select(
1150
+ flag.label,
1151
+ flag.description,
1152
+ [
1153
+ {
1154
+ label: value.enabled && value.value
1155
+ ? `Edit value (current: "${truncate(value.value, 32)}")`
1156
+ : "Set value...",
1157
+ value: "__edit__",
1158
+ },
1159
+ { label: "Bare flag (no value)", value: "__bare__" },
1160
+ { label: "Disable flag", value: "__off__" },
1161
+ ],
1162
+ value.enabled ? (value.value ? 0 : 1) : 2,
1163
+ );
1164
+ if (action === null) return null;
1165
+ if (action === "__off__") {
1166
+ return { kind: "optional-text", enabled: false, value: value.value };
1167
+ }
1168
+ if (action === "__bare__") {
1169
+ return { kind: "optional-text", enabled: true, value: "" };
1170
+ }
1171
+ const v = await modal.input(
1172
+ flag.label,
1173
+ flag.description,
1174
+ value.value,
1175
+ );
1176
+ if (v === null) return null;
1177
+ return { kind: "optional-text", enabled: true, value: v };
1178
+ }
1179
+
1180
+ case "text-list":
1181
+ case "multi-with-custom":
1182
+ // These kinds are routed to the flag-detail sub-screen by
1183
+ // `editSelected`, never to this modal-based editor. If you see this
1184
+ // throw, the caller bypassed the routing in editSelected — fix that
1185
+ // instead of re-introducing the modal cascade.
1186
+ throw new Error(
1187
+ `${flag.kind} flags use the flag-detail sub-screen, not the modal editor`,
1188
+ );
1189
+ }
1190
+ }
1191
+
1192
+
1193
+ // ─── Render helpers ─────────────────────────────────────────────────────
1194
+
1195
+ /**
1196
+ * The alias-name row sits above all flag groups. We render it with a clear
1197
+ * "Alias:" label and the value styled as the editable field, plus an `[edit]`
1198
+ * hint, so it doesn't blend in with the flag rows below.
1199
+ */
1200
+ function renderAliasNameRow(name: string, selected: boolean): React.ReactNode {
1201
+ if (selected) {
1202
+ return (
1203
+ <box key="alias-name-row" flexDirection="row">
1204
+ <text bg="#7e57c2" fg="white">
1205
+ {`▶ Alias name: ${name} [enter to edit]`}
1206
+ </text>
1207
+ </box>
1208
+ );
1209
+ }
1210
+ return (
1211
+ <text key="alias-name-row">
1212
+ <span fg="gray">{" Alias name: "}</span>
1213
+ <span fg="cyan">
1214
+ <strong>{name}</strong>
1215
+ </span>
1216
+ <span fg="gray">{" [enter to edit]"}</span>
1217
+ </text>
1218
+ );
1219
+ }
1220
+
1221
+ /** Stable key per item for the windowed list. */
1222
+ function getItemKey(item: Item): string {
1223
+ switch (item.kind) {
1224
+ case "alias-name":
1225
+ return "alias-name";
1226
+ case "flag":
1227
+ return `flag:${item.row.flag.id}`;
1228
+ case "header":
1229
+ return `header:${item.label}`;
1230
+ }
1231
+ }
1232
+
1233
+ /** Dispatch render for one flat-list item. */
1234
+ function renderItem(
1235
+ item: Item,
1236
+ isSelected: boolean,
1237
+ isGated: (f: AliasFlag) => boolean,
1238
+ ): React.ReactNode {
1239
+ switch (item.kind) {
1240
+ case "alias-name":
1241
+ return renderAliasNameRow(item.row.value, isSelected);
1242
+ case "flag":
1243
+ return renderRow(item.row, isSelected, isGated(item.row.flag));
1244
+ case "header":
1245
+ return <text fg="#7e57c2">{item.label}</text>;
1246
+ }
1247
+ }
1248
+
1249
+
1250
+ function renderRow(
1251
+ row: FlagRow,
1252
+ selected: boolean,
1253
+ gated: boolean,
1254
+ ): React.ReactNode {
1255
+ // Prefer the precomputed effective state (e.g. --channels shows enabled when
1256
+ // a dev-loaded channel forces it to emit) over the flag's own enabled flag.
1257
+ const enabled = row.effectiveEnabled ?? isFlagEnabled(row.value);
1258
+ const marker = enabled ? "[x]" : "[ ]";
1259
+ const issueMark = row.issue ? " ⚠" : "";
1260
+ const fg = gated ? "gray" : enabled ? "white" : "gray";
1261
+ // Only show a value suffix when the flag carries non-redundant detail.
1262
+ // Boolean / tri-state state is fully conveyed by the checkbox. A precomputed
1263
+ // effectiveDetail (e.g. --channels union count) wins over the own-value one.
1264
+ const detail = row.effectiveDetail ?? enabledDetail(row.value);
1265
+ const detailSuffix = detail ? ` ${detail}` : "";
1266
+ const line = `${selected ? "▶ " : " "}${marker} ${row.flag.flag}${detailSuffix}${issueMark}`;
1267
+ if (selected) {
1268
+ return (
1269
+ <text key={row.flag.id} bg="#7e57c2" fg="white">
1270
+ {line}
1271
+ </text>
1272
+ );
1273
+ }
1274
+ return (
1275
+ <text key={row.flag.id} fg={fg}>
1276
+ {line}
1277
+ </text>
1278
+ );
1279
+ }
1280
+
1281
+ /**
1282
+ * Per-row trailing detail. Returns "" for booleans and tri-states (their state
1283
+ * is already in the checkbox) and for disabled flags (no need to advertise an
1284
+ * unset value). Non-empty for flags that carry an extra value the checkbox
1285
+ * doesn't capture.
1286
+ */
1287
+ function enabledDetail(value: FlagValue): string {
1288
+ switch (value.kind) {
1289
+ case "boolean":
1290
+ case "tri-state":
1291
+ return "";
1292
+ case "select":
1293
+ if (!value.enabled) return "";
1294
+ return value.value === "" ? "(bare)" : value.value;
1295
+ case "text":
1296
+ case "optional-text":
1297
+ if (!value.enabled) return "";
1298
+ return value.value ? truncate(value.value, 24) : "(empty)";
1299
+ case "text-list":
1300
+ if (!value.enabled) return "";
1301
+ return value.values.length === 0 ? "(empty)" : `${value.values.length}`;
1302
+ case "multi-with-custom": {
1303
+ if (!value.enabled) return "";
1304
+ const total = new Set([...value.picked, ...value.custom]).size;
1305
+ return total === 0 ? "(no filter)" : `${total} token(s)`;
1306
+ }
1307
+ }
1308
+ }
1309
+
1310
+ function renderDetail(
1311
+ row: Row | undefined,
1312
+ isGated: (f: AliasFlag) => boolean,
1313
+ aliasName: string,
1314
+ segments: Segment[],
1315
+ ): React.ReactNode {
1316
+ if (!row) {
1317
+ return <text fg="gray">No row selected.</text>;
1318
+ }
1319
+
1320
+ const headerAndBody =
1321
+ row.kind === "alias-name"
1322
+ ? renderAliasNameDetail(row)
1323
+ : renderFlagDetail(row, isGated);
1324
+
1325
+ // Preview is the only "global" element kept in the detail panel — it
1326
+ // updates as the user toggles, which is the strongest visual feedback for
1327
+ // "what alias am I building". We show argv one-per-line rather than the
1328
+ // raw POSIX-escaped alias body (which contains `'\''` sequences that look
1329
+ // alarming to readers who don't know the close-escape-reopen pattern).
1330
+ return (
1331
+ <box flexDirection="column">
1332
+ {headerAndBody}
1333
+ <text> </text>
1334
+ <text fg="gray">Preview:</text>
1335
+ {renderPreviewLines(aliasName, segments)}
1336
+ </box>
1337
+ );
1338
+ }
1339
+
1340
+ function renderAliasNameDetail(row: AliasNameRow): React.ReactNode {
1341
+ return (
1342
+ <box flexDirection="column">
1343
+ <text fg="white">
1344
+ <strong>Alias name</strong>
1345
+ </text>
1346
+ <text fg="gray">
1347
+ Shell name for the wrapped claude command. Default: "c". Letters,
1348
+ digits, _ or - only.
1349
+ </text>
1350
+ <text> </text>
1351
+ <text>
1352
+ <span fg="gray">Current: </span>
1353
+ <span fg="white">
1354
+ <strong>{row.value}</strong>
1355
+ </span>
1356
+ </text>
1357
+ <text> </text>
1358
+ <text fg="gray">Enter to rename. r resets to "c".</text>
1359
+ </box>
1360
+ );
1361
+ }
1362
+
1363
+ function renderFlagDetail(
1364
+ row: FlagRow,
1365
+ isGated: (f: AliasFlag) => boolean,
1366
+ ): React.ReactNode {
1367
+ const gated = isGated(row.flag);
1368
+ // State must agree with the checkbox and the Preview. For --channels we show
1369
+ // the effective channel-list summary (union of own + derived); other flags
1370
+ // use their own value summary.
1371
+ const stateText = row.stateSummary ?? summarizeValue(row.value);
1372
+ return (
1373
+ <box flexDirection="column">
1374
+ <text fg="white">
1375
+ <strong>{row.flag.flag}</strong>
1376
+ </text>
1377
+ <text fg="gray">{row.flag.description}</text>
1378
+ <text> </text>
1379
+ <text>
1380
+ <span fg="gray">State: </span>
1381
+ <span fg="white">{stateText}</span>
1382
+ </text>
1383
+ {gated && (
1384
+ <text fg="gray">
1385
+ {`Depends on --${row.flag.requires}. Enabling this will turn that on too.`}
1386
+ </text>
1387
+ )}
1388
+ {row.issue && (
1389
+ <text fg="yellow">{`Conflict: ${row.issue.reason}`}</text>
1390
+ )}
1391
+ </box>
1392
+ );
1393
+ }
1394
+
1395
+ /**
1396
+ * Render the alias preview as one argv arg per line. This replaces the raw
1397
+ * POSIX-escaped alias body (`alias c='claude '\''text with spaces'\'' …'`),
1398
+ * which is correct but visually alarming — readers without POSIX shell
1399
+ * fluency mistake `'\''` for broken quoting.
1400
+ *
1401
+ * We show each argv token on its own row, with values that need quoting
1402
+ * wrapped in plain double quotes (display-only — these are NOT what gets
1403
+ * written to the rc file). Composite segments (templated values) display
1404
+ * with their `{token}` placeholders intact in cyan so users see exactly
1405
+ * what will expand at shell-eval time.
1406
+ *
1407
+ * Example output:
1408
+ * alias c='claude
1409
+ * --dangerously-skip-permissions
1410
+ * --append-system-prompt "you ae the smaretes model ever"
1411
+ * --debug plugins
1412
+ * '
1413
+ */
1414
+ function renderPreviewLines(
1415
+ aliasName: string,
1416
+ segments: Segment[],
1417
+ ): React.ReactNode {
1418
+ if (segments.length === 0) {
1419
+ return <text fg="white">{`alias ${aliasName || "?"}='claude'`}</text>;
1420
+ }
1421
+ const lines: React.ReactNode[] = [
1422
+ <text key="head" fg="white">{`alias ${aliasName || "?"}='claude`}</text>,
1423
+ ];
1424
+ segments.forEach((seg, i) => {
1425
+ lines.push(
1426
+ <text key={`arg-${i}`} fg="white">
1427
+ {" "}
1428
+ {renderPreviewSegment(seg)}
1429
+ </text>,
1430
+ );
1431
+ });
1432
+ lines.push(
1433
+ <text key="tail" fg="white">
1434
+ {"'"}
1435
+ </text>,
1436
+ );
1437
+ return <>{lines}</>;
1438
+ }
1439
+
1440
+ function renderPreviewSegment(seg: Segment): React.ReactNode {
1441
+ if (seg.kind === "literal") {
1442
+ return <span fg="white">{quoteForDisplay(seg.text)}</span>;
1443
+ }
1444
+ // Composite: literal parts as-is, raw parts as `{token}` reconstructed
1445
+ // from the writer's TOKEN_TO_SUB map. We don't have direct access to the
1446
+ // map here, so we reverse-lookup using the same forms.
1447
+ const spans: React.ReactNode[] = [];
1448
+ seg.parts.forEach((p, i) => {
1449
+ if (p.kind === "literal") {
1450
+ spans.push(
1451
+ <span key={`p-${i}`} fg="white">
1452
+ {p.text}
1453
+ </span>,
1454
+ );
1455
+ } else {
1456
+ spans.push(
1457
+ <span key={`p-${i}`} fg="cyan">
1458
+ {substitutionToToken(p.posix)}
1459
+ </span>,
1460
+ );
1461
+ }
1462
+ });
1463
+ return <span>{spans}</span>;
1464
+ }
1465
+
1466
+ const PREVIEW_SAFE = /^[A-Za-z0-9_\-=,:.\/!@%+]+$/;
1467
+
1468
+ function quoteForDisplay(text: string): string {
1469
+ if (text === "") return '""';
1470
+ if (PREVIEW_SAFE.test(text)) return text;
1471
+ // Display-only quoting. We don't have to escape anything for the shell
1472
+ // since this string never reaches a parser — it's just for the user to
1473
+ // read. We do escape an embedded `"` to keep visual balance.
1474
+ return `"${text.replace(/"/g, '\\"')}"`;
1475
+ }
1476
+
1477
+ /**
1478
+ * Reverse-lookup the writer's POSIX substitution codes to their `{token}`
1479
+ * forms. Mirror of `TOKEN_TO_SUB` in `alias-shell-writer.ts`.
1480
+ */
1481
+ function substitutionToToken(posixCode: string): string {
1482
+ switch (posixCode) {
1483
+ case '$(basename "$PWD")':
1484
+ return "{folder}";
1485
+ case "$(date +%d)":
1486
+ return "{day}";
1487
+ case "$(date +%m)":
1488
+ return "{month}";
1489
+ case "$(date +%Y)":
1490
+ return "{year}";
1491
+ default:
1492
+ return posixCode;
1493
+ }
1494
+ }
1495
+
1496
+
1497
+
1498
+ function renderFlagDetailRightPane(
1499
+ flag: AliasFlag,
1500
+ value: FlagValue,
1501
+ aliasName: string,
1502
+ segments: Segment[],
1503
+ derivedNote?: string,
1504
+ stateOverride?: string,
1505
+ ): React.ReactNode {
1506
+ return (
1507
+ <box flexDirection="column">
1508
+ <text fg="white">
1509
+ <strong>{flag.flag}</strong>
1510
+ </text>
1511
+ <text fg="gray">{flag.description}</text>
1512
+ {derivedNote && (
1513
+ <>
1514
+ <text> </text>
1515
+ <text fg="gray">{derivedNote}</text>
1516
+ </>
1517
+ )}
1518
+ <text> </text>
1519
+ <text>
1520
+ <span fg="gray">State: </span>
1521
+ <span fg="white">{stateOverride ?? summarizeValue(value)}</span>
1522
+ </text>
1523
+ <text> </text>
1524
+ <text fg="gray">Preview:</text>
1525
+ {renderPreviewLines(aliasName, segments)}
1526
+ <text> </text>
1527
+ <text fg="gray">Keys:</text>
1528
+ <text fg="white"> space/enter toggle the highlighted row</text>
1529
+ <text fg="white"> a add a new value</text>
1530
+ <text fg="white"> d delete the highlighted item</text>
1531
+ <text fg="white"> h back to flag list</text>
1532
+ </box>
1533
+ );
1534
+ }