@@ -0,0 +1,875 @@
1+/**
2+ * cel-tui: Virtual Pet (Tamagotchi)
3+ *
4+ * A feature-rich example exercising the full API surface:
5+ * - Layers (creation modal, log modal, death screen overlay)
6+ * - TextInput (pet name)
7+ * - Focus traversal (Tab/Shift+Tab/Enter/Escape)
8+ * - Controlled scroll (activity log)
9+ * - Timers & animation frames
10+ * - Buttons, key bindings, click handlers
11+ * - External state management
12+ *
13+ * Run: bun run examples/pet.ts
14+ */
15+
16+import {
17+ cel,
18+ VStack,
19+ HStack,
20+ Text,
21+ TextInput,
22+ ProcessTerminal,
23+} from "@cel-tui/core";
24+import { Button, Divider, Spacer } from "@cel-tui/components";
25+
26+// ─── Pet Definitions ────────────────────────────────────────────
27+
28+interface PetDef {
29+ kind: string;
30+ idle: { happy: string[][]; hungry: string[][] };
31+ excited: string[][];
32+ dead: string[];
33+}
34+
35+const PETS: PetDef[] = [
36+ {
37+ kind: "Cat",
38+ idle: {
39+ happy: [
40+ [
41+ " /\\_/\\ ",
42+ " ( ^.^ ) ",
43+ " > ^ < ",
44+ " /| |\\ ",
45+ " (_| |_) ",
46+ ],
47+ [
48+ " /\\_/\\ ",
49+ " ( ^.^ ) ",
50+ " > ^ < ",
51+ " /| |\\ ",
52+ " (_| |_) ",
53+ " ~ ~ ",
54+ ],
55+ ],
56+ hungry: [
57+ [
58+ " /\\_/\\ ",
59+ " ( T.T ) ",
60+ " > o < ",
61+ " /| |\\ ",
62+ " (_| |_) ",
63+ ],
64+ [
65+ " /\\_/\\ ",
66+ " ( T.T ) ",
67+ " > o < ",
68+ " | | ",
69+ " _| |_ ",
70+ ],
71+ ],
72+ },
73+ excited: [
74+ [
75+ " /\\_/\\ ",
76+ " ( >.< ) ",
77+ " >\\^/< ",
78+ " \\| |/ ",
79+ " (_\\_/_) ",
80+ " \\ / ",
81+ ],
82+ [
83+ " /\\_/\\ ",
84+ " \\( ^o^ )/ ",
85+ " > ^ < ",
86+ " | | ",
87+ " (_\\_/_) ",
88+ " / \\ ",
89+ ],
90+ ],
91+ dead: [
92+ " /\\_/\\ ",
93+ " ( x.x ) ",
94+ " > - < ",
95+ " | | ",
96+ " |___| ",
97+ ],
98+ },
99+ {
100+ kind: "Dog",
101+ idle: {
102+ happy: [
103+ [
104+ " |\\_/| ",
105+ " | @ @ ",
106+ " | <> _ ",
107+ " |_/| |// ",
108+ " |/ / ",
109+ " / / ",
110+ ],
111+ [
112+ " |\\_/| ",
113+ " | @ @ ",
114+ " | <> _ ",
115+ " |_/| |// ",
116+ " |/ / ",
117+ " / |_/ ",
118+ ],
119+ ],
120+ hungry: [
121+ [
122+ " |\\_/| ",
123+ " | ; ; ",
124+ " | .. _ ",
125+ " |_/| |// ",
126+ " |/ / ",
127+ " / / ",
128+ ],
129+ [
130+ " |\\_/| ",
131+ " | ; ; ",
132+ " | .. ",
133+ " |_/| | ",
134+ " |/ | ",
135+ " / / ",
136+ ],
137+ ],
138+ },
139+ excited: [
140+ [
141+ " |\\_/| ",
142+ " | O O ",
143+ " | D> _ ",
144+ " |_/| |// ",
145+ " /|/ _/ ",
146+ " / |__/ ",
147+ ],
148+ [
149+ " |\\_/| ",
150+ " | O O ",
151+ " | D> ",
152+ " __|_/|\\__ ",
153+ " / |/ \\ ",
154+ " / | / ",
155+ ],
156+ ],
157+ dead: [
158+ " |\\_/| ",
159+ " | x x ",
160+ " | -- _ ",
161+ " |_/| |// ",
162+ " |/ / ",
163+ " /____/ ",
164+ ],
165+ },
166+ {
167+ kind: "Bunny",
168+ idle: {
169+ happy: [
170+ [" (\\(\\ ", " ( -.-) ", ' o_(")(") '],
171+ [" (\\(\\ ", " ( -.-) ", ' o_(")(") ', " * "],
172+ ],
173+ hungry: [
174+ [" (\\(\\ ", " ( ;.;) ", ' c_(")(") '],
175+ [" (\\(\\ ", " ( ;.;) ", ' c_(")(") ', " ... "],
176+ ],
177+ },
178+ excited: [
179+ [" (\\(\\ ", " ( ^.^) ", ' (")(")_o ', " \\|/ "],
180+ [" (\\(\\ ", " \\( ^o^)/ ", ' (")(") ', " /|\\ "],
181+ ],
182+ dead: [
183+ " (\\(\\ ",
184+ " ( x.x) ",
185+ ' c_(")(") ',
186+ " R.I.P ",
187+ ],
188+ },
189+];
190+
191+// ─── App State ──────────────────────────────────────────────────
192+
193+type Screen = "create" | "main" | "dead";
194+let screen: Screen = "create";
195+
196+// -- Creation state --
197+let petName = "";
198+let selectedKind = 0;
199+let focused: string | null = "name"; // tracks which element has focus
200+
201+// -- Pet state --
202+let pet: PetDef | null = null;
203+let health = 100;
204+let hunger = 0; // 0 = full, 100 = starving
205+let happiness = 100;
206+
207+// -- Animation --
208+let frame = 0;
209+let anim: "idle" | "excited" = "idle";
210+let excitedTimer: ReturnType<typeof setTimeout> | null = null;
211+
212+// -- Timers --
213+let tickTimer: ReturnType<typeof setInterval> | null = null;
214+let decayTimer: ReturnType<typeof setInterval> | null = null;
215+
216+// -- Activity log --
217+let log: string[] = [];
218+let showLog = false;
219+let logScroll = 0;
220+
221+// ─── Helpers ────────────────────────────────────────────────────
222+
223+function ts(): string {
224+ return new Date().toLocaleTimeString("en-US", { hour12: false });
225+}
226+
227+function addLog(msg: string) {
228+ log.push(`[${ts()}] ${msg}`);
229+ logScroll = Math.max(0, log.length - 12);
230+}
231+
232+function startGame() {
233+ pet = PETS[selectedKind]!;
234+ if (!petName.trim()) petName = pet.kind;
235+ health = 100;
236+ hunger = 0;
237+ happiness = 100;
238+ anim = "idle";
239+ frame = 0;
240+ log = [];
241+ screen = "main";
242+ focused = null;
243+
244+ addLog(`${petName} the ${pet.kind} was born!`);
245+ addLog("Press [F] feed · [P] pet · [L] log");
246+ startTimers();
247+ cel.render();
248+}
249+
250+function startTimers() {
251+ stopTimers();
252+ tickTimer = setInterval(() => {
253+ frame++;
254+ cel.render();
255+ }, 600);
256+
257+ decayTimer = setInterval(() => {
258+ if (screen !== "main") return;
259+ hunger = Math.min(100, hunger + 3);
260+ happiness = Math.max(0, happiness - 2);
261+ if (hunger >= 80) happiness = Math.max(0, happiness - 2);
262+ if (hunger >= 90 && happiness <= 20) health = Math.max(0, health - 5);
263+ if (health <= 0) return die();
264+ if (hunger >= 70) addLog(`${petName} is hungry...`);
265+ if (happiness <= 25) addLog(`${petName} feels lonely...`);
266+ if (health <= 30) addLog(`${petName} doesn't look well!`);
267+ cel.render();
268+ }, 3000);
269+}
270+
271+function stopTimers() {
272+ if (tickTimer) clearInterval(tickTimer);
273+ if (decayTimer) clearInterval(decayTimer);
274+ if (excitedTimer) clearTimeout(excitedTimer);
275+ tickTimer = decayTimer = excitedTimer = null;
276+}
277+
278+function feed() {
279+ if (screen !== "main") return;
280+ hunger = Math.max(0, hunger - 30);
281+ health = Math.min(100, health + 5);
282+ addLog(`You fed ${petName}! Hunger -30`);
283+ playExcited();
284+}
285+
286+function petIt() {
287+ if (screen !== "main") return;
288+ happiness = Math.min(100, happiness + 25);
289+ addLog(`You pet ${petName}! Happy +25`);
290+ playExcited();
291+}
292+
293+function playExcited() {
294+ anim = "excited";
295+ frame = 0;
296+ if (excitedTimer) clearTimeout(excitedTimer);
297+ excitedTimer = setTimeout(() => {
298+ anim = "idle";
299+ cel.render();
300+ }, 2400);
301+ cel.render();
302+}
303+
304+function die() {
305+ addLog(`${petName} has passed away...`);
306+ screen = "dead";
307+ focused = null;
308+ stopTimers();
309+ cel.render();
310+}
311+
312+function restart() {
313+ stopTimers();
314+ pet = null;
315+ petName = "";
316+ selectedKind = 0;
317+ focused = "name";
318+ screen = "create";
319+ log = [];
320+ showLog = false;
321+ cel.render();
322+}
323+
324+function quit() {
325+ stopTimers();
326+ cel.stop();
327+ process.exit(0);
328+}
329+
330+// ─── Art Helpers ────────────────────────────────────────────────
331+
332+function maxArtHeight(def: PetDef): number {
333+ const all = [
334+ ...def.idle.happy,
335+ ...def.idle.hungry,
336+ ...def.excited,
337+ [def.dead],
338+ ].flat();
339+ // all is string[][] flattened once → string[][]; we need max of each frame's length
340+ const frames = [
341+ ...def.idle.happy,
342+ ...def.idle.hungry,
343+ ...def.excited,
344+ def.dead,
345+ ];
346+ return Math.max(...frames.map((f) => f.length));
347+}
348+
349+function currentArt(): string[] {
350+ if (!pet) return [];
351+ let lines: string[];
352+ if (anim === "excited") {
353+ lines = pet.excited[frame % pet.excited.length]!;
354+ } else {
355+ const mood = hunger > 60 || happiness < 30 ? "hungry" : "happy";
356+ const frames = pet.idle[mood];
357+ lines = frames[frame % frames.length]!;
358+ }
359+ // Pad to consistent height so buttons don't bounce
360+ const target = maxArtHeight(pet);
361+ while (lines.length < target) lines = [...lines, ""];
362+ return lines;
363+}
364+
365+function artColor(): "cyan" | "yellow" | "magenta" {
366+ if (anim === "excited") return "magenta";
367+ if (hunger > 60 || happiness < 30) return "yellow";
368+ return "cyan";
369+}
370+
371+// ─── Reusable Widgets ───────────────────────────────────────────
372+
373+function StatBar(
374+ label: string,
375+ value: number,
376+ color: "green" | "yellow" | "red" | "cyan",
377+) {
378+ const w = 12;
379+ const filled = Math.round((value / 100) * w);
380+ return VStack({}, [
381+ Text(label, { bold: true, fgColor: "white" }),
382+ HStack({}, [
383+ Text("█", { repeat: Math.max(filled, 0), fgColor: color }),
384+ Text("░", { repeat: Math.max(w - filled, 0), fgColor: "brightBlack" }),
385+ Text(` ${value}`, { fgColor: "brightBlack" }),
386+ ]),
387+ ]);
388+}
389+
390+function barColor(val: number, invert = false): "green" | "yellow" | "red" {
391+ const v = invert ? 100 - val : val;
392+ if (v > 60) return "green";
393+ if (v > 30) return "yellow";
394+ return "red";
395+}
396+
397+// ─── Create Screen ──────────────────────────────────────────────
398+
399+function createView() {
400+ const preview = PETS[selectedKind]!;
401+ const previewArt = preview.idle.happy[frame % preview.idle.happy.length]!;
402+
403+ return VStack(
404+ {
405+ height: "100%",
406+ justifyContent: "center",
407+ alignItems: "center",
408+ onKeyPress: (key) => {
409+ if (key === "ctrl+q" || key === "ctrl+c") quit();
410+ // Arrow keys to pick species (only when name input is NOT focused)
411+ if (focused !== "name") {
412+ if (key === "up") {
413+ selectedKind = (selectedKind - 1 + PETS.length) % PETS.length;
414+ cel.render();
415+ } else if (key === "down") {
416+ selectedKind = (selectedKind + 1) % PETS.length;
417+ cel.render();
418+ }
419+ }
420+ },
421+ },
422+ [
423+ VStack({ width: 40, padding: { x: 2, y: 1 }, gap: 1 }, [
424+ // Title
425+ HStack({ justifyContent: "center" }, [
426+ Text("~ New Pet ~", { bold: true, fgColor: "cyan" }),
427+ ]),
428+ Divider({ fgColor: "brightBlack" }),
429+
430+ // Name
431+ Text("Name:", { bold: true, fgColor: "white" }),
432+ TextInput({
433+ value: petName,
434+ onChange: (v) => {
435+ petName = v;
436+ cel.render();
437+ },
438+ height: 1,
439+ fgColor: "brightWhite",
440+ bgColor: "black",
441+ placeholder: Text("enter a name...", {
442+ fgColor: "brightBlack",
443+ bgColor: "black",
444+ }),
445+ focused: focused === "name",
446+ onFocus: () => {
447+ focused = "name";
448+ cel.render();
449+ },
450+ onBlur: () => {
451+ if (focused === "name") focused = null;
452+ cel.render();
453+ },
454+ submitKey: "enter",
455+ onSubmit: () => {
456+ focused = null;
457+ cel.render();
458+ },
459+ }),
460+
461+ // Species picker
462+ Text("Species:", { bold: true, fgColor: "white" }),
463+ VStack({ padding: { x: 1 } }, [
464+ ...PETS.map((p, i) => {
465+ const sel = i === selectedKind;
466+ return HStack(
467+ {
468+ onClick: () => {
469+ selectedKind = i;
470+ cel.render();
471+ },
472+ focusable: false,
473+ },
474+ [
475+ Text(sel ? " > " : " ", {
476+ fgColor: sel ? "cyan" : "brightBlack",
477+ bold: sel,
478+ }),
479+ Text(p.kind, {
480+ fgColor: sel ? "cyan" : "white",
481+ bold: sel,
482+ }),
483+ ],
484+ );
485+ }),
486+ ]),
487+ Text(" arrow keys to change", {
488+ fgColor: "brightBlack",
489+ italic: true,
490+ }),
491+
492+ Divider({ fgColor: "brightBlack" }),
493+
494+ // Preview
495+ VStack(
496+ { alignItems: "center" },
497+ previewArt.map((ln) => Text(ln, { fgColor: "yellow" })),
498+ ),
499+
500+ Divider({ fgColor: "brightBlack" }),
501+
502+ // Create button
503+ HStack({ justifyContent: "center" }, [
504+ HStack(
505+ {
506+ onClick: startGame,
507+ focused: focused === "create",
508+ onFocus: () => {
509+ focused = "create";
510+ cel.render();
511+ },
512+ onBlur: () => {
513+ if (focused === "create") focused = null;
514+ cel.render();
515+ },
516+ },
517+ [
518+ Text(
519+ focused === "create" ? ">> Create Pet! <<" : "[ Create Pet! ]",
520+ { bold: true, fgColor: "green" },
521+ ),
522+ ],
523+ ),
524+ ]),
525+
526+ // Hints
527+ HStack({ justifyContent: "center" }, [
528+ Text("Tab", { fgColor: "brightBlack", bold: true }),
529+ Text(" navigate ", { fgColor: "brightBlack" }),
530+ Text("Ctrl+Q", { fgColor: "brightBlack", bold: true }),
531+ Text(" quit", { fgColor: "brightBlack" }),
532+ ]),
533+ ]),
534+ ],
535+ );
536+}
537+
538+// ─── Main Screen ────────────────────────────────────────────────
539+
540+function mainView() {
541+ const art = currentArt();
542+ const color = artColor();
543+
544+ return VStack(
545+ {
546+ height: "100%",
547+ onKeyPress: (key) => {
548+ if (key === "ctrl+q" || key === "ctrl+c") quit();
549+ if (key === "f") feed();
550+ if (key === "p") petIt();
551+ if (key === "l") {
552+ showLog = !showLog;
553+ if (showLog) {
554+ logScroll = Math.max(0, log.length - 12);
555+ focused = null;
556+ }
557+ cel.render();
558+ }
559+ },
560+ },
561+ [
562+ // ── Header ──
563+ HStack({ padding: { x: 1 } }, [
564+ Text(` ${petName} `, { bold: true, fgColor: "black", bgColor: "cyan" }),
565+ Text(` the ${pet!.kind} `, { fgColor: "brightBlack" }),
566+ Spacer(),
567+ Text("[L]", { fgColor: "brightBlack", bold: true }),
568+ Text("og ", { fgColor: "brightBlack" }),
569+ Text("[Q]", { fgColor: "brightBlack", bold: true }),
570+ Text("uit ", { fgColor: "brightBlack" }),
571+ ]),
572+ Divider({ char: "═", fgColor: "brightBlack" }),
573+
574+ // ── Body ──
575+ HStack({ flex: 1 }, [
576+ // Sidebar
577+ VStack({ width: 20, padding: { x: 1, y: 1 }, gap: 1 }, [
578+ Text(" Stats", { bold: true, fgColor: "brightBlack" }),
579+ Divider({ char: "─", fgColor: "brightBlack" }),
580+ StatBar("Health", health, barColor(health)),
581+ StatBar("Food", 100 - hunger, barColor(hunger, true)),
582+ StatBar("Happy", happiness, barColor(happiness)),
583+ Spacer(),
584+ Divider({ char: "─", fgColor: "brightBlack" }),
585+ Text(" [F] Feed", { fgColor: "yellow", bold: true }),
586+ Text(" [P] Pet", { fgColor: "magenta", bold: true }),
587+ ]),
588+
589+ // Divider column
590+ VStack({ width: 1, height: "100%" }, [
591+ Text("│", { repeat: "fill", fgColor: "brightBlack" }),
592+ ]),
593+
594+ // Main pane
595+ VStack(
596+ { flex: 1, justifyContent: "center", alignItems: "center", gap: 1 },
597+ [
598+ VStack(
599+ { alignItems: "center" },
600+ art.map((ln) =>
601+ Text(ln, { fgColor: color, bold: anim === "excited" }),
602+ ),
603+ ),
604+ Text(""),
605+ HStack({ gap: 3 }, [
606+ HStack(
607+ {
608+ onClick: feed,
609+ focused: focused === "feed",
610+ onFocus: () => {
611+ focused = "feed";
612+ cel.render();
613+ },
614+ onBlur: () => {
615+ if (focused === "feed") focused = null;
616+ cel.render();
617+ },
618+ },
619+ [
620+ Text(focused === "feed" ? ">> Feed <<" : "[ Feed ]", {
621+ bold: true,
622+ fgColor: "yellow",
623+ }),
624+ ],
625+ ),
626+ HStack(
627+ {
628+ onClick: petIt,
629+ focused: focused === "pet",
630+ onFocus: () => {
631+ focused = "pet";
632+ cel.render();
633+ },
634+ onBlur: () => {
635+ if (focused === "pet") focused = null;
636+ cel.render();
637+ },
638+ },
639+ [
640+ Text(focused === "pet" ? ">> Pet <<" : "[ Pet ]", {
641+ bold: true,
642+ fgColor: "magenta",
643+ }),
644+ ],
645+ ),
646+ ]),
647+ ],
648+ ),
649+ ]),
650+ ],
651+ );
652+}
653+
654+// ─── Log Modal (Layer) ──────────────────────────────────────────
655+
656+function logView() {
657+ const bg = "black" as const;
658+ const closeLog = () => {
659+ showLog = false;
660+ cel.render();
661+ };
662+
663+ return VStack(
664+ {
665+ height: "100%",
666+ justifyContent: "center",
667+ alignItems: "center",
668+ onKeyPress: (key) => {
669+ if (key === "l" || key === "escape") closeLog();
670+ },
671+ },
672+ [
673+ // Top border
674+ HStack({}, [
675+ Text("╔", { fgColor: "brightBlack", bgColor: bg }),
676+ Text("═", { repeat: 58, fgColor: "brightBlack", bgColor: bg }),
677+ Text("╗", { fgColor: "brightBlack", bgColor: bg }),
678+ ]),
679+
680+ VStack({ width: 60, height: 14, padding: { x: 1 } }, [
681+ // Header row
682+ HStack({}, [
683+ Text(" Activity Log ", { bold: true, fgColor: "cyan", bgColor: bg }),
684+ VStack({ flex: 1 }, [Text(" ", { repeat: "fill", bgColor: bg })]),
685+ HStack({ onClick: closeLog, focusable: false }, [
686+ Text(" [x] ", { fgColor: "red", bold: true, bgColor: bg }),
687+ ]),
688+ ]),
689+ Text("─", { repeat: "fill", fgColor: "brightBlack", bgColor: bg }),
690+
691+ // Scrollable entries
692+ VStack(
693+ {
694+ flex: 1,
695+ overflow: "scroll",
696+ scrollbar: true,
697+ scrollOffset: logScroll,
698+ onScroll: (off) => {
699+ logScroll = off;
700+ cel.render();
701+ },
702+ },
703+ (() => {
704+ const blank = () =>
705+ HStack({}, [
706+ VStack({ flex: 1 }, [
707+ Text(" ", { repeat: "fill", bgColor: bg }),
708+ ]),
709+ ]);
710+ if (log.length === 0) {
711+ return [
712+ HStack({}, [
713+ Text(" No activity yet.", {
714+ fgColor: "brightBlack",
715+ italic: true,
716+ bgColor: bg,
717+ }),
718+ VStack({ flex: 1 }, [
719+ Text(" ", { repeat: "fill", bgColor: bg }),
720+ ]),
721+ ]),
722+ ...Array.from({ length: 9 }, blank),
723+ ];
724+ }
725+ const rows = log.map((entry) => {
726+ let fg:
727+ | "yellow"
728+ | "magenta"
729+ | "red"
730+ | "green"
731+ | "white"
732+ | "brightRed" = "white";
733+ if (entry.includes("born")) fg = "green";
734+ else if (entry.includes("fed") || entry.includes("Feed"))
735+ fg = "yellow";
736+ else if (entry.includes("pet ") || entry.includes("Happy"))
737+ fg = "magenta";
738+ else if (entry.includes("hungry") || entry.includes("lonely"))
739+ fg = "brightRed";
740+ else if (entry.includes("passed") || entry.includes("well"))
741+ fg = "red";
742+ return HStack({}, [
743+ Text(entry, { fgColor: fg, bgColor: bg }),
744+ VStack({ flex: 1 }, [
745+ Text(" ", { repeat: "fill", bgColor: bg }),
746+ ]),
747+ ]);
748+ });
749+ // Pad with blank rows so empty space has background
750+ const visibleRows = 10;
751+ const pad = Math.max(0, visibleRows - rows.length);
752+ return [...rows, ...Array.from({ length: pad }, blank)];
753+ })(),
754+ ),
755+
756+ Text("─", { repeat: "fill", fgColor: "brightBlack", bgColor: bg }),
757+ HStack({}, [
758+ Text(" Scroll: mouse wheel Close: [L] or Esc", {
759+ fgColor: "brightBlack",
760+ italic: true,
761+ bgColor: bg,
762+ }),
763+ VStack({ flex: 1 }, [Text(" ", { repeat: "fill", bgColor: bg })]),
764+ ]),
765+ ]),
766+
767+ // Bottom border
768+ HStack({}, [
769+ Text("╚", { fgColor: "brightBlack", bgColor: bg }),
770+ Text("═", { repeat: 58, fgColor: "brightBlack", bgColor: bg }),
771+ Text("╝", { fgColor: "brightBlack", bgColor: bg }),
772+ ]),
773+ ],
774+ );
775+}
776+
777+// ─── Dead Screen ────────────────────────────────────────────────
778+
779+function deadView() {
780+ const art = pet?.dead ?? [];
781+ return VStack(
782+ {
783+ height: "100%",
784+ justifyContent: "center",
785+ alignItems: "center",
786+ onKeyPress: (key) => {
787+ if (key === "ctrl+q" || key === "ctrl+c") quit();
788+ if (key === "l") {
789+ showLog = !showLog;
790+ if (showLog) focused = null;
791+ cel.render();
792+ }
793+ },
794+ },
795+ [
796+ Text("╔═══════════════════════╗", { fgColor: "brightBlack" }),
797+ Text("║ ║", { fgColor: "brightBlack" }),
798+ Text("║ R . I . P . ║", { fgColor: "red", bold: true }),
799+ Text("║ ║", { fgColor: "brightBlack" }),
800+ Text("╚═══════════════════════╝", { fgColor: "brightBlack" }),
801+ Text("", {}),
802+ ...art.map((ln) => Text(ln, { fgColor: "brightBlack" })),
803+ Text("", {}),
804+ Text(`${petName} has crossed the rainbow bridge.`, {
805+ fgColor: "brightBlack",
806+ italic: true,
807+ }),
808+ Text("", {}),
809+ HStack({ gap: 3 }, [
810+ HStack(
811+ {
812+ onClick: restart,
813+ focused: focused === "restart",
814+ onFocus: () => {
815+ focused = "restart";
816+ cel.render();
817+ },
818+ onBlur: () => {
819+ if (focused === "restart") focused = null;
820+ cel.render();
821+ },
822+ },
823+ [
824+ Text(focused === "restart" ? ">> New Pet <<" : "[ New Pet ]", {
825+ bold: true,
826+ fgColor: "green",
827+ }),
828+ ],
829+ ),
830+ HStack(
831+ {
832+ onClick: () => {
833+ showLog = !showLog;
834+ focused = null;
835+ cel.render();
836+ },
837+ focused: focused === "viewlog",
838+ onFocus: () => {
839+ focused = "viewlog";
840+ cel.render();
841+ },
842+ onBlur: () => {
843+ if (focused === "viewlog") focused = null;
844+ cel.render();
845+ },
846+ },
847+ [
848+ Text(focused === "viewlog" ? ">> View Log <<" : "[ View Log ]", {
849+ bold: true,
850+ fgColor: "cyan",
851+ }),
852+ ],
853+ ),
854+ ]),
855+ HStack({}, [
856+ Text("Ctrl+Q", { fgColor: "brightBlack", bold: true }),
857+ Text(" quit", { fgColor: "brightBlack" }),
858+ ]),
859+ ],
860+ );
861+}
862+
863+// ─── Viewport ───────────────────────────────────────────────────
864+
865+cel.init(new ProcessTerminal());
866+
867+cel.viewport(() => {
868+ let base;
869+ if (screen === "create") base = createView();
870+ else if (screen === "main") base = mainView();
871+ else base = deadView();
872+
873+ if (showLog) return [base, logView()];
874+ return base;
875+});