fisi-dock 0.0.0-stage → 0.3.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.
@@ -0,0 +1,274 @@
1
+ import { actionDetail, addAction, oddAction, stepSummary, type OddAction, type StepActions } from "./odd-detail.ts";
2
+ import { inferOddPhase, oddPhase, type OddPhase } from "./odd-inference.ts";
3
+ import { orbFrameIndex, SHIMMER_STEP_MS, shimmerStepIndex, type OrbMotion } from "./orbs.ts";
4
+ import type { OddInfo, OddStep } from "./panel-model.ts";
5
+
6
+ // The ODD phase timeline of one request (S6): every phase change closes the
7
+ // running step and opens the next, in occurrence order. Times come from an
8
+ // injected clock, so the reducer and the tracker run the same under tests.
9
+
10
+ /** Steps kept and drawn; older ones only count toward "+N earlier" (S7). */
11
+ export const ODD_STEP_CAP = 6;
12
+ /** The tick under potato motion, where only the seconds move. */
13
+ const STILL_TICK_MS = 1000;
14
+
15
+ interface TimedStep {
16
+ phase: OddPhase;
17
+ /** Milliseconds from the clock. */
18
+ start: number;
19
+ /** Set once the step is closed. */
20
+ end?: number;
21
+ /** What the step did (S60), bounded. */
22
+ acts?: StepActions;
23
+ }
24
+
25
+ export interface Timeline {
26
+ steps: readonly TimedStep[];
27
+ /** Closed steps dropped past the cap. */
28
+ earlier: number;
29
+ }
30
+
31
+ export function emptyTimeline(): Timeline {
32
+ return { steps: [], earlier: 0 };
33
+ }
34
+
35
+ function closeLast(steps: readonly TimedStep[], now: number): TimedStep[] {
36
+ const last = steps.at(-1);
37
+ return last && last.end === undefined ? [...steps.slice(0, -1), { ...last, end: Math.max(last.start, now) }] : [...steps];
38
+ }
39
+
40
+ /** Opens `phase` as the active step; the same phase as the active step changes nothing. */
41
+ export function enterPhase(timeline: Timeline, phase: OddPhase, now: number): Timeline {
42
+ const last = timeline.steps.at(-1);
43
+ if (last && last.end === undefined && last.phase === phase) return timeline;
44
+ const steps = [...closeLast(timeline.steps, now), { phase, start: now }];
45
+ const dropped = Math.max(0, steps.length - ODD_STEP_CAP);
46
+ return { steps: steps.slice(dropped), earlier: timeline.earlier + dropped };
47
+ }
48
+
49
+ /** Records `action` on the active step (S60); without one, nothing changes. */
50
+ export function recordAction(timeline: Timeline, action: OddAction): Timeline {
51
+ const last = timeline.steps.at(-1);
52
+ if (!last || last.end !== undefined) return timeline;
53
+ return { ...timeline, steps: [...timeline.steps.slice(0, -1), { ...last, acts: addAction(last.acts, action) }] };
54
+ }
55
+
56
+ /** Closes the active step, if any. */
57
+ export function endRun(timeline: Timeline, now: number): Timeline {
58
+ const last = timeline.steps.at(-1);
59
+ return last && last.end === undefined ? { ...timeline, steps: closeLast(timeline.steps, now) } : timeline;
60
+ }
61
+
62
+ /** The panel's odd section at `now`, or undefined while the timeline is empty. */
63
+ export function timelineView(timeline: Timeline, now: number): OddInfo | undefined {
64
+ if (timeline.steps.length === 0) return undefined;
65
+ const steps: OddStep[] = timeline.steps.map((step) => {
66
+ const active = step.end === undefined;
67
+ // The active step says what its last tool is doing, a done one sums up what it did (S60).
68
+ const detail = active ? step.acts && actionDetail(step.acts.last) : stepSummary(step.acts);
69
+ return {
70
+ phase: step.phase,
71
+ state: active ? "active" : "done",
72
+ seconds: Math.max(0, Math.floor(((step.end ?? now) - step.start) / 1000)),
73
+ ...(detail ? { detail } : {}),
74
+ };
75
+ });
76
+ return timeline.earlier > 0 ? { steps, earlier: timeline.earlier } : { steps };
77
+ }
78
+
79
+ // Gentle Shell's process-wide OddPhaseRegistry (lib/odd-phase.ts). It holds the
80
+ // current phase per Pi session id, from gentle_odd_phase reports and its own
81
+ // tool inference, and is cleared at agent_start, agent_settled and shutdown. It
82
+ // has no change event, so Fisi Dock samples get(sessionId) on its own triggers.
83
+ export const ODD_REGISTRY_KEY = Symbol.for("gentle-pi.odd-phase-registry");
84
+
85
+ /** The registry's phase for `sessionId`; undefined when absent, empty or malformed. */
86
+ export function registryPhase(sessionId: string, scope: object = globalThis): OddPhase | undefined {
87
+ try {
88
+ const registry = (scope as Record<symbol, unknown>)[ODD_REGISTRY_KEY] as { get?: unknown } | undefined;
89
+ if (!registry || typeof registry.get !== "function") return undefined;
90
+ return oddPhase(registry.get(sessionId));
91
+ } catch {
92
+ return undefined;
93
+ }
94
+ }
95
+
96
+ export interface OddTrackerOptions {
97
+ sessionId: string;
98
+ now(): number;
99
+ /** Called with the new view whenever the timeline may have changed. */
100
+ onChange(view: OddInfo | undefined): void;
101
+ /** The session folder; action paths under it are shown relative (S60). */
102
+ cwd?: string;
103
+ /** Where the registry lives; globalThis in Pi. */
104
+ scope?: object;
105
+ /** True while this session's subagents still run: agent_end then keeps the active step open (S91). */
106
+ busy?(): boolean;
107
+ /** Gentle Shell's animation level, read at each new request; quality when absent. */
108
+ motion?(): OrbMotion;
109
+ setInterval?(tick: () => void, ms: number): unknown;
110
+ clearInterval?(handle: unknown): void;
111
+ }
112
+
113
+ export interface OddTracker {
114
+ /** agent_start: a new request after a settled run, else a continuation. */
115
+ runStart(): void;
116
+ toolStart(event: { toolName?: string; args?: unknown }): void;
117
+ /** Reads the registry again (turn events, tool ends). */
118
+ sample(): void;
119
+ /** agent_end: closes the active step, or holds it open while `busy` (S91). */
120
+ runEnd(): void;
121
+ /** The subagents changed: a step held past agent_end closes once none runs (S91). */
122
+ busyChanged(): void;
123
+ /** agent_settled: the request is over (the view says so, S52); the next agent_start starts afresh. */
124
+ runSettled(): void;
125
+ view(): OddInfo | undefined;
126
+ dispose(): void;
127
+ }
128
+
129
+ /**
130
+ * One session's timeline. The registry wins whenever it names a phase; the
131
+ * tool-event fallback applies only while it has named none in this request.
132
+ * While a step is active a single timer recomputes the view every shine step
133
+ * (75 ms, S49), so the label shine, the orb frame (from the clock, S17) and the
134
+ * live seconds move; potato motion keeps a 1 s tick. It stops at run end and on dispose.
135
+ */
136
+ export function createOddTracker(options: OddTrackerOptions): OddTracker {
137
+ const scope = options.scope ?? globalThis;
138
+ const startTimer = options.setInterval ?? ((tick: () => void, ms: number) => {
139
+ const handle = setInterval(tick, ms);
140
+ (handle as { unref?(): void }).unref?.();
141
+ return handle;
142
+ });
143
+ const stopTimer = options.clearInterval ?? ((handle: unknown) => clearInterval(handle as ReturnType<typeof setInterval>));
144
+ let timeline = emptyTimeline();
145
+ /** The request's first phase start, kept when its step drops past the cap (S52). */
146
+ let firstStart: number | undefined;
147
+ let running = false;
148
+ let settled = true;
149
+ let registrySeen = false;
150
+ /** agent_end came while subagents ran; the step stays open until they finish or the run settles (S91). */
151
+ let held = false;
152
+ let timer: unknown;
153
+ let timerMs = 0;
154
+ let disposed = false;
155
+ let motion: OrbMotion = "quality";
156
+ const readMotion = () => {
157
+ try {
158
+ motion = options.motion?.() ?? "quality";
159
+ } catch {
160
+ motion = "quality";
161
+ }
162
+ };
163
+
164
+ /** The view with the active step's orb frame and shine step at `now`; once settled, the request's wall time. */
165
+ const view = (): OddInfo | undefined => {
166
+ const now = options.now();
167
+ const info = timelineView(timeline, now);
168
+ if (!info) return info;
169
+ if (settled) {
170
+ const last = timeline.steps.at(-1)!;
171
+ const totalSeconds = Math.max(0, Math.floor(((last.end ?? now) - (firstStart ?? timeline.steps[0].start)) / 1000));
172
+ return { ...info, settled: true, totalSeconds };
173
+ }
174
+ const steps = info.steps.map((step) => {
175
+ if (step.state !== "active") return step;
176
+ const scan = shimmerStepIndex(now, motion);
177
+ return { ...step, frame: orbFrameIndex(step.phase, now, motion), ...(scan === undefined ? {} : { scan }) };
178
+ });
179
+ return { ...info, steps };
180
+ };
181
+ const emit = () => {
182
+ if (!disposed) options.onChange(view());
183
+ };
184
+ const clearTimer = () => {
185
+ if (timer === undefined) return;
186
+ stopTimer(timer);
187
+ timer = undefined;
188
+ };
189
+ const enter = (phase: OddPhase | undefined) => {
190
+ if (!phase || !running) return;
191
+ timeline = enterPhase(timeline, phase, options.now());
192
+ firstStart ??= timeline.steps[0].start;
193
+ const ms = motion === "potato" ? STILL_TICK_MS : SHIMMER_STEP_MS;
194
+ if (timer !== undefined && timerMs === ms) return;
195
+ clearTimer();
196
+ timerMs = ms;
197
+ timer = startTimer(tick, ms);
198
+ };
199
+ const fromRegistry = (): OddPhase | undefined => {
200
+ const phase = registryPhase(options.sessionId, scope);
201
+ if (phase) registrySeen = true;
202
+ return phase;
203
+ };
204
+ const tick = () => {
205
+ if (disposed) return;
206
+ enter(fromRegistry());
207
+ emit();
208
+ };
209
+ const isBusy = () => {
210
+ try {
211
+ return options.busy?.() === true;
212
+ } catch {
213
+ return false;
214
+ }
215
+ };
216
+ const stop = () => {
217
+ held = false;
218
+ if (!running) return false;
219
+ running = false;
220
+ timeline = endRun(timeline, options.now());
221
+ clearTimer();
222
+ return true;
223
+ };
224
+
225
+ return {
226
+ runStart() {
227
+ if (disposed) return;
228
+ if (settled) {
229
+ timeline = emptyTimeline();
230
+ firstStart = undefined;
231
+ registrySeen = false;
232
+ readMotion();
233
+ }
234
+ settled = false;
235
+ running = true;
236
+ held = false;
237
+ enter(fromRegistry());
238
+ emit();
239
+ },
240
+ toolStart(event) {
241
+ if (disposed || !running) return;
242
+ const registry = fromRegistry();
243
+ enter(registry ?? (registrySeen ? undefined : inferOddPhase(event?.toolName ?? "", event?.args)));
244
+ const action = oddAction(event?.toolName ?? "", event?.args, options.cwd);
245
+ if (action) timeline = recordAction(timeline, action);
246
+ emit();
247
+ },
248
+ sample: tick,
249
+ runEnd() {
250
+ if (disposed) return;
251
+ if (running && isBusy()) {
252
+ held = true;
253
+ return;
254
+ }
255
+ if (stop()) emit();
256
+ },
257
+ busyChanged() {
258
+ if (disposed || !held || isBusy()) return;
259
+ if (stop()) emit();
260
+ },
261
+ runSettled() {
262
+ if (disposed) return;
263
+ stop();
264
+ settled = true;
265
+ emit();
266
+ },
267
+ view,
268
+ dispose() {
269
+ disposed = true;
270
+ running = false;
271
+ clearTimer();
272
+ },
273
+ };
274
+ }
package/lib/orbs.ts ADDED
@@ -0,0 +1,181 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { isAbsolute, join } from "node:path";
3
+ import type { OddPhase } from "./odd-inference.ts";
4
+ import { fg, palette, type Rgb } from "./palette.ts";
5
+ import { padEnd, visibleWidth } from "./text.ts";
6
+
7
+ // Animated braille orbs per ODD phase (S17), from artboard 09
8
+ // (design/gentle-shell-redesign/09-phase-orbs.html, ORBS table). Frames are a
9
+ // pure function of the clock, so the panel and the tests agree on what shows.
10
+
11
+ export interface Orb {
12
+ /** Every frame padded to the same visible width. */
13
+ frames: readonly string[];
14
+ color: Rgb;
15
+ /** Milliseconds per frame at full rate. */
16
+ ms: number;
17
+ }
18
+
19
+ /** Gentle Shell's animation levels (/gentle:animations). */
20
+ export type OrbMotion = "quality" | "performance" | "potato";
21
+
22
+ export const STATIC_ORB = "●";
23
+ /** The static tick: potato motion and unknown phases only move the m:ss. */
24
+ const STILL_TICK_MS = 1000;
25
+
26
+ function orb(color: Rgb, ms: number, frames: string[]): Orb {
27
+ const width = Math.max(...frames.map(visibleWidth));
28
+ return { color, ms, frames: frames.map((frame) => padEnd(frame, width)) };
29
+ }
30
+
31
+ export const ORBS: Record<OddPhase, Orb> = {
32
+ exploring: orb(palette.teal, 80, ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]),
33
+ researching: orb(palette.lilac, 110, ["⢄⠀", "⢂⠀", "⢁⠀", "⡁⠀", "⡈⢀", "⡐⢄", "⡠⢂", "⠠⡁", "⠀⡈", "⠀⡐", "⠀⡠", "⠀⠠"]),
34
+ authorizing: orb(palette.coral, 140, ["⠁⠀⠈", "⠂⠀⠐", "⠄⠀⠠", "⡀⣀⢀", "⠄⠀⠠", "⠂⠀⠐"]),
35
+ deciding: orb(palette.pink, 160, ["⠀", "⠂", "⠒", "⠶", "⣶", "⣿", "⣶", "⠶", "⠒", "⠂"]),
36
+ planning: orb(palette.blue, 180, ["⠄⠀⠀", "⠆⠀⠀", "⠇⠄⠀", "⠇⠆⠀", "⠇⠇⠄", "⠇⠇⠆", "⠇⠇⠇", "⠀⠀⠀"]),
37
+ implementing: orb(palette.rose, 120, ["⡀", "⡄", "⡆", "⡇", "⣇", "⣧", "⣷", "⣿", "⣾", "⣼", "⣸", "⢸", "⠸", "⠘", "⠈", "⠀"]),
38
+ checking: orb(palette.amber, 90, ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"]),
39
+ closing: orb(palette.green, 150, ["⣿", "⡿", "⠿", "⠟", "⠛", "⠋", "⠉", "⠁", "⠀", "⠁", "⠉", "⠋", "⠛", "⠟", "⠿", "⡿"]),
40
+ };
41
+
42
+ /** The orb of `phase`, or undefined for a phase the artboard does not name. */
43
+ export function orbOf(phase: string): Orb | undefined {
44
+ return Object.hasOwn(ORBS, phase) ? ORBS[phase as OddPhase] : undefined;
45
+ }
46
+
47
+ /** Visible width of every frame of `phase`; 1 for the static dot. */
48
+ export function orbWidth(phase: string): number {
49
+ const found = orbOf(phase);
50
+ return found ? visibleWidth(found.frames[0]) : 1;
51
+ }
52
+
53
+ /** Milliseconds between visible frame changes; the 1 s tick when nothing animates. */
54
+ export function orbInterval(phase: string, motion: OrbMotion = "quality"): number {
55
+ const found = orbOf(phase);
56
+ if (!found || motion === "potato") return STILL_TICK_MS;
57
+ // S29: performance used to halve the rate and looked sluggish; only potato stops motion.
58
+ return found.ms;
59
+ }
60
+
61
+ /** The still frame: the middle one, as the artboard shows under reduced motion. */
62
+ export function orbStillIndex(phase: string): number {
63
+ return Math.floor((orbOf(phase)?.frames.length ?? 0) / 2);
64
+ }
65
+
66
+ /** Frame index at `nowMs`; potato and unknown phases stay on the still frame. */
67
+ export function orbFrameIndex(phase: string, nowMs: number, motion: OrbMotion = "quality"): number {
68
+ const found = orbOf(phase);
69
+ if (!found || motion === "potato") return orbStillIndex(phase);
70
+ return Math.floor(Math.max(0, nowMs) / orbInterval(phase, motion)) % found.frames.length;
71
+ }
72
+
73
+ /** Frame `index` of `phase` (the still frame when undefined), or the static dot. */
74
+ export function orbAt(phase: string, index?: number): string {
75
+ const found = orbOf(phase);
76
+ if (!found) return STATIC_ORB;
77
+ const count = found.frames.length;
78
+ const at = index === undefined || !Number.isFinite(index) ? orbStillIndex(phase) : ((Math.floor(index) % count) + count) % count;
79
+ return found.frames[at];
80
+ }
81
+
82
+ /** The visible orb of `phase` at `nowMs`. */
83
+ export function orbFrame(phase: string, nowMs: number, motion: OrbMotion = "quality"): string {
84
+ return orbAt(phase, orbFrameIndex(phase, nowMs, motion));
85
+ }
86
+
87
+ // The running spinner (S31) on active todo tasks and running agents: the
88
+ // classic braille dots, one column each, at the exploring orb's rate.
89
+ export const SPINNER_FRAMES: readonly string[] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
90
+ export const SPINNER_MS = 80;
91
+ /** ⠹, the glyph the panel showed before it moved: potato motion and no frame. */
92
+ const SPINNER_STILL = 2;
93
+
94
+ /** Spinner frame index at `nowMs`; potato stays on the still frame. */
95
+ export function spinnerFrameIndex(nowMs: number, motion: OrbMotion = "quality"): number {
96
+ if (motion === "potato") return SPINNER_STILL;
97
+ return Math.floor(Math.max(0, nowMs) / SPINNER_MS) % SPINNER_FRAMES.length;
98
+ }
99
+
100
+ /** Spinner frame `index`, the still one when undefined. */
101
+ export function spinnerAt(index?: number): string {
102
+ const count = SPINNER_FRAMES.length;
103
+ const at = index === undefined || !Number.isFinite(index) ? SPINNER_STILL : ((Math.floor(index) % count) + count) % count;
104
+ return SPINNER_FRAMES[at];
105
+ }
106
+
107
+ // The active ODD label's shine (S49), after Gentle Shell's prompt scanner
108
+ // (lib/shell-prompt.ts scanWorkingText): a white head walks a muted label one
109
+ // letter per step, entering 3 cells before it and leaving 3 cells after it.
110
+ export const SHIMMER_STEP_MS = 75;
111
+ /** Cells the head spends before the first letter and after the last one. */
112
+ const SHIMMER_ENTRY = 3;
113
+ /** Head, then its two neighbours on each side; everything else is muted. */
114
+ const SHIMMER_COLORS: readonly Rgb[] = [[0xff, 0xff, 0xff], [0xc8, 0xc8, 0xc8], [0x9a, 0x9a, 0x9a]];
115
+
116
+ /** Shine step at `nowMs`; undefined under potato, where the label stays still. */
117
+ export function shimmerStepIndex(nowMs: number, motion: OrbMotion = "quality"): number | undefined {
118
+ if (motion === "potato") return undefined;
119
+ return Math.floor(Math.max(0, nowMs) / SHIMMER_STEP_MS);
120
+ }
121
+
122
+ /** `text` in muted gray with the white head at `step`; all muted without a step. */
123
+ export function shimmerText(text: string, step?: number): string {
124
+ const chars = Array.from(text);
125
+ if (step === undefined || !Number.isFinite(step)) return fg(palette.muted, text);
126
+ const cycle = chars.length + SHIMMER_ENTRY * 2;
127
+ const head = (((Math.floor(step) % cycle) + cycle) % cycle) - SHIMMER_ENTRY;
128
+ let out = "";
129
+ let run = "";
130
+ let runColor: Rgb = palette.muted;
131
+ chars.forEach((char, index) => {
132
+ const color = SHIMMER_COLORS[Math.abs(index - head)] ?? palette.muted;
133
+ if (color !== runColor) {
134
+ out += fg(runColor, run);
135
+ run = "";
136
+ runColor = color;
137
+ }
138
+ run += char;
139
+ });
140
+ return out + fg(runColor, run);
141
+ }
142
+
143
+ // Gentle Shell keeps its animation level in <config home>/animations.json
144
+ // (lib/animation-policy.ts, schema gentle-pi.animations/v1). The config home
145
+ // rule is restated from lib/gentle-shell-bundled.ts resolveGentlePiConfigHome,
146
+ // without its bundled-install validation: a bundled prefix's file is tried
147
+ // first and skipped when absent. FISI_DOCK_ORBS=off forces the still orb.
148
+ const ANIMATION_SCHEMA = "gentle-pi.animations/v1";
149
+
150
+ type Env = Record<string, string | undefined>;
151
+
152
+ function policyFrom(raw: string): OrbMotion | undefined {
153
+ try {
154
+ const value = JSON.parse(raw) as { schema?: unknown; policy?: unknown } | null;
155
+ if (!value || typeof value !== "object" || value.schema !== ANIMATION_SCHEMA) return undefined;
156
+ return value.policy === "quality" || value.policy === "performance" || value.policy === "potato" ? value.policy : undefined;
157
+ } catch {
158
+ return undefined;
159
+ }
160
+ }
161
+
162
+ /** The orb motion for this process; quality when nothing says otherwise. */
163
+ export function readOrbMotion(env: Env, home: string, read: (path: string) => string = (path) => readFileSync(path, "utf8")): OrbMotion {
164
+ if (env.FISI_DOCK_ORBS?.toLowerCase() === "off") return "potato";
165
+ const attempt = (dir: string): OrbMotion | "missing" => {
166
+ try {
167
+ return policyFrom(read(join(dir, "animations.json"))) ?? "quality";
168
+ } catch (error) {
169
+ return (error as { code?: unknown })?.code === "ENOENT" ? "missing" : "quality";
170
+ }
171
+ };
172
+ const prefix = env.GENTLE_SHELL_PREFIX;
173
+ if (prefix && isAbsolute(prefix)) {
174
+ const bundled = attempt(join(prefix, "config", "gentle-ai"));
175
+ if (bundled !== "missing") return bundled;
176
+ }
177
+ const marker = env.GENTLE_SHELL_CONFIG_HOME;
178
+ const dir = env.GENTLE_PI_CONFIG_HOME || (marker && isAbsolute(marker) ? marker : join(home, ".pi", "gentle-ai"));
179
+ const motion = attempt(dir);
180
+ return motion === "missing" ? "quality" : motion;
181
+ }
package/lib/palette.ts ADDED
@@ -0,0 +1,84 @@
1
+ // Design 07 palette in 24-bit color. The panel paints with its own accents
2
+ // instead of Pi theme roles so each section keeps its hue (S9).
3
+
4
+ export type Rgb = readonly [number, number, number];
5
+
6
+ export const BACKGROUND: Rgb = [0x16, 0x16, 0x16];
7
+
8
+ export const palette = {
9
+ pink: [0xf2, 0xa7, 0xc9],
10
+ /** The theme's deepPink (mdQuoteBorder): the deepest tone of Gentle Shell's petal. */
11
+ deepPink: [0xc9, 0x77, 0x9f],
12
+ teal: [0x5f, 0xd4, 0xe0],
13
+ coral: [0xff, 0x9b, 0x85],
14
+ amber: [0xe0, 0xaf, 0x68],
15
+ lilac: [0xc3, 0xa6, 0xff],
16
+ green: [0x8f, 0xc9, 0x8f],
17
+ blue: [0x7b, 0x9c, 0xff],
18
+ red: [0xff, 0x6b, 0x6b],
19
+ /** High agent effort (S56). */
20
+ purple: [0xb4, 0x8c, 0xff],
21
+ /** Gentle Shell's Gentleman-Sexy theme accent and its deepPink: the wordmark's SHELL. */
22
+ sexyPink: [0xf4, 0x38, 0x88],
23
+ sexyDeepPink: [0xbf, 0x0f, 0x50],
24
+ /** Gentle Shell's banner rose. */
25
+ rose: [0xff, 0x76, 0xc3],
26
+ text: [0xe1, 0xe1, 0xe1],
27
+ muted: [0x6c, 0x6c, 0x6c],
28
+ faint: [0x4a, 0x4a, 0x4a],
29
+ /** Section frames: one neutral gray, Grok-style, same as Gentle Shell's welcome card frame; titles carry the hue. */
30
+ frame: [0x45, 0x45, 0x45],
31
+ highlight: [0x26, 0x26, 0x26],
32
+ } as const satisfies Record<string, Rgb>;
33
+
34
+ export const RESET = "\x1b[0m";
35
+
36
+ /** Blend `color` over the panel background, keeping `amount` of the color. */
37
+ export function dim(color: Rgb, amount = 0.4): Rgb {
38
+ return color.map((channel, index) => Math.round(channel * amount + BACKGROUND[index] * (1 - amount))) as unknown as Rgb;
39
+ }
40
+
41
+ /** The WCAG contrast of `color` on the panel background. */
42
+ function contrast(color: Rgb): number {
43
+ const luminance = (rgb: Rgb) => {
44
+ const [r, g, b] = rgb.map((channel) => channel / 255).map((value) => (value <= 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4));
45
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
46
+ };
47
+ return (luminance(color) + 0.05) / (luminance(BACKGROUND) + 0.05);
48
+ }
49
+
50
+ /** `color`'s hue at HSL saturation 0.9 and `lightness`. */
51
+ function withLightness(color: Rgb, lightness: number): Rgb {
52
+ const [r, g, b] = color.map((channel) => channel / 255);
53
+ const max = Math.max(r, g, b);
54
+ const spread = max - Math.min(r, g, b);
55
+ let hue = 0;
56
+ if (spread) hue = max === r ? ((g - b) / spread) % 6 : max === g ? (b - r) / spread + 2 : (r - g) / spread + 4;
57
+ hue = (hue * 60 + 360) % 360;
58
+ const chroma = (1 - Math.abs(2 * lightness - 1)) * 0.9;
59
+ const x = chroma * (1 - Math.abs(((hue / 60) % 2) - 1));
60
+ const m = lightness - chroma / 2;
61
+ const sextant = Math.floor(hue / 60);
62
+ const [r1, g1, b1] = [[chroma, x, 0], [x, chroma, 0], [0, chroma, x], [0, x, chroma], [x, 0, chroma], [chroma, 0, x]][sextant];
63
+ return [r1, g1, b1].map((value) => Math.round((value + m) * 255)) as unknown as Rgb;
64
+ }
65
+
66
+ /**
67
+ * The vividness of the cover title #f43888 on `color`'s own hue (vivid labels S1):
68
+ * HSL saturation 90%, lightness 59%; a hue that then reads under 4.5:1 on the
69
+ * background gets lighter in steps of 0.01 until it does, at most 0.80.
70
+ */
71
+ export function vivid(color: Rgb): Rgb {
72
+ for (let step = 59; step < 80; step++) {
73
+ const out = withLightness(color, step / 100);
74
+ if (contrast(out) >= 4.5) return out;
75
+ }
76
+ return withLightness(color, 0.8);
77
+ }
78
+
79
+ export const fgCode = ([r, g, b]: Rgb) => `\x1b[38;2;${r};${g};${b}m`;
80
+ export const bgCode = ([r, g, b]: Rgb) => `\x1b[48;2;${r};${g};${b}m`;
81
+
82
+ // Style closers reset only their own attribute, so a row background survives.
83
+ export const fg = (color: Rgb, text: string) => (text ? `${fgCode(color)}${text}\x1b[39m` : "");
84
+ export const bold = (text: string) => (text ? `\x1b[1m${text}\x1b[22m` : "");