Ghostty 编译为 WASM,兼容 xterm.js API
Ghostty compiled to WASM with xterm.js API compatibility

原始链接: https://github.com/coder/ghostty-web

## Ghostty-web:浏览器中的真正VT100终端 Ghostty-web 使用 xterm.js API 为 Web 应用程序带来真正的 VT100 终端体验,不同于 xterm.js 这种 JavaScript 的*近似实现*。它利用相同的、经过实战检验的模拟器,为原生 Ghostty 应用程序提供支持,通过约 400KB 的 WASM 包实现,且没有任何运行时依赖。 最初为 Mux 应用程序开发,Ghostty-web 在 xterm.js 难以处理的方面表现出色——特别是对从右到左的语言、Devanagari 和阿拉伯语等复杂脚本的支持,以及对 XTPUSHSGR/XTPOPSGR 等功能的全面支持。 迁移很简单:将 `@xterm/xterm` 替换为 `ghostty-web`。可通过 `npx @ghostty-web/demo@next` 访问演示服务器。 Ghostty-web 使用 Zig 和 Bun 构建,目前正在修补 Ghostty 的源代码,但未来旨在利用原生 Ghostty WASM 分发版,同时保持与 xterm.js API 的兼容性。

Kyle Carbs 成功将 Ghostty(一个终端模拟器)编译为 WebAssembly (WASM),并使其兼容 xterm.js API。这使得在浏览器中直接运行终端成为可能。 该项目在 Hacker News 上分享后,获得了 Ghostty 组织 Mitchellh 的赞扬,他指出当前性能可能并非最佳,因为使用了视口捕获方法。他建议利用 Ghostty 新的 `RenderState API` 进行显著改进的、基于增量的渲染——与他们的 GPU 渲染器使用的相同 API。 另一位用户建议集成 `webassembly.sh`,以实现一个功能齐全的浏览器内 shell,并支持软件包安装,Kyle 计划实现此功能以提供更好的演示体验。目前,演示需要命令行访问。 核心成就证明了在 Web 浏览器环境中运行 Ghostty 的可行性。
相关文章

原文

NPM Version NPM Downloads npm bundle size license

Ghostty for the web with xterm.js API compatibility — giving you a proper VT100 implementation in the browser, not a JavaScript approximation of one.

  • Migrate from xterm by changing your import: @xterm/xtermghostty-web
  • WASM-compiled parser from Ghostty—the same code that runs the native app
  • Zero runtime dependencies, ~400KB WASM bundle

Originally created for Mux (a desktop app for isolated, parallel agentic development), but designed to be used anywhere.

npx @ghostty-web/demo@next

This starts a local HTTP server with a real shell on http://localhost:8080. Works best on Linux and macOS.

ghostty

xterm.js is everywhere—VS Code, Hyper, countless web terminals. But it has fundamental issues:

Issue xterm.js ghostty-web
RTL languages Broken since 2017 ✓ Works
Complex scripts (Devanagari, Arabic) Rendering issues ✓ Proper grapheme handling
XTPUSHSGR/XTPOPSGR Not supported ✓ Full support

xterm.js reimplements terminal emulation in JavaScript. Every escape sequence, every edge case, every Unicode quirk—all hand-coded. Ghostty's emulator is the same battle-tested code that runs the native Ghostty app.

ghostty-web aims to be API-compatible with the xterm.js API.

import { init, Terminal } from 'ghostty-web';

await init();

const term = new Terminal({
  fontSize: 14,
  theme: {
    background: '#1a1b26',
    foreground: '#a9b1d6',
  },
});

term.open(document.getElementById('terminal'));
term.onData((data) => websocket.send(data));
websocket.onmessage = (e) => term.write(e.data);

For a comprehensive client <-> server example, refer to the demo.

ghostty-web builds from Ghostty's source with a patch to expose additional functionality.

Requires Zig and Bun.

Mitchell Hashimoto (author of Ghostty) has been working on libghostty which makes this all possible. The patches are very minimal thanks to the work the Ghostty team has done, and we expect them to get smaller.

This library will eventually consume a native Ghostty WASM distribution once available, and will continue to provide an xterm.js compatible API.

At Coder we're big fans of Ghostty, so kudos to that team for all the amazing work.

MIT

联系我们 contact @ memedata.com