How to build native TypeScript terminal apps with tui Vercel Labs released tui 0.2.1, a zero-dependency TypeScript library for building terminal applications on Node.js 24+ that compiles to standalone native binaries via scriptc 0.5.0, according to a tutorial by Flavio. The library uses an immediate-mode rendering model that diffs a typed-array cell grid and emits only changed bytes — 58 bytes on a diffed frame versus 2,520 bytes on the first — and exposes a running terminal app as an MCP server for AI agents by adding one line to runApp. The author measured startup time and memory of the roughly 2 MB compiled binary against Node.js 26 on an Apple M4 Pro MacBook Pro. How to build native TypeScript terminal apps with tui By Flavio https://flaviocopes.com/about/ Tagged: TypeScript https://flaviocopes.com/tags/typescript/ , CLI https://flaviocopes.com/tags/cli/ , AI https://flaviocopes.com/tags/ai/ Build fast terminal interfaces in TypeScript with Vercel Labs' tui, test them without a terminal, wire up one-line MCP control, and compile to native binaries with scriptc. Chris Tate at Vercel Labs just released tui https://github.com/vercel-labs/tui , a zero-dependency TypeScript library to build terminal applications that run on Node.js 24+ and compile to standalone native binaries with scriptc https://scriptc.dev . What caught my attention right away in the launch post https://x.com/ctatedev/status/2129033440440017207 was the combination of 3 things that usually live in separate worlds. You write plain TypeScript. You compile it to a ~2 MB native executable that starts in a few milliseconds. Adding one line to runApp exposes your running terminal app as an MCP server for AI agents. I installed tui@0.2.1 and scriptc@0.5.0 on my MacBook Pro Apple M4 Pro and built a real multi-panel app from scratch, ran its headless test suite, drove the compiled binary over MCP, and measured startup time and memory against Node.js 26. In this tutorial we’ll go through everything tui gives you, from a 20-line picker to a full service and log inspector with 100,000 searchable lines, custom primitives, spring animations, inline prompts, headless tests, MCP control, and native compilation. What makes tui different Most JavaScript terminal libraries, like Ink, recreate React’s virtual DOM and Yoga flexbox layout inside the terminal. You keep React on the hook, pay for Node’s startup cost every time a user invokes your CLI, and ship a runtime that weighs tens of megabytes. tui goes the opposite route. It uses an immediate-mode rendering model with zero runtime dependencies. Instead of mutating a component tree, you write a single view frame function that draws the whole screen from your current state. tui writes your drawing calls into a flat typed-array grid of cells, diffs that grid against the previous frame, and emits only the changed bytes inside terminal synchronized-output markers so frames never tear. Step through the figure below to see what happens on screen when you move a list selection by one row 2,520 bytes on the first frame vs 58 bytes on the diffed frame : Without an fps option, tui does zero background work when idle. It only redraws when a key arrives, the window resizes, a spring asks for the next frame, or your code calls app.redraw . When you are ready to ship, scriptc compiles that exact TypeScript file into a native binary, with no Node.js or V8 bundled inside. Installation and setup tui requires Node.js 24 or later . Node 24+ runs .ts files directly, without a build step. If you’re brushing up on TypeScript syntax or types before diving in, keep the TypeScript Handbook https://flaviocopes.com/ebooks/typescript-handbook/ handy. You can get a free copy in PDF or EPUB https://flaviocopes.com/access/ . Create a project folder and add "type": "module" to package.json so both Node and scriptc treat .ts files as ES modules: { "name": "tui-lab", "private": true, "type": "module" } Then install tui and add scriptc as a dev dependency: npm install tui npm install -D scriptc I hit a real problem here on npm 11, and you might too if you have ignore-scripts=true in your ~/.npmrc or if you rely on the new npm 11 script approval prompts. scriptc uses a postinstall script node scripts/install-native.mjs to install its native compiler binary, so running npx scriptc --version right after install, I got scriptc: native installation is incomplete . Approve scriptc ’s install script and rebuild it once: npm install-scripts approve scriptc npm rebuild scriptc --ignore-scripts=false npx scriptc --version You also need clang and a linker on your machine to build native executables. On macOS, Xcode Command Line Tools already provide clang . On Debian or Ubuntu, you can install clang , lld , binutils and libc6-dev . tui exposes 3 separate entry points so you only pay for what you import: | Import path | What it includes | |---|---| | tui | runApp , createTestApp , layout helpers, built-in components, definePrimitive , drawing, Markdown, motion, search, and game helpers | | tui/prompt | Inline shell prompts text , select , multiselect , confirm , spinner , progress | | tui/agent | agentServer , createAgent , and agentTools for MCP control | Your first tui app Let’s start with a small, complete interactive program in hello.ts . It splits the screen into a header, a list of deploy targets, and a status bar at the bottom, and lets you pick an environment using the arrow keys and Enter: js import { Listbox, matchesShortcut, runApp, split, text } from 'tui' const environments = { value: 'local', label: 'Local localhost:4321 ' }, { value: 'staging', label: 'Staging staging.example.internal ' }, { value: 'production', label: 'Production flaviocopes.com ' }, let selected = 'none yet' const app = runApp { view: frame = { const header, list, footer = split frame.rect, 'rows', 1, -1, 1 text frame, header.x, header.y, 'Deploy Target', frame.theme.heading Listbox.Root frame, { id: 'envs', label: 'Environment', items: environments, onValueChange: value = { selected = value }, }, root = Listbox.Content root, list , text frame, footer.x, footer.y, Selected: ${selected} · Enter to pick · q to quit , frame.theme.muted, }, keys: key = { if key.name === 'char' && key.text === 'q' { app.exit 0 return true } if matchesShortcut key, 'ctrl+c' { app.exit 0 return true } return false }, } You can run it right away on Node 24+, or compile it to a standalone executable: node hello.ts npx scriptc build hello.ts --strip -o hello-strip ./hello-strip On my machine, hello.ts compiled to a 1.83 MB stripped native executable that emits the same 338-byte initial ANSI frame as node hello.ts . Look at the keys callback too. runApp always delivers keys to the focused component first so Up, Down, PageUp, PageDown, Home, End, typeahead, and Enter go straight to Listbox . Only keys the focused component does not consume fall through to your top-level keys handler. Layout math: rects instead of CSS There is no Flexbox or CSS engine in tui . Every component and drawing helper takes a plain Rect object: { x, y, columns, rows } , starting from frame.rect the full terminal size . Four tiny helpers will handle almost every terminal layout you will ever write: - split rect, direction, sizes, gap? divides a rect into 'rows' or 'columns' . Positive numbers are fixed cell counts. Negative numbers are proportional shares of whatever space remains. For example, 1, -1, 1 gives you a 1-row header, a flexible body, and a 1-row footer. 24, -1 with a gap of 1 gives you a 24-column sidebar, a 1-column gutter, and the rest of the screen. - inset rect, padding shrinks a rect by n cells on all sides, or by vertical, horizontal . - center rect, columns, rows returns a fixed-size rect centered inside another rect, which is how you position a modal Dialog . - drawBox frame.cells, rect, { title, border } draws a border 'rounded' , 'single' , 'double' , or 'heavy' and returns the inner rect ready to pass to split or a component. Because the layout is recomputed inside view frame on every frame, resizing your terminal window, or animating a panel width with a spring, works out of the box, with no additional layout listeners needed. The component model and state contract tui ships 8 components out of the box, and every one of them follows the same Root + parts pattern. You pass id to keep a component’s internal state stable across frames, and label so AI agents know what to call it. | Component | Parts | Keyboard & mouse behavior | |---|---|---| | Listbox | Root , Content , Item | Arrows, Home/End, Page keys, Ctrl+P/N, typeahead, click; Enter or Space picks | | CheckboxGroup | Root , Content | Arrows move, Space toggles check, Enter fires onSubmit | | Tabs | Root , List | Left/Right and Home/End switch tabs, click opens tab | | Table | Root , Content | Header row + aligned columns left or right , arrows and click pick rows | | Input | Root , Field | Single-line shell-style editing Ctrl+A/E/U/K/W, word jumps , bracketed paste, cursor positioning | | Textarea | Root , Field | Wrapped multi-line editing, root.api.rows columns for auto-grow | | Dialog | Root , Content | Centered overlay that traps Tab focus and keys while open; Escape closes it | | ScrollArea | Root , Viewport | Scrollable viewport with scrollbar, total virtualization, and stickToEnd for logs | Every stateful prop follows a single controlled/uncontrolled contract: - Value: value , defaultValue , onValueChange value, details - Highlight: highlighted , defaultHighlighted , onHighlight value, details - Open state Dialog : open , defaultOpen , onOpenChange open, details - Scroll offset ScrollArea : offset , defaultOffset , onOffsetChange offset, details Every change callback gets a details object as its second argument: - details.source tells you who triggered the change: 'keyboard' , 'mouse' , 'paste' , 'programmatic' , or 'agent' . - details.reason tells you why: 'item-press' , 'list-navigation' , 'input-change' , 'escape-key' , and so on. - details.cancel rejects the change before it commits. One gotcha is worth knowing early: focus follows draw order . The first focusable part drawn in view frame will get keyboard focus on startup, and Tab / Shift+Tab will cycle through parts in the order you draw them. ScrollArea.Viewport is focusable, so always draw your search Input or chat Textarea before a ScrollArea , otherwise typing will scroll the viewport instead of entering text. Building a real multi-panel app: a service and log inspector Toy counters are nice, but they don’t show how layout, focus, virtualization, dialogs, and custom components all work together in a real tool. Let’s build a complete Service & Log Inspector ops that puts together all the major pieces of tui in one shared module ops-view.ts and a thin entry point ops.ts . Here’s what ops does: - A left sidebar with a live Input filter, a Listbox of services, and a custom Toggle primitive at the bottom. - A right workspace with Tabs switching between Overview drawMeter CPU bar, drawSparkline latency chart, a secret API Token field, and a Table of recent deploys , Logs virtualized ScrollArea filtering 100,000 log lines incrementally with createFilter and drawMatches , and Runbook live rendered Markdown via createMarkdown and drawLines . - A spring-animated right drawer d key holding a CheckboxGroup for log levels. - A focus-trapped confirmation Dialog r key to restart the selected service. - Full mouse support mouse: true and one-line MCP exposure agentServer . Let’s start with the custom Toggle primitive in ops-view.ts . Using definePrimitive and controllable , your own components inherit focus management, keyboard handling, controlled/uncontrolled props, and MCP agent actions: js import { type ChangeDetails, controllable, definePrimitive, matchesShortcut, type PrimitiveContext, type RootProps, text, } from 'tui' interface ToggleProps extends RootProps { value?: boolean defaultValue?: boolean onValueChange?: value: boolean, details: ChangeDetails = void } export const Toggle = definePrimitive { name: 'toggle', state: context: PrimitiveContext