Skip to content

Commit a6a3e61

Browse files
committed
✨ example: add interactive hello world
1 parent 3c55adf commit a6a3e61

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

‎examples/hello-world/package.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@examples/hello-world",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"start": "tsx src/index.ts"
7+
},
8+
"dependencies": {
9+
"@bomb.sh/tty": "^0.7.0",
10+
"@clack/ui": "workspace:*"
11+
},
12+
"devDependencies": {
13+
"@types/node": "^22.20.0",
14+
"tsx": "^4.19.0"
15+
}
16+
}

‎examples/hello-world/src/index.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { stdin, stdout } from 'node:process';
2+
import { fixed, grow, percent } from '@bomb.sh/tty';
3+
import { type BoxProps, createUI, type HostElement, rgba, type TextProps } from '@clack/ui';
4+
5+
const blue = rgba(0, 0, 238);
6+
const cyan = rgba(0, 205, 205);
7+
const gray = rgba(127, 127, 127);
8+
9+
await using ui = await createUI({ input: stdin, output: stdout });
10+
const { host } = ui;
11+
12+
const sayInput = host.createElement('input');
13+
const toInput = host.createElement('input');
14+
15+
const say = host.createLiteral('Hello');
16+
const comma = host.createLiteral(', ');
17+
const to = host.createLiteral('World');
18+
const bang = host.createLiteral('!');
19+
20+
const output = host.createElement('text');
21+
22+
host.setProperty(output, 'color', cyan);
23+
host.insertBefore(output, say);
24+
host.insertBefore(output, comma);
25+
host.insertBefore(output, to);
26+
host.insertBefore(output, bang);
27+
28+
host.addEventListener(sayInput, 'input', (event) => {
29+
host.setText(say, event.value);
30+
});
31+
host.addEventListener(toInput, 'input', (event) => {
32+
host.setText(to, event.value);
33+
});
34+
35+
const app = box(
36+
{
37+
layout: {
38+
direction: 'ttb',
39+
gap: 1,
40+
padding: { top: 1, bottom: 1, left: 2, right: 2 },
41+
width: fixed(40),
42+
},
43+
border: { color: blue, top: 1, right: 1, bottom: 1, left: 1 },
44+
},
45+
output,
46+
box(
47+
{ layout: { direction: 'ttb', width: grow() } },
48+
box({ layout: { direction: 'ltr', gap: 1, width: grow() } }, label('say:'), label('to:')),
49+
box({ layout: { direction: 'ltr', gap: 1, width: grow() } }, sayInput, toInput),
50+
),
51+
);
52+
53+
host.insertBefore(host.element, app);
54+
await ui.main();
55+
56+
function box(properties: Omit<BoxProps, 'key'>, ...children: HostElement[]): HostElement {
57+
const element = host.createElement('box');
58+
setProperties(element, properties);
59+
for (const child of children) {
60+
host.insertBefore(element, child);
61+
}
62+
return element;
63+
}
64+
65+
function label(content: string): HostElement {
66+
return box({ layout: { width: percent(0.3) } }, text({ color: gray }, content));
67+
}
68+
69+
function text(properties: TextProps, content: string): HostElement {
70+
const element = host.createElement('text');
71+
setProperties(element, properties);
72+
host.insertBefore(element, host.createLiteral(content));
73+
return element;
74+
}
75+
76+
function setProperties(element: HostElement, properties: object): void {
77+
for (const [name, value] of Object.entries(properties)) {
78+
host.setProperty(element, name, value);
79+
}
80+
}

‎examples/hello-world/tsconfig.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "@bomb.sh/tools/tsconfig.json",
3+
"compilerOptions": {
4+
"composite": false,
5+
"declaration": false,
6+
"declarationMap": false,
7+
"noEmit": true,
8+
"types": ["node"]
9+
}
10+
}

‎pnpm-lock.yaml‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)