@bomb.sh/tty

中文文档
0.10.0发布于 2026/10/22MITES Module零依赖构建来源已验证近 30 天下载 3.6万

A terminal rendering backend for Clay, compiled to WebAssembly

原始使用文档
文档目录10 节
  1. Features
  2. Examples
  3. Architecture
  4. Output
  5. Input
  6. Usage
  7. Rendering
  8. Pointer detection
  9. Input parsing
  10. Development

@bomb.sh/tty

A low-level, platform-independent terminal renderer and event parser for JavaScript. You can use @bomb.sh/tty directly, or as the foundation for your own framework.

Features

Declarative terminal UI — Build terminal interfaces the same way you'd build a web page. @bomb.sh/tty uses Clay under the hood, giving you flexbox-like layout, pointer detection, and scroll containers — all rendered to the terminal as box-drawing characters and ANSI escape sequences.

Zero I/O — @bomb.sh/tty never reads stdin or writes stdout. You feed it bytes and get bytes back. This makes it trivially embeddable in any framework, any runtime, any event loop. There are no opinions about how you do I/O, just pure computation.

Runs everywhere — The entire engine is compiled to WebAssembly, so @bomb.sh/tty will run anywhere JavaScript runs with no native dependencies, and no build step for consumers.

Examples

See this keyboard example and more in the examples folder. This demo uses @bomb.sh/tty for all layout and input parsing.

Keyboard Events

The input parser decodes raw terminal bytes into structured events. Here you can see each key event as the string "hello world" is typed.

Keyboard events demo

Pointer Events

Here we see hover styles applied to UI elements in response to the pointer state. Clay drives the hit testing; no manual coordinate math required.

Pointer events demo

Architecture

@bomb.sh/tty does not do any I/O itself. On the ouput side, it converts UI elements into a raw sequence of bytes and pointer events, and on the input side, it converts a stream of raw bytes into structured events.

Output

With every frame, the entire UI tree is packed into a flat byte array and sent to WASM in a single call. On the C side, Clay runs layout, render commands are walked into a cell buffer, and the buffer is diffed against the previous frame. Only the cells that actually changed produce output. The result is an ANSI escape sequence that can be written directly to stdout. One trip to WASM per frame, double buffered, and only the bytes that need to change hit the output stream.

Because the WASM module is pure computation with no I/O, it runs anywhere WebAssembly does: Deno, Node, Bun, browsers, or any other runtime.

 TypeScript                        WASM (C)
+---------------+                +---------------------------+
|               |  Uint32Array   |                           |
| UI ops...     | =============> | Clay layout               |
|               |                |   -> render commands      |
+---------------+                |   -> cell buffer (back)   |
                                 |   -> diff against (front) |
                                 |   -> escape bytes         |
+---------------+                |                           |
|               | ANSI byte array|                           |
| stdout.write  | <============= |                           |
|               |                |                           |
+---------------+                +---------------------------+

Input

Raw bytes from stdin are fed into a WASM-based parser that recognizes VT/ANSI escape sequences, UTF-8 codepoints, and mouse protocols (VT200, SGR, urxvt). The parser maintains its own internal buffer so partial sequences that arrive across read boundaries are reassembled automatically. A lone ESC byte is held for a configurable latency window (default 25ms) before being emitted, giving multi-byte sequences time to arrive.

 TypeScript                        WASM (C)
+---------------+                +---------------------------+
|               |  raw byte array|                           |
| stdin.read    | =============> | trie match (keys/seqs)    |
|               |                |   -> mouse protocol       |
|               |                |   -> UTF-8 decode         |
+---------------+                |   -> ESC codes            |
                                 |                           |
+---------------+                |                           |
|               |  events[]      |                           |
| KeyEvent      | <============= |                           |
| MouseDownEvent|                |                           |
| MouseUpEvent  |                +---------------------------+
| MouseMoveEvent|
| WheelEvent    |
| ResizeEvent   |
+---------------+

Usage

Rendering

To render this:

╭───────────────╮
│ Hello, World! │
╰───────────────╯
import { close, createTerm, open, rgba, text } from "@bomb.sh/tty";

let term = await createTerm({ width: 80, height: 24 });

let { output } = term.render([
  open("greeting", {
    layout: { padding: { left: 1, right: 1, top: 1, bottom: 1 } },
    border: {
      color: rgba(0, 255, 0),
      left: 1,
      right: 1,
      top: 1,
      bottom: 1,
    },
    cornerRadius: { tl: 1, tr: 1, bl: 1, br: 1 },
  }),
  text("Hello, World!"),
  close(),
]);

process.stdout.write(output);

Pointer detection

Pass pointer state to render() to have @bomb.sh/tty do hit detection and return pointer events in addition to the byte sequence.

import { close, createTerm, fixed, grow, open, rgba, text } from "@bomb.sh/tty";

let term = await createTerm({ width: 80, height: 24 });

let { output, events } = term.render(
  [
    open("root", {
      layout: { width: grow(), height: grow(), direction: "ltr" },
    }),
    open("sidebar", {
      layout: { width: fixed(20), height: grow() },
      bg: rgba(30, 30, 40),
    }),
    text("Sidebar"),
    close(),
    open("main", {
      layout: { width: grow(), height: grow() },
    }),
    text("Main content"),
    close(),
    close(),
  ],
  {
    pointer: { x: mouseX, y: mouseY, down: mouseDown },
  },
);

for (let event of events) {
  // { type: "pointerenter", id: "sidebar" }
  // { type: "pointerleave", id: "sidebar" }
  // { type: "pointerclick", id: "main" }
  console.log(event);
}

process.stdout.write(output);

Input parsing

import { createInput } from "@bomb.sh/tty";

let input = await createInput({ escLatency: 25 });

process.stdin.setRawMode(true);
let timer: ReturnType<typeof setTimeout> | undefined;

process.stdin.on("data", (buf) => {
  clearTimeout(timer);

  let { events, pending } = input.scan(new Uint8Array(buf));

  for (let event of events) {
    dispatch(event);
  }

  // if a lone ESC is pending, wait and re-scan to flush it
  if (pending) {
    timer = setTimeout(() => {
      let flush = input.scan();
      for (let event of flush.events) {
        dispatch(event);
      }
    }, pending.delay);
  }
});

Development

For local source builds, toolchain setup, and clay submodule instructions, see BUILD.md.

Quick local validation:

make
deno task test

版本与发布记录

@bomb.sh/tty 共发布 5 个版本,首次发布于 2026/06/19;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.10.0latest2026/10/2217 天前
0.9.02026/09/052 个月前
0.8.02026/06/264 个月前
0.7.02026/06/194 个月前
0.0.0-register.02026/06/194 个月前

关于 @bomb.sh/tty 的常见问题

@bomb.sh/tty 是什么?

A terminal rendering backend for Clay, compiled to WebAssembly

如何安装 @bomb.sh/tty?

在项目目录运行 npm install @bomb.sh/tty,或使用 pnpm add / yarn add / bun add。

@bomb.sh/tty 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bomb.sh__tty 是否存在。

@bomb.sh/tty 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bomb.sh/tty 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bomb.sh/tty 可以商用吗?

@bomb.sh/tty 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bomb.sh/tty 的下载量有多少?

根据 npm 官方下载统计,@bomb.sh/tty 在 2026/10/09 – 2026/11/07 这 30 天内被下载 36,264 次,日均约 1,209 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bomb.sh/tty 还在维护吗?

最新版本 0.10.0 发布于 2026/10/22(17 天前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/06/19。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。