vt: a terminal's emulation
#import "trame:vt". libghostty-vt, Ghostty's terminal emulation, as the source of trame:term's grid. The bytes of
a child go in, a grid of cells, a scrollback, modes and a selection come out, and Ghostty's encoders turn the keys and the
mouse into bytes. The program owns the child (a pseudo-terminal, a socket): what the emulation sends back goes to its
callback. Why one emulator, and why this one: src/md/features/local-terminal.md.
heard := vt.Callbacks{
user = &shell,
send = proc(user: rawptr, bytes: string) { child_write((^Shell)(user), bytes) }, // typed, pasted, the mouse, the answers
title = proc(user: rawptr, title: string) { tab_rename((^Shell)(user), title) },
resized = proc(user: rawptr, cols, rows: int, cell: [2]int) { child_resize((^Shell)(user), cols, rows) },
}
t := vt.create(80, 24, heard, my_colours()) // nil: the emulation is not linked in (vt.AVAILABLE)
defer vt.destroy(t)
v: term.View
term.init(&v, vt.source(t), 80, 24)
defer term.destroy(&v)
vt.feed(t, bytes_read) // on the interface's thread: what a reader thread postedThe program
A program declares toolbox.Feature.Vt in its Project (toolbox.md):
- the define
-define:GT_VT=true; - the native part
vt:trame build_native -features:vtputslibghostty-vt.a(Windows:ghostty-vt-static.lib) in the toolkit'sbuild/native, besidelibtrame_native.a; - on Windows,
toolbox.vt_dedup: Zig'scompiler_rtin libghostty-vt and Rust'scompiler_builtinsin the native library define the same builtins, weak in COFF, which the linkers refuse twice; the program links a copy of the Rust library without the members that define them,build/vt_dedup/<name>.lib, and the original is not written (Linux allows them already,LINUX_LINK). A program that carries its own Rust library names it inLink.rust_libsand reads its path from<STEM>_LIBin its foreign import (toolbox.md); - in
THIRD-PARTY-NOTICES, libghostty-vt with Ghostty's MIT licence (src/vt/GHOSTTY-LICENSE.txt).
Without the define, vt.AVAILABLE is false, create returns nil and nothing of Ghostty is linked: a package may import
trame:vt for a program that does not want a terminal.
The API
| Procedure | What |
|---|---|
create(cols, rows, callbacks, colours, scrollback) -> ^Terminal | a terminal of cols by rows cells, scrollback rows above them (SCROLLBACK_DEFAULT, 10 000) |
destroy(t) | |
feed(t, bytes) | the child's output; its answers go to Callbacks.send before it returns |
resize(t, cols, rows, cell) | a new size: the rows are wrapped again. The source calls it once the grid rests |
recolour(t, colours) | the program's colours (a new theme); then term.recolour(&view) |
source(t) -> term.Source | the grid's source: every procedure of term.md, the optional ones too |
title(t) -> string | the title the child set last, valid until the next feed |
text(t, allocator) -> string | the screen and its scrollback, as the child wrote it (a wrapped line is one line) |
Callbacks (each may be nil, user goes to each):
send(user, bytes): bytes for the child. What is typed and pasted, the mouse, and the answers to its queries (a device status reportESC [6n, the modes). A program that asks waits for the answer.title(user, title): the child set its title (OSC 0 or 2).resized(user, cols, rows, cell): the grid took a new size, once it rested (term.REST_MS). The emulation has it already: the program tells the child.
Colours are 0xRRGGBBAA. A colour of alpha 0 stays Ghostty's own. foreground and background are those of a cell
without its own. named are the 16 colours a child asks for by number. When given, the 240 after them are made between
the background and the foreground (Ghostty's palette_generate), so the cube follows the theme.
Where it runs
A Terminal belongs to one thread: the one that feeds it and draws its grid, the interface's. Its callbacks run inside
feed, resize and the grid's calls, on that thread, with the context of the call. Reading the child blocks: a thread
of the program reads it and posts the bytes to the interface's thread (ui.post), which feeds them and asks a frame.
The selection
The selection is the emulation's: its ends follow their text when rows come, when the scrollback drops its oldest rows,
when the grid is wrapped again. term.selection_text(&v) reads it through Ghostty's formatter, wrapped lines joined.
The binding
src/vt/vt_gen.fm holds the numbers and the calls of libghostty-vt's C API that trame:vt uses. trame gen_vt writes it
from the headers of the built library (build/vt/ghostty/include/ghostty/vt). trame gen_vt -check fails when it differs.
A name that went from the headers, or a type the generator does not know, stops it. The layouts of the structures passed
by value are in src/vt/vt.fm, written by hand: tests/vt checks their sizes on the real library. A program may call
the binding itself (vt.ghostty_terminal_get, ...) for what Terminal does not give.
The build
libghostty-vt is a static library of its own, made by Zig, not cargo
(src/md/infrastructure/native-library.md). trame build_native -features:vt
takes the first that works:
TRAME_NATIVE_PREBUILT(the Nix package.#nativeholdslib/libghostty-vt.aandlib/VT_STAMP);build/native, when itsVT_STAMPis the pin;- the release archive
trame-vt-<os>-x64of the checkout's version, checked by its.sha256and itsVT_STAMP(not withTRAME_NATIVE_BUILD_LOCAL=1or-force); - Zig 0.17.0 (pinned by SHA-256) fetched into
build/vt, Ghostty's source at the commit of its tagvt-1.0.0fetched intobuild/vt/ghostty, thenzig build -Demit-lib-vt=true -Doptimize=ReleaseFast. About four minutes.
VT_STAMP is the pin itself: ghostty <commit> zig <version> <target> ReleaseFast. trame vt_package packs the archive a
release publishes.
Tests
tests/vt (./build/run_tests vt, after trame build_native -features:text,vt): the sizes of the structures, the
rows and a cell's look through the source, a resize that wraps the rows again, keys in normal and application cursor
mode, the selection's text, the answer to ESC [6n and the title through the callbacks.