幽灵 (Yōulíng)
Ghostling

原始链接: https://github.com/ghostty-org/ghostling

## Ghostling:一个极简终端演示 Ghostling 是一个单文件 C 演示程序,展示了 libghostty 的功能,libghostty 是从 Ghostty GUI 中提取的一个可嵌入终端模拟库。它利用 Raylib 进行窗口管理和渲染,展示了 libghostty 在传统 GUI 环境之外的灵活性。 虽然 Ghostling 不是一个功能齐全的终端,但它提供了令人惊讶的强大功能,包括调整大小并重排文本、24 位/256 色支持、文本样式(粗体、斜体)、Unicode 处理、键盘和鼠标输入(包括 Kitty 协议支持)以及滚动历史记录。这些功能由 libghostty-vt 提供支持,libghostty-vt 是一个零依赖库,用于处理 VT 序列解析和终端状态。 Ghostling 优先考虑核心模拟,省略了全功能终端中常见的选项卡、拆分和配置等功能——这些功能留给开发者实现。它被设计为一个易于理解的 libghostty C API 示例,其经过验证且优化的代码库受益于数百万 Ghostty GUI 用户。 Ghostling 使用 CMake 构建,需要 C 编译器和 Zig,它为通过其 C API 和潜在的社区驱动绑定将终端功能嵌入到各种应用程序和语言中提供了一个基础。

## Ghostling & Libghostty:将TUI带到桌面应用 Ghostling基于libghostty构建,允许开发者将文本用户界面(TUI)打包为原生桌面应用程序——类似于Electron打包Web应用程序的方式。这使得从现有的基于终端的工具创建跨平台桌面应用程序成为可能,甚至在Windows上也可以。 讨论亮点包括它在Blisswriter(一个剧本编写工具)和Trolley(打包TUI)等项目中的应用。开发者们正在探索其将CLI工具带到移动平台(Android/iOS)的潜力,并欣赏它与iTerm2等替代方案相比的渲染速度。 一个关键的技术细节是将二进制资源(字体等)嵌入到代码中,讨论涉及CMake基于的头文件生成、`xxd`和`objcopy`等方法。对话还涉及终端模拟器与窗口管理器中选项卡归属的争论,许多人提倡窗口管理器控制,但承认在某些工作流程中应用程序级别的选项卡具有便利性。
相关文章

原文

Ghostling is a demo project meant to highlight a minimum functional terminal built on the libghostty C API in a single C file.

The example uses Raylib for windowing and rendering. It is single-threaded (although libghostty-vt supports threading) and uses a 2D graphics renderer instead of a direct GPU renderer like the primary Ghostty GUI. This is to showcase the flexibility of libghostty and how it can be used in a variety of contexts.

Warning

The Ghostling terminal isn't meant to be a full featured, daily use terminal. It is a minimal viable terminal based on libghostty. Also, since this is basically a demo, I didn't carefully audit every single place for correctness, and this is C, so you've been warned!

Ghostling Demo

Libghostty is an embeddable library extracted from Ghostty's core, exposing a C and Zig API so any application can embed correct, fast terminal emulation.

Ghostling uses libghostty-vt, a zero-dependency library (not even libc) that handles VT sequence parsing, terminal state management (cursor position, styles, text reflow, scrollback, etc.), and renderer state management. It contains no renderer drawing or windowing code; the consumer (Ghostling, in this case) provides its own. The core logic is extracted directly from Ghostty and inherits all of its real-world benefits: excellent, accurate, and complete terminal emulation support, SIMD-optimized parsing, leading Unicode support, highly optimized memory usage, and a robust fuzzed and tested codebase, all proven by millions of daily active users of Ghostty GUI.

Despite being a minimal, thin layer above libghostty, look at all the features you do get:

  • Resize with text reflow
  • Full 24-bit color and 256-color palette support
  • Bold, italic, and inverse text styles
  • Unicode and multi-codepoint grapheme handling (no shaping or layout)
  • Keyboard input with modifier support (Shift, Ctrl, Alt, Super)
  • Kitty keyboard protocol support
  • Mouse tracking (X10, normal, button, and any-event modes)
  • Mouse reporting formats (SGR, URxvt, UTF8, X10)
  • Scroll wheel support (viewport scrollback or forwarded to applications)
  • Scrollbar with mouse drag-to-scroll
  • Focus reporting (CSI I / CSI O)
  • And more. Effectively all the terminal emulation features supported by Ghostty!

These features aren't properly exposed by libghostty-vt yet but will be:

  • Kitty Graphics Protocol
  • OSC clipboard support
  • OSC title setting

These are things that could work but haven't been tested or aren't implemented in Ghostling itself:

  • Windows support (libghostty-vt supports Windows)

This list is incomplete and we'll add things as we find them.

libghostty is focused on core terminal emulation features. As such, you don't get features that are provided by the GUI above the terminal emulation layer, such as:

  • Tabs
  • Multiple windows
  • Splits
  • Session management
  • Configuration file or GUI
  • Search UI (although search internals are provided by libghostty-vt)

These are the things that libghostty consumers are expected to implement on their own, if they want them. This example doesn't implement these to try to stay as minimal as possible.

Requires CMake 3.19+, a C compiler, and Zig 0.15.x on PATH. Raylib is fetched automatically via CMake's FetchContent if not already installed.

cmake -B build -G Ninja
cmake --build build
./build/ghostling

Warning

Debug builds are VERY SLOW since Ghostty included a lot of extra safety and correctness checks. Do not benchmark debug builds.

For a release (optimized) build:

cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build

After the initial configure, you only need to run the build step:

libghostty-vt has a fully capable and proven Zig API. Ghostty GUI itself uses this and is a good -- although complex -- example of how to use it. However, this demo is meant to showcase the minimal C API since C is so much more broadly used and accessible to a wide variety of developers and language ecosystems.

What about Rust or any other language?

libghostty-vt has a C API and can have zero dependencies, so it can be used with minimally thin bindings in basically any language. I'm not sure yet if the Ghostty project will maintain official bindings for languages other than C and Zig, but I hope the community will create and maintain bindings for many languages!

Does libghostty require Raylib?

No no no! libghostty has no opinion about the renderer or GUI framework used; it's even standalone WASM-compatible for browsers and other environments.

libghostty provides a high-performance render state API which only keeps track of the state required to build a renderer. This is the same API used by Ghostty GUI for Metal and OpenGL rendering and in this repository for the Raylib 2D graphics API. You can layer any renderer on top of this!

I needed to pick something. Really, any build system and any library could be used. CMake is widely used and supported, and Raylib is a simple and elegant library for windowing and 2D rendering that is easy to set up. Don't get bogged down in these details!

联系我们 contact @ memedata.com