xonecas.io
70160
Ce

cel-tui / commit dfe0a70

fix: make examples responsive and add minimum terminal size guard

- pet.ts: compact create screen (gap:0, conditional art preview),
  overflow:scroll + Spacer centering on create/dead screens, narrow
  mode (<50 cols) stacks stats above pet, adaptive log modal sizing,
  tooSmallView at <40×15
- hello.ts: overflow:scroll + Spacer centering, tooSmallView at <38×14
- simple-scroll.ts: tooSmallView at <30×8

xonecas2026-04-04 dfe0a70b49527fe96b2742b5dfbfa37543e83df5 parent 587ac55

3 files changed

examples/hello.ts+42−5
@@ -9,6 +9,7 @@
99 */
1010
1111 import { cel, VStack, HStack, Text, ProcessTerminal } from "@cel-tui/core";
12+import { Spacer } from "@cel-tui/components";
1213
1314 const logo = [
1415 "╭─────────────────────╮",
@@ -27,6 +28,9 @@ const palette: Array<"cyan" | "magenta" | "yellow" | "green" | "blue" | "red"> =
2728 let colorIndex = 0;
2829 let tick = 0;
2930
31+const MIN_COLS = 38;
32+const MIN_ROWS = 14;
33+
3034 const spinnerFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
3135
3236 function clock(): string {
@@ -40,26 +44,58 @@ setInterval(() => {
4044 cel.render();
4145 }, 80);
4246
47+function quit() {
48+ cel.stop();
49+ process.exit();
50+}
51+
4352 cel.init(new ProcessTerminal());
4453 cel.viewport(() => {
54+ const cols = process.stdout.columns || 80;
55+ const rows = process.stdout.rows || 24;
56+
57+ if (cols < MIN_COLS || rows < MIN_ROWS) {
58+ return VStack(
59+ {
60+ height: "100%",
61+ justifyContent: "center",
62+ alignItems: "center",
63+ onKeyPress: (key) => {
64+ if (key === "ctrl+q" || key === "ctrl+c") quit();
65+ },
66+ },
67+ [
68+ Text("┌─────────────────────────┐", { fgColor: "yellow" }),
69+ Text("│ Terminal too small :( │", { fgColor: "yellow" }),
70+ Text("│ │", { fgColor: "yellow" }),
71+ Text("│ Please resize to at │", { fgColor: "yellow" }),
72+ Text(
73+ `│ least ${String(MIN_COLS).padStart(3)}×${String(MIN_ROWS).padStart(2)} chars. │`,
74+ { fgColor: "yellow" },
75+ ),
76+ Text("│ │", { fgColor: "yellow" }),
77+ Text("│ Ctrl+Q to quit │", { fgColor: "yellow" }),
78+ Text("└─────────────────────────┘", { fgColor: "yellow" }),
79+ ],
80+ );
81+ }
82+
4583 const color = palette[colorIndex % palette.length]!;
4684 const spinner = spinnerFrames[tick % spinnerFrames.length]!;
4785
4886 return VStack(
4987 {
5088 height: "100%",
51- justifyContent: "center",
5289 alignItems: "center",
90+ overflow: "scroll",
5391 onKeyPress: (key) => {
54- if (key === "ctrl+q" || key === "ctrl+c") {
55- cel.stop();
56- process.exit();
57- }
92+ if (key === "ctrl+q" || key === "ctrl+c") quit();
5893 colorIndex++;
5994 cel.render();
6095 },
6196 },
6297 [
98+ Spacer(),
6399 // Logo
64100 ...logo.map((line) => Text(line, { fgColor: color, bold: true })),
65101
@@ -93,6 +129,7 @@ cel.viewport(() => {
93129 Text(" ctrl+q ", { bgColor: color, fgColor: "black", bold: true }),
94130 Text("quit", { fgColor: "brightBlack" }),
95131 ]),
132+ Spacer(),
96133 ],
97134 );
98135 });
examples/pet.ts+195−123
@@ -327,16 +327,47 @@ function quit() {
327327 process.exit(0);
328328 }
329329
330+// ─── Terminal Size ──────────────────────────────────────────────
331+
332+const MIN_COLS = 40;
333+const MIN_ROWS = 15;
334+
335+function termSize(): { cols: number; rows: number } {
336+ return {
337+ cols: process.stdout.columns || 80,
338+ rows: process.stdout.rows || 24,
339+ };
340+}
341+
342+function tooSmallView() {
343+ return VStack(
344+ {
345+ height: "100%",
346+ justifyContent: "center",
347+ alignItems: "center",
348+ onKeyPress: (key) => {
349+ if (key === "ctrl+q" || key === "ctrl+c") quit();
350+ },
351+ },
352+ [
353+ Text("┌─────────────────────────┐", { fgColor: "yellow" }),
354+ Text("│ Terminal too small :( │", { fgColor: "yellow" }),
355+ Text("│ │", { fgColor: "yellow" }),
356+ Text("│ Please resize to at │", { fgColor: "yellow" }),
357+ Text(
358+ `│ least ${String(MIN_COLS).padStart(3)}×${String(MIN_ROWS).padStart(2)} chars. │`,
359+ { fgColor: "yellow" },
360+ ),
361+ Text("│ │", { fgColor: "yellow" }),
362+ Text("│ Ctrl+Q to quit │", { fgColor: "yellow" }),
363+ Text("└─────────────────────────┘", { fgColor: "yellow" }),
364+ ],
365+ );
366+}
367+
330368 // ─── Art Helpers ────────────────────────────────────────────────
331369
332370 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
340371 const frames = [
341372 ...def.idle.happy,
342373 ...def.idle.hungry,
@@ -397,14 +428,17 @@ function barColor(val: number, invert = false): "green" | "yellow" | "red" {
397428 // ─── Create Screen ──────────────────────────────────────────────
398429
399430 function createView() {
431+ const { rows } = termSize();
400432 const preview = PETS[selectedKind]!;
401433 const previewArt = preview.idle.happy[frame % preview.idle.happy.length]!;
434+ // Compact mode: drop art preview when height is tight
435+ const showPreview = rows >= 22;
402436
403437 return VStack(
404438 {
405439 height: "100%",
406- justifyContent: "center",
407440 alignItems: "center",
441+ overflow: "scroll",
408442 onKeyPress: (key) => {
409443 if (key === "ctrl+q" || key === "ctrl+c") quit();
410444 // Arrow keys to pick species (only when name input is NOT focused)
@@ -420,7 +454,8 @@ function createView() {
420454 },
421455 },
422456 [
423- VStack({ width: 40, padding: { x: 2, y: 1 }, gap: 1 }, [
457+ Spacer(),
458+ VStack({ width: 40, padding: { x: 2 }, gap: 0 }, [
424459 // Title
425460 HStack({ justifyContent: "center" }, [
426461 Text("~ New Pet ~", { bold: true, fgColor: "cyan" }),
@@ -459,43 +494,45 @@ function createView() {
459494 }),
460495
461496 // Species picker
497+ Text(""),
462498 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,
499+ ...PETS.map((p, i) => {
500+ const sel = i === selectedKind;
501+ return HStack(
502+ {
503+ onClick: () => {
504+ selectedKind = i;
505+ cel.render();
473506 },
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", {
507+ focusable: false,
508+ },
509+ [
510+ Text(sel ? " > " : " ", {
511+ fgColor: sel ? "cyan" : "brightBlack",
512+ bold: sel,
513+ }),
514+ Text(p.kind, {
515+ fgColor: sel ? "cyan" : "white",
516+ bold: sel,
517+ }),
518+ ],
519+ );
520+ }),
521+ Text(" ↑/↓ to change", {
488522 fgColor: "brightBlack",
489523 italic: true,
490524 }),
491525
492- Divider({ fgColor: "brightBlack" }),
493-
494- // Preview
495- VStack(
496- { alignItems: "center" },
497- previewArt.map((ln) => Text(ln, { fgColor: "yellow" })),
498- ),
526+ // Preview (hidden when terminal is short)
527+ ...(showPreview
528+ ? [
529+ Divider({ fgColor: "brightBlack" }),
530+ VStack(
531+ { alignItems: "center" },
532+ previewArt.map((ln) => Text(ln, { fgColor: "yellow" })),
533+ ),
534+ ]
535+ : []),
499536
500537 Divider({ fgColor: "brightBlack" }),
501538
@@ -531,6 +568,7 @@ function createView() {
531568 Text(" quit", { fgColor: "brightBlack" }),
532569 ]),
533570 ]),
571+ Spacer(),
534572 ],
535573 );
536574 }
@@ -538,8 +576,89 @@ function createView() {
538576 // ─── Main Screen ────────────────────────────────────────────────
539577
540578 function mainView() {
579+ const { cols } = termSize();
541580 const art = currentArt();
542581 const color = artColor();
582+ // Narrow mode: stack sidebar above pet pane instead of side-by-side
583+ const narrow = cols < 50;
584+
585+ const statsPanel = VStack(
586+ { ...(narrow ? {} : { width: 20 }), padding: { x: 1 }, gap: 0 },
587+ [
588+ ...(narrow
589+ ? [
590+ HStack({ gap: 2 }, [
591+ StatBar("Health", health, barColor(health)),
592+ StatBar("Food", 100 - hunger, barColor(hunger, true)),
593+ StatBar("Happy", happiness, barColor(happiness)),
594+ ]),
595+ ]
596+ : [
597+ Text(" Stats", { bold: true, fgColor: "brightBlack" }),
598+ Divider({ char: "─", fgColor: "brightBlack" }),
599+ StatBar("Health", health, barColor(health)),
600+ StatBar("Food", 100 - hunger, barColor(hunger, true)),
601+ StatBar("Happy", happiness, barColor(happiness)),
602+ Spacer(),
603+ Divider({ char: "─", fgColor: "brightBlack" }),
604+ Text(" [F] Feed", { fgColor: "yellow", bold: true }),
605+ Text(" [P] Pet", { fgColor: "magenta", bold: true }),
606+ ]),
607+ ],
608+ );
609+
610+ const petPane = VStack(
611+ { flex: 1, justifyContent: "center", alignItems: "center", gap: 1 },
612+ [
613+ VStack(
614+ { alignItems: "center" },
615+ art.map((ln) => Text(ln, { fgColor: color, bold: anim === "excited" })),
616+ ),
617+ Text(""),
618+ HStack({ gap: 3 }, [
619+ HStack(
620+ {
621+ onClick: feed,
622+ focused: focused === "feed",
623+ onFocus: () => {
624+ focused = "feed";
625+ cel.render();
626+ },
627+ onBlur: () => {
628+ if (focused === "feed") focused = null;
629+ cel.render();
630+ },
631+ },
632+ [
633+ Text(focused === "feed" ? ">> Feed <<" : "[ Feed ]", {
634+ bold: true,
635+ fgColor: "yellow",
636+ }),
637+ ],
638+ ),
639+ HStack(
640+ {
641+ onClick: petIt,
642+ focused: focused === "pet",
643+ onFocus: () => {
644+ focused = "pet";
645+ cel.render();
646+ },
647+ onBlur: () => {
648+ if (focused === "pet") focused = null;
649+ cel.render();
650+ },
651+ },
652+ [
653+ Text(focused === "pet" ? ">> Pet <<" : "[ Pet ]", {
654+ bold: true,
655+ fgColor: "magenta",
656+ }),
657+ ],
658+ ),
659+ ]),
660+ ],
661+ );
543662
544663 return VStack(
545664 {
@@ -572,81 +691,23 @@ function mainView() {
572691 Divider({ char: "═", fgColor: "brightBlack" }),
573692
574693 // ── 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- ),
694+ ...(narrow
695+ ? [
696+ // Narrow: stats bar on top, pet below
697+ statsPanel,
698+ Divider({ char: "─", fgColor: "brightBlack" }),
699+ petPane,
700+ ]
701+ : [
702+ // Wide: sidebar | pet side-by-side
703+ HStack({ flex: 1 }, [
704+ statsPanel,
705+ VStack({ width: 1, height: "100%" }, [
706+ Text("│", { repeat: "fill", fgColor: "brightBlack" }),
707+ ]),
708+ petPane,
646709 ]),
647- ],
648- ),
649- ]),
710+ ]),
650711 ],
651712 );
652713 }
@@ -654,12 +715,18 @@ function mainView() {
654715 // ─── Log Modal (Layer) ──────────────────────────────────────────
655716
656717 function logView() {
718+ const { cols, rows } = termSize();
657719 const bg = "black" as const;
658720 const closeLog = () => {
659721 showLog = false;
660722 cel.render();
661723 };
662724
725+ // Adapt modal size to terminal
726+ const modalW = Math.min(60, cols - 4);
727+ const modalH = Math.min(14, rows - 4);
728+ const borderW = modalW - 2; // inner repeat count for ═ chars
729+
663730 return VStack(
664731 {
665732 height: "100%",
@@ -673,11 +740,11 @@ function logView() {
673740 // Top border
674741 HStack({}, [
675742 Text("╔", { fgColor: "brightBlack", bgColor: bg }),
676- Text("═", { repeat: 58, fgColor: "brightBlack", bgColor: bg }),
743+ Text("═", { repeat: borderW, fgColor: "brightBlack", bgColor: bg }),
677744 Text("╗", { fgColor: "brightBlack", bgColor: bg }),
678745 ]),
679746
680- VStack({ width: 60, height: 14, padding: { x: 1 } }, [
747+ VStack({ width: modalW, height: modalH, padding: { x: 1 } }, [
681748 // Header row
682749 HStack({}, [
683750 Text(" Activity Log ", { bold: true, fgColor: "cyan", bgColor: bg }),
@@ -719,7 +786,7 @@ function logView() {
719786 Text(" ", { repeat: "fill", bgColor: bg }),
720787 ]),
721788 ]),
722- ...Array.from({ length: 9 }, blank),
789+ ...Array.from({ length: Math.max(0, modalH - 5) }, blank),
723790 ];
724791 }
725792 const rows = log.map((entry) => {
@@ -747,7 +814,7 @@ function logView() {
747814 ]);
748815 });
749816 // Pad with blank rows so empty space has background
750- const visibleRows = 10;
817+ const visibleRows = Math.max(0, modalH - 4);
751818 const pad = Math.max(0, visibleRows - rows.length);
752819 return [...rows, ...Array.from({ length: pad }, blank)];
753820 })(),
@@ -767,7 +834,7 @@ function logView() {
767834 // Bottom border
768835 HStack({}, [
769836 Text("╚", { fgColor: "brightBlack", bgColor: bg }),
770- Text("═", { repeat: 58, fgColor: "brightBlack", bgColor: bg }),
837+ Text("═", { repeat: borderW, fgColor: "brightBlack", bgColor: bg }),
771838 Text("╝", { fgColor: "brightBlack", bgColor: bg }),
772839 ]),
773840 ],
@@ -781,8 +848,8 @@ function deadView() {
781848 return VStack(
782849 {
783850 height: "100%",
784- justifyContent: "center",
785851 alignItems: "center",
852+ overflow: "scroll",
786853 onKeyPress: (key) => {
787854 if (key === "ctrl+q" || key === "ctrl+c") quit();
788855 if (key === "l") {
@@ -793,19 +860,20 @@ function deadView() {
793860 },
794861 },
795862 [
863+ Spacer(),
796864 Text("╔═══════════════════════╗", { fgColor: "brightBlack" }),
797865 Text("║ ║", { fgColor: "brightBlack" }),
798866 Text("║ R . I . P . ║", { fgColor: "red", bold: true }),
799867 Text("║ ║", { fgColor: "brightBlack" }),
800868 Text("╚═══════════════════════╝", { fgColor: "brightBlack" }),
801- Text("", {}),
869+ Text(""),
802870 ...art.map((ln) => Text(ln, { fgColor: "brightBlack" })),
803- Text("", {}),
871+ Text(""),
804872 Text(`${petName} has crossed the rainbow bridge.`, {
805873 fgColor: "brightBlack",
806874 italic: true,
807875 }),
808- Text("", {}),
876+ Text(""),
809877 HStack({ gap: 3 }, [
810878 HStack(
811879 {
@@ -856,6 +924,7 @@ function deadView() {
856924 Text("Ctrl+Q", { fgColor: "brightBlack", bold: true }),
857925 Text(" quit", { fgColor: "brightBlack" }),
858926 ]),
927+ Spacer(),
859928 ],
860929 );
861930 }
@@ -865,6 +934,9 @@ function deadView() {
865934 cel.init(new ProcessTerminal());
866935
867936 cel.viewport(() => {
937+ const { cols, rows } = termSize();
938+ if (cols < MIN_COLS || rows < MIN_ROWS) return tooSmallView();
939+
868940 let base;
869941 if (screen === "create") base = createView();
870942 else if (screen === "main") base = mainView();
examples/simple-scroll.ts+42−8
@@ -6,6 +6,9 @@
66 */
77 import { cel, VStack, HStack, Text, ProcessTerminal } from "@cel-tui/core";
88
9+const MIN_COLS = 30;
10+const MIN_ROWS = 8;
11+
912 const items = Array.from({ length: 50 }, (_, i) => `Item ${i + 1} — ${fake()}`);
1013
1114 function fake(): string {
@@ -38,16 +41,47 @@ function fake(): string {
3841
3942 let scrollOffset = 0;
4043
44+function quit() {
45+ cel.stop();
46+ process.exit();
47+}
48+
4149 cel.init(new ProcessTerminal());
42-cel.viewport(() =>
43- VStack(
50+cel.viewport(() => {
51+ const cols = process.stdout.columns || 80;
52+ const rows = process.stdout.rows || 24;
53+
54+ if (cols < MIN_COLS || rows < MIN_ROWS) {
55+ return VStack(
56+ {
57+ height: "100%",
58+ justifyContent: "center",
59+ alignItems: "center",
60+ onKeyPress: (key) => {
61+ if (key === "ctrl+q" || key === "ctrl+c") quit();
62+ },
63+ },
64+ [
65+ Text("┌─────────────────────────┐", { fgColor: "yellow" }),
66+ Text("│ Terminal too small :( │", { fgColor: "yellow" }),
67+ Text("│ │", { fgColor: "yellow" }),
68+ Text("│ Please resize to at │", { fgColor: "yellow" }),
69+ Text(
70+ `│ least ${String(MIN_COLS).padStart(3)}×${String(MIN_ROWS).padStart(2)} chars. │`,
71+ { fgColor: "yellow" },
72+ ),
73+ Text("│ │", { fgColor: "yellow" }),
74+ Text("│ Ctrl+Q to quit │", { fgColor: "yellow" }),
75+ Text("└─────────────────────────┘", { fgColor: "yellow" }),
76+ ],
77+ );
78+ }
79+
80+ return VStack(
4481 {
4582 height: "100%",
4683 onKeyPress: (key) => {
47- if (key === "ctrl+q" || key === "ctrl+c") {
48- cel.stop();
49- process.exit();
50- }
84+ if (key === "ctrl+q" || key === "ctrl+c") quit();
5185 },
5286 },
5387 [
@@ -82,5 +116,5 @@ cel.viewport(() =>
82116 Text("Mouse wheel to scroll · Ctrl+Q quit", { fgColor: "brightBlack" }),
83117 ]),
84118 ],
85- ),
86-);
119+ );
120+});