ghostling-libghostty-terminal
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGhostling — libghostty Terminal Emulator
Ghostling — libghostty-vt 终端模拟器
Skill by ara.so — Daily 2026 Skills collection.
Ghostling is a minimal viable terminal emulator built on libghostty-vt, the embeddable C library extracted from Ghostty. It uses Raylib for windowing/rendering and lives in a single C file. The project demonstrates how to wire libghostty-vt's VT parsing, terminal state, and render-state API to any 2D or GPU renderer.
What libghostty-vt Provides
libghostty-vt 提供的功能
- VT sequence parsing (SIMD-optimized)
- Terminal state: cursor, styles, text reflow, scrollback
- Render state management (what cells changed and how to draw them)
- Unicode / multi-codepoint grapheme handling
- Kitty keyboard protocol, mouse tracking, focus reporting
- Zero dependencies (not even libc) — WASM-compatible
libghostty-vt does NOT provide: windowing, rendering, PTY management, tabs, splits, or configuration.
- VT序列解析(SIMD优化)
- 终端状态管理:光标、样式、文本重排、回滚
- 渲染状态管理(追踪哪些单元格发生变化以及如何绘制)
- Unicode/多码点字形处理
- Kitty键盘协议、鼠标追踪、焦点报告
- 零依赖(甚至无需libc)——支持WASM
libghostty-vt 不提供以下功能: 窗口管理、渲染、PTY管理、标签页、分屏、配置。
Requirements
环境要求
| Tool | Version |
|---|---|
| CMake | 3.19+ |
| Ninja | any |
| C compiler | clang/gcc |
| Zig | 0.15.x (on PATH) |
| macOS | Xcode CLT or Xcode |
| 工具 | 版本 |
|---|---|
| CMake | 3.19+ |
| Ninja | 任意版本 |
| C语言编译器 | clang/gcc |
| Zig | 0.15.x(需在PATH中) |
| macOS | Xcode CLT 或 Xcode |
Build & Run
构建与运行
sh
undefinedsh
undefinedClone
克隆仓库
git clone https://github.com/ghostty-org/ghostling.git
cd ghostling
git clone https://github.com/ghostty-org/ghostling.git
cd ghostling
Debug build (slow — safety checks enabled)
调试构建(速度慢——启用了安全检查)
cmake -B build -G Ninja
cmake --build build
./build/ghostling
cmake -B build -G Ninja
cmake --build build
./build/ghostling
Release build (optimized)
发布构建(已优化)
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/ghostling
After first configure, only the build step is needed:
```sh
cmake --build buildWarning: Debug builds are very slow due to Ghostty's safety/correctness assertions. Always benchmark with Release builds.
cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/ghostling
首次配置后,仅需执行构建步骤:
```sh
cmake --build build注意: 调试构建由于Ghostty的安全/正确性断言会非常慢。请始终使用发布构建进行性能测试。
Project Structure
项目结构
ghostling/
├── main.c # Entire terminal implementation (single file)
├── CMakeLists.txt # Build config; fetches libghostty-vt + Raylib
└── demo.gifghostling/
├── main.c # 完整的终端实现(单文件)
├── CMakeLists.txt # 构建配置;自动拉取libghostty-vt + Raylib
└── demo.gifCore libghostty-vt API Patterns
libghostty-vt 核心API使用模式
All implementation lives in . Below are the key patterns extracted from it.
main.c所有实现都在中。以下是从中提取的关键使用模式。
main.c1. Initialize the Terminal
1. 初始化终端
c
#include "ghostty.h" // provided by libghostty-vt via CMake
// Create terminal with cols x rows cells
ghostty_terminal_t *terminal = ghostty_terminal_new(
&(ghostty_terminal_config_t){
.cols = cols,
.rows = rows,
}
);
if (!terminal) { /* handle error */ }c
#include "ghostty.h" // 由libghostty-vt通过CMake提供
// 创建cols x rows单元格的终端实例
ghostty_terminal_t *terminal = ghostty_terminal_new(
&(ghostty_terminal_config_t){
.cols = cols,
.rows = rows,
}
);
if (!terminal) { /* 处理错误 */ }2. Write Data from PTY into the Terminal
2. 将PTY中的数据写入终端
c
// Read from PTY fd, feed raw bytes to libghostty-vt
ssize_t n = read(pty_fd, buf, sizeof(buf));
if (n > 0) {
ghostty_terminal_write(terminal, buf, (size_t)n);
}c
// 从PTY文件描述符读取数据,将原始字节传入libghostty-vt
ssize_t n = read(pty_fd, buf, sizeof(buf));
if (n > 0) {
ghostty_terminal_write(terminal, buf, (size_t)n);
}3. Send Keyboard Input
3. 发送键盘输入
c
// libghostty-vt encodes the correct escape sequences
ghostty_key_event_t ev = {
.key = GHOSTTY_KEY_A, // key enum
.mods = GHOSTTY_MODS_CTRL, // modifier flags
.action = GHOSTTY_ACTION_PRESS,
.composing = false,
};
uint8_t out[64];
size_t out_len = 0;
ghostty_terminal_key(terminal, &ev, out, sizeof(out), &out_len);
// Write encoded bytes to PTY
if (out_len > 0) write(pty_fd, out, out_len);c
// libghostty-vt会编码正确的转义序列
ghostty_key_event_t ev = {
.key = GHOSTTY_KEY_A, // 按键枚举值
.mods = GHOSTTY_MODS_CTRL, // 修饰符标志
.action = GHOSTTY_ACTION_PRESS,
.composing = false,
};
uint8_t out[64];
size_t out_len = 0;
ghostty_terminal_key(terminal, &ev, out, sizeof(out), &out_len);
// 将编码后的字节写入PTY
if (out_len > 0) write(pty_fd, out, out_len);4. Send Mouse Events
4. 发送鼠标事件
c
ghostty_mouse_event_t mev = {
.x = cell_col, // cell column
.y = cell_row, // cell row
.button = GHOSTTY_MOUSE_LEFT,
.action = GHOSTTY_MOUSE_PRESS,
.mods = GHOSTTY_MODS_NONE,
};
uint8_t out[64];
size_t out_len = 0;
ghostty_terminal_mouse(terminal, &mev, out, sizeof(out), &out_len);
if (out_len > 0) write(pty_fd, out, out_len);c
ghostty_mouse_event_t mev = {
.x = cell_col, // 单元格列号
.y = cell_row, // 单元格行号
.button = GHOSTTY_MOUSE_LEFT,
.action = GHOSTTY_MOUSE_PRESS,
.mods = GHOSTTY_MODS_NONE,
};
uint8_t out[64];
size_t out_len = 0;
ghostty_terminal_mouse(terminal, &mev, out, sizeof(out), &out_len);
if (out_len > 0) write(pty_fd, out, out_len);5. Resize the Terminal
5. 调整终端大小
c
ghostty_terminal_resize(terminal, new_cols, new_rows);
// libghostty-vt handles text reflow automatically
// Send SIGWINCH to the child process after this
struct winsize ws = { .ws_col = new_cols, .ws_row = new_rows };
ioctl(pty_fd, TIOCSWINSZ, &ws);
kill(child_pid, SIGWINCH);c
ghostty_terminal_resize(terminal, new_cols, new_rows);
// libghostty-vt会自动处理文本重排
// 之后向子进程发送SIGWINCH信号
struct winsize ws = { .ws_col = new_cols, .ws_row = new_rows };
ioctl(pty_fd, TIOCSWINSZ, &ws);
kill(child_pid, SIGWINCH);6. Render: Walk the Render State
6. 渲染:遍历渲染状态
The render state API tells you exactly which cells changed and how to draw them — no need to redraw everything every frame.
c
// Get render state handle
ghostty_render_state_t *rs = ghostty_terminal_render_state(terminal);
// Begin a render pass (snapshot current state)
ghostty_render_state_begin(rs);
// Iterate dirty cells
ghostty_render_cell_iter_t iter = {0};
ghostty_render_cell_t cell;
while (ghostty_render_state_next_cell(rs, &iter, &cell)) {
// cell.col, cell.row — grid position
// cell.codepoint — Unicode codepoint (0 = empty)
// cell.fg.r/g/b — foreground RGB
// cell.bg.r/g/b — background RGB
// cell.attrs.bold — bold flag
// cell.attrs.italic — italic flag
// cell.attrs.reverse — reverse video
// Example: draw with Raylib
Color fg = { cell.fg.r, cell.fg.g, cell.fg.b, 255 };
Color bg = { cell.bg.r, cell.bg.g, cell.bg.b, 255 };
Rectangle rect = {
.x = cell.col * cell_width,
.y = cell.row * cell_height,
.width = cell_width,
.height = cell_height,
};
DrawRectangleRec(rect, bg);
if (cell.codepoint != 0) {
char glyph[8] = {0};
// encode codepoint to UTF-8 yourself or use a helper
encode_utf8(cell.codepoint, glyph);
DrawText(glyph, (int)rect.x, (int)rect.y, font_size, fg);
}
}
// End render pass (marks cells as clean)
ghostty_render_state_end(rs);渲染状态API会准确告诉你哪些单元格发生了变化以及如何绘制——无需每次帧都重绘所有内容。
c
// 获取渲染状态句柄
ghostty_render_state_t *rs = ghostty_terminal_render_state(terminal);
// 开始渲染过程(快照当前状态)
ghostty_render_state_begin(rs);
// 遍历脏单元格
ghostty_render_cell_iter_t iter = {0};
ghostty_render_cell_t cell;
while (ghostty_render_state_next_cell(rs, &iter, &cell)) {
// cell.col, cell.row — 网格位置
// cell.codepoint — Unicode码点(0表示空)
// cell.fg.r/g/b — 前景色RGB值
// cell.bg.r/g/b — 背景色RGB值
// cell.attrs.bold — 粗体标志
// cell.attrs.italic — 斜体标志
// cell.attrs.reverse — 反显标志
// 示例:使用Raylib绘制
Color fg = { cell.fg.r, cell.fg.g, cell.fg.b, 255 };
Color bg = { cell.bg.r, cell.bg.g, cell.bg.b, 255 };
Rectangle rect = {
.x = cell.col * cell_width,
.y = cell.row * cell_height,
.width = cell_width,
.height = cell_height,
};
DrawRectangleRec(rect, bg);
if (cell.codepoint != 0) {
char glyph[8] = {0};
// 自行将码点编码为UTF-8或使用辅助函数
encode_utf8(cell.codepoint, glyph);
DrawText(glyph, (int)rect.x, (int)rect.y, font_size, fg);
}
}
// 结束渲染过程(标记单元格为已清理)
ghostty_render_state_end(rs);7. Scrollback
7. 回滚
c
// Scroll viewport up/down by N rows
ghostty_terminal_scroll(terminal, -3); // scroll up 3
ghostty_terminal_scroll(terminal, 3); // scroll down 3
// Scroll to bottom
ghostty_terminal_scroll_bottom(terminal);c
// 将视口向上/向下滚动N行
ghostty_terminal_scroll(terminal, -3); // 向上滚动3行
ghostty_terminal_scroll(terminal, 3); // 向下滚动3行
// 滚动到底部
ghostty_terminal_scroll_bottom(terminal);8. Cursor Position
8. 光标位置
c
ghostty_cursor_t cursor;
ghostty_terminal_cursor(terminal, &cursor);
// cursor.col, cursor.row — cell position
// cursor.visible — bool
// cursor.shape — GHOSTTY_CURSOR_BLOCK, _UNDERLINE, _BARc
ghostty_cursor_t cursor;
ghostty_terminal_cursor(terminal, &cursor);
// cursor.col, cursor.row — 单元格位置
// cursor.visible — 布尔值
// cursor.shape — GHOSTTY_CURSOR_BLOCK, _UNDERLINE, _BAR9. Cleanup
9. 清理资源
c
ghostty_terminal_free(terminal);c
ghostty_terminal_free(terminal);PTY Setup (POSIX)
PTY设置(POSIX系统)
libghostty-vt has no PTY management — you own this:
c
#include <pty.h> // openpty
#include <unistd.h>
#include <stdlib.h>
int master_fd, slave_fd;
struct winsize ws = { .ws_row = rows, .ws_col = cols,
.ws_xpixel = 0, .ws_ypixel = 0 };
openpty(&master_fd, &slave_fd, NULL, NULL, &ws);
pid_t child = fork();
if (child == 0) {
// Child: become session leader, attach slave PTY
setsid();
ioctl(slave_fd, TIOCSCTTY, 0);
dup2(slave_fd, STDIN_FILENO);
dup2(slave_fd, STDOUT_FILENO);
dup2(slave_fd, STDERR_FILENO);
close(master_fd);
close(slave_fd);
char *shell = getenv("SHELL");
if (!shell) shell = "/bin/sh";
execl(shell, shell, NULL);
_exit(1);
}
// Parent: use master_fd for read/write
close(slave_fd);libghostty-vt不提供PTY管理——这部分需要你自行实现:
c
#include <pty.h> // openpty
#include <unistd.h>
#include <stdlib.h>
int master_fd, slave_fd;
struct winsize ws = { .ws_row = rows, .ws_col = cols,
.ws_xpixel = 0, .ws_ypixel = 0 };
openpty(&master_fd, &slave_fd, NULL, NULL, &ws);
pid_t child = fork();
if (child == 0) {
// 子进程:成为会话领导者,关联从PTY
setsid();
ioctl(slave_fd, TIOCSCTTY, 0);
dup2(slave_fd, STDIN_FILENO);
dup2(slave_fd, STDOUT_FILENO);
dup2(slave_fd, STDERR_FILENO);
close(master_fd);
close(slave_fd);
char *shell = getenv("SHELL");
if (!shell) shell = "/bin/sh";
execl(shell, shell, NULL);
_exit(1);
}
// 父进程:使用master_fd进行读写
close(slave_fd);CMakeLists.txt Pattern
CMakeLists.txt 配置示例
The project fetches libghostty-vt automatically via CMake FetchContent:
cmake
cmake_minimum_required(VERSION 3.19)
project(ghostling C)
include(FetchContent)该项目通过CMake FetchContent自动拉取libghostty-vt:
cmake
cmake_minimum_required(VERSION 3.19)
project(ghostling C)
include(FetchContent)libghostty-vt
libghostty-vt
FetchContent_Declare(
libghostty
URL https://release.files.ghostty.org/tip/libghostty-vt-<platform>.tar.gz
)
FetchContent_MakeAvailable(libghostty)
FetchContent_Declare(
libghostty
URL https://release.files.ghostty.org/tip/libghostty-vt-<platform>.tar.gz
)
FetchContent_MakeAvailable(libghostty)
Raylib
Raylib
FetchContent_Declare(
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 5.0
)
FetchContent_MakeAvailable(raylib)
add_executable(ghostling main.c)
target_link_libraries(ghostling PRIVATE ghostty-vt raylib)
undefinedFetchContent_Declare(
raylib
GIT_REPOSITORY https://github.com/raysan5/raylib.git
GIT_TAG 5.0
)
FetchContent_MakeAvailable(raylib)
add_executable(ghostling main.c)
target_link_libraries(ghostling PRIVATE ghostty-vt raylib)
undefinedKey Enums & Constants
关键枚举与常量
c
// Keys
GHOSTTY_KEY_A … GHOSTTY_KEY_Z
GHOSTTY_KEY_UP, GHOSTTY_KEY_DOWN, GHOSTTY_KEY_LEFT, GHOSTTY_KEY_RIGHT
GHOSTTY_KEY_ENTER, GHOSTTY_KEY_BACKSPACE, GHOSTTY_KEY_ESCAPE, GHOSTTY_KEY_TAB
GHOSTTY_KEY_F1 … GHOSTTY_KEY_F12
// Modifiers (bitmask)
GHOSTTY_MODS_NONE
GHOSTTY_MODS_SHIFT
GHOSTTY_MODS_CTRL
GHOSTTY_MODS_ALT
GHOSTTY_MODS_SUPER
// Mouse buttons
GHOSTTY_MOUSE_LEFT, GHOSTTY_MOUSE_RIGHT, GHOSTTY_MOUSE_MIDDLE
GHOSTTY_MOUSE_WHEEL_UP, GHOSTTY_MOUSE_WHEEL_DOWN
// Cursor shapes
GHOSTTY_CURSOR_BLOCK, GHOSTTY_CURSOR_UNDERLINE, GHOSTTY_CURSOR_BARc
// 按键
GHOSTTY_KEY_A … GHOSTTY_KEY_Z
GHOSTTY_KEY_UP, GHOSTTY_KEY_DOWN, GHOSTTY_KEY_LEFT, GHOSTTY_KEY_RIGHT
GHOSTTY_KEY_ENTER, GHOSTTY_KEY_BACKSPACE, GHOSTTY_KEY_ESCAPE, GHOSTTY_KEY_TAB
GHOSTTY_KEY_F1 … GHOSTTY_KEY_F12
// 修饰符(位掩码)
GHOSTTY_MODS_NONE
GHOSTTY_MODS_SHIFT
GHOSTTY_MODS_CTRL
GHOSTTY_MODS_ALT
GHOSTTY_MODS_SUPER
// 鼠标按键
GHOSTTY_MOUSE_LEFT, GHOSTTY_MOUSE_RIGHT, GHOSTTY_MOUSE_MIDDLE
GHOSTTY_MOUSE_WHEEL_UP, GHOSTTY_MOUSE_WHEEL_DOWN
// 光标形状
GHOSTTY_CURSOR_BLOCK, GHOSTTY_CURSOR_UNDERLINE, GHOSTTY_CURSOR_BARCommon Patterns
常见使用模式
Non-blocking PTY Read Loop
非阻塞PTY读取循环
c
// Set master_fd non-blocking
fcntl(master_fd, F_SETFL, O_NONBLOCK);
// In your main loop:
uint8_t buf[4096];
ssize_t n;
while ((n = read(master_fd, buf, sizeof(buf))) > 0) {
ghostty_terminal_write(terminal, buf, (size_t)n);
}
// EAGAIN means no data available — not an errorc
// 将master_fd设置为非阻塞模式
fcntl(master_fd, F_SETFL, O_NONBLOCK);
// 在主循环中:
uint8_t buf[4096];
ssize_t n;
while ((n = read(master_fd, buf, sizeof(buf))) > 0) {
ghostty_terminal_write(terminal, buf, (size_t)n);
}
// EAGAIN表示无可用数据——并非错误Raylib Key → ghostty_key_t Mapping
Raylib按键 → ghostty_key_t 映射
c
ghostty_key_t raylib_key_to_ghostty(int rl_key) {
switch (rl_key) {
case KEY_A: return GHOSTTY_KEY_A;
case KEY_ENTER: return GHOSTTY_KEY_ENTER;
case KEY_BACKSPACE: return GHOSTTY_KEY_BACKSPACE;
case KEY_UP: return GHOSTTY_KEY_UP;
case KEY_DOWN: return GHOSTTY_KEY_DOWN;
// ... etc
default: return GHOSTTY_KEY_INVALID;
}
}c
ghostty_key_t raylib_key_to_ghostty(int rl_key) {
switch (rl_key) {
case KEY_A: return GHOSTTY_KEY_A;
case KEY_ENTER: return GHOSTTY_KEY_ENTER;
case KEY_BACKSPACE: return GHOSTTY_KEY_BACKSPACE;
case KEY_UP: return GHOSTTY_KEY_UP;
case KEY_DOWN: return GHOSTTY_KEY_DOWN;
// ... 其他按键
default: return GHOSTTY_KEY_INVALID;
}
}Scrollbar Rendering
滚动条渲染
c
int total_rows = ghostty_terminal_total_rows(terminal);
int viewport_rows = rows; // your grid height
int scroll_offset = ghostty_terminal_scroll_offset(terminal);
float bar_h = (float)viewport_rows / total_rows * window_height;
float bar_y = (float)scroll_offset / total_rows * window_height;
DrawRectangle(window_width - SCROLLBAR_W, (int)bar_y,
SCROLLBAR_W, (int)bar_h, GRAY);c
int total_rows = ghostty_terminal_total_rows(terminal);
int viewport_rows = rows; // 你的网格高度
int scroll_offset = ghostty_terminal_scroll_offset(terminal);
float bar_h = (float)viewport_rows / total_rows * window_height;
float bar_y = (float)scroll_offset / total_rows * window_height;
DrawRectangle(window_width - SCROLLBAR_W, (int)bar_y,
SCROLLBAR_W, (int)bar_h, GRAY);Troubleshooting
故障排查
| Problem | Fix |
|---|---|
Build fails: | Install Zig 0.15.x and ensure it's on |
| Debug build extremely slow | Use Release: |
| Terminal renders garbage | Verify you're calling |
| Child process not getting resize | Call |
| Kitty keyboard protocol broken | Known upstream Raylib/GLFW limitation — libghostty-vt supports it correctly but needs richer input events |
| Colors look wrong | Check |
| Ensure buffer passed is valid and |
| 问题 | 解决方法 |
|---|---|
构建失败: | 安装Zig 0.15.x并确保其在 |
| 调试构建速度极慢 | 使用发布构建: |
| 终端渲染内容乱码 | 确保在遍历单元格前调用 |
| 子进程未接收到大小调整信号 | 在 |
| Kitty键盘协议失效 | 这是Raylib/GLFW的已知上游限制——libghostty-vt本身支持该协议,但需要更丰富的输入事件 |
| 颜色显示异常 | 检查 |
| 确保传入的缓冲区有效且 |
What libghostty-vt Will NOT Do For You
libghostty-vt 不提供的功能
You must implement these yourself:
- PTY creation and process management
- Window creation and event loop
- Font loading and glyph rendering
- Clipboard read/write (OSC 52 bytes are provided, handling is yours)
- Tabs, splits, multiple windows
- Configuration UI
以下功能需要你自行实现:
- PTY创建与进程管理
- 窗口创建与事件循环
- 字体加载与字形渲染
- 剪贴板读写(会提供OSC 52字节数据,但处理逻辑需自行实现)
- 标签页、分屏、多窗口
- 配置UI
API Reference
API参考
Full libghostty-vt C API docs: https://libghostty.tip.ghostty.org/group__render.html
完整的libghostty-vt C API文档:https://libghostty.tip.ghostty.org/group__render.html