|
| 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 | +} |
0 commit comments