@b9g/termdom

中文文档
0.2.5发布于 2026/11/02MITTypeScriptESM + CJS构建来源已验证近 30 天下载 2073

HTML, CSS and DOM for terminal emulators.

原始使用文档
文档目录7 节
  1. Features
  2. How it works
  3. Examples
  4. Runtimes
  5. Compatibility
  6. Name
  7. License

TermDOM

Build terminal apps with HTML, CSS, and DOM.

Klondike solitaire rendered by TermDOM

TermDOM renders HTML and CSS in the terminal. It draws a real DOM and redraws when it changes, so you can write terminal apps with plain JavaScript or any frontend framework.

npm install @b9g/termdom
import {TermDOM} from "@b9g/termdom";

const term = new TermDOM();
term.attach();

const {document} = term;
document.body.innerHTML = `
  <style>
    .card { border: 1px solid #5fafff; padding: 0 1ch; width: 36ch; }
    .title { color: #5fafff; font-weight: bold; }
    progress { width: 25ch; }
    progress::slider-fill { background-color: green; }
    .pct { color: #888; }
  </style>
  <div class="card">
    <div class="title">Installing</div>
    <div>
      <progress id="bar" max="100" value="0"></progress>
      <span class="pct" id="pct"></span>
    </div>
  </div>
`;

let n = 0;
setInterval(() => {
  n = (n + 1) % 101;
  document.querySelector("progress")!.value = n;
  document.getElementById("pct")!.textContent = String(n).padStart(3) + "%";
}, 50);

The card above, animating in a terminal

Features

  • Stylesheets: the cascade and inheritance, drawn as ANSI colors and attributes.
  • Layout: the box model, flexbox, grid, and tables, in whole cells.
  • Text: correct widths for CJK, emoji, and combining characters; Hebrew and Arabic in visual order with shaping.
  • DOM: querySelector(), MutationObserver, ResizeObserver, and getBoundingClientRect(), measured in cells.
  • Events: keyboard, mouse, focus, and paste, decoded from stdin.
  • Forms: inputs, textareas, selects, checkboxes, radios, and sliders, styleable with CSS.
  • Scrolling: documents and overflow boxes, by wheel, keys, or scrollTo().
  • Web Components: custom elements, shadow DOM, <slot>, and scoped styles.
  • Selection: drag to select, ::selection, and readline keys in text fields.
  • Fullscreen: requestFullscreen() uses the alternate screen and restores the shell on exit.
  • Images and canvas: PNG, JPEG, GIF, and BMP, and 2D and cell-grid canvases, drawn two pixels per cell. A csp option and "fetch" listener control what loads.

How it works

TermDOM runs the browser's rendering pipeline on a grid of character cells. By default 1px and 1ch are both one cell. Each frame, it recomputes style and layout for what changed, paints into a cell buffer, and writes the difference from the last frame as ANSI escape sequences. Input from stdin becomes DOM events.

Examples

  • markdown.ts — a paging Markdown viewer.
  • chat.ts — a streaming LLM chat client using ch.at.
  • todomvc.ts — the official TodoMVC, with only the stylesheet changed.
  • fuzzy-finder.ts — a file picker that prints the selection to stdout.
  • weather.ts — a forecast from Open-Meteo, with city search.
  • popover.ts — a menu bar built from popover attributes.
  • codemirror.ts — CodeMirror editing a file, with line numbers and syntax highlighting.
  • solitaire.ts — the solitaire above, played by keyboard or mouse.
  • hello-{react,vue,svelte,crank}.ts — a key-press counter in each framework, using its standard renderer.
  • ssh.ts — an SSH server that gives each session its own document.
  • shell.ts — ls, cat, less, cd, and pwd. On termdom.org, it browses this repository.
  • images.ts — an image under each object-fit. Pass a path or URL to show your own.
  • canvas.ts — an animated 2D canvas and Conway's Life on a cell-grid canvas.

More are in examples/, and most also run in the browser at termdom.org/examples.

Runtimes

Node, Bun, and Deno. There are no native dependencies, so bun build --compile works.

Compatibility

COMPATIBILITY.md lists supported features, generated by testing each one.

Name

Not to be confused with DomTerm by Per Bothner, which puts a terminal in the DOM. TermDOM puts the DOM in a terminal.

License

MIT

版本与发布记录

@b9g/termdom 共发布 17 个版本,首次发布于 2026/08/08;过去 12 个月发布了 17 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.5latest2026/11/026 天前
0.2.42026/10/2613 天前
0.2.32026/10/1623 天前
0.2.22026/10/1623 天前
0.2.12026/10/1623 天前
0.2.02026/10/1227 天前

关于 @b9g/termdom 的常见问题

@b9g/termdom 是什么?

HTML, CSS and DOM for terminal emulators.

如何安装 @b9g/termdom?

在项目目录运行 npm install @b9g/termdom,或使用 pnpm add / yarn add / bun add。

@b9g/termdom 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@b9g/termdom 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@b9g/termdom 需要什么版本的 Node.js?

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

@b9g/termdom 可以商用吗?

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

@b9g/termdom 的下载量有多少?

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

@b9g/termdom 还在维护吗?

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