xonecas.io
70160
Ce

cel-tui / files

cel-tui / examples / hello.ts

3.3 KB
1/**
2 * cel-tui: Hello World
3 *
4 * A stylish first impression — centered ASCII art logo with a
5 * reactive clock and keyboard hints. Press any key to cycle
6 * colors, Ctrl+Q to quit.
7 *
8 * Run: bun run examples/hello.ts
9 */
10
11import { Spacer, Spinner } from "@cel-tui/components";
12import { cel, HStack, ProcessTerminal, Text, VStack } from "@cel-tui/core";
13import { warningBox } from "./warning-box";
14
15const logo = [
16  "╭─────────────────────╮",
17  "│  ██████╗███████╗██╗ │",
18  "│ ██╔════╝██╔════╝██║ │",
19  "│ ██║     █████╗  ██║ │",
20  "│ ██║     ██╔══╝  ██║ │",
21  "│ ╚██████╗███████╗██║ │",
22  "│  ╚═════╝╚══════╝╚═╝ │",
23  "╰─────────────────────╯",
24];
25
26const palette: Array<
27  "color06" | "color05" | "color03" | "color02" | "color04" | "color01"
28> = ["color06", "color05", "color03", "color02", "color04", "color01"];
29
30let colorIndex = 0;
31
32const MIN_COLS = 38;
33const MIN_ROWS = 14;
34
35const spinner = Spinner({ maxFps: 12.5 });
36
37function clock(): string {
38  const now = new Date();
39  return now.toLocaleTimeString("en-US", { hour12: false });
40}
41
42function quit() {
43  spinner.dispose();
44  cel.stop();
45  process.exit();
46}
47
48cel.init(new ProcessTerminal());
49spinner.start();
50cel.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        ...warningBox([
66          "  Terminal too small :(",
67          "",
68          "  Please resize to at",
69          `  least ${String(MIN_COLS).padStart(3)}×${String(MIN_ROWS).padStart(2)} chars.`,
70          "",
71          "  Ctrl+Q to quit",
72        ]),
73      ],
74    );
75  }
76
77  const color = palette[colorIndex % palette.length]!;
78
79  return VStack(
80    {
81      height: "100%",
82      alignItems: "center",
83      overflow: "scroll",
84      onKeyPress: (key) => {
85        if (key === "ctrl+q" || key === "ctrl+c") quit();
86        colorIndex++;
87        cel.render();
88      },
89    },
90    [
91      Spacer(),
92      // Logo
93      ...logo.map((line) => Text(line, { fgColor: color, bold: true })),
94
95      // Tagline
96      Text(""),
97      Text("terminal UIs, cell by cell", {
98        italic: true,
99        fgColor: "color08",
100      }),
101
102      // Divider
103      Text(""),
104      HStack({}, [
105        Text("  "),
106        Text("─", { repeat: 34, fgColor: color }),
107        Text("  "),
108      ]),
109      Text(""),
110
111      // Live info row
112      HStack({ gap: 2 }, [
113        spinner({ fgColor: color, bold: true }),
114        Text(clock()),
115        Text("│", { fgColor: "color08" }),
116        Text("press any key to change color", { fgColor: "color08" }),
117      ]),
118
119      // Quit hint
120      Text(""),
121      HStack({ gap: 1 }, [
122        Text(" ctrl+q ", { bgColor: color, fgColor: "color00", bold: true }),
123        Text("quit", { fgColor: "color08" }),
124      ]),
125      Spacer(),
126    ],
127  );
128});