Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ fn configureObj(b: *Build, opts: *BunBuildOptions, obj: *Compile) void {

obj.no_link_obj = opts.os != .windows and !opts.no_llvm;


if (opts.enable_asan and !enableFastBuild(b)) {
if (@hasField(Build.Module, "sanitize_address")) {
if (opts.enable_fuzzilli) {
Expand Down Expand Up @@ -800,6 +799,11 @@ fn addInternalImports(b: *Build, mod: *Module, opts: *BunBuildOptions) void {
});
}

// libghostty-vt: Terminal VT emulator library from Ghostty
// Provides terminal escape sequence parsing, state management, input encoding
// Source is cloned by CMake to vendor/ghostty
addGhosttyModule(b, mod);

// Finally, make it so all modules share the same import table.
propagateImports(mod) catch @panic("OOM");
}
Expand Down Expand Up @@ -832,6 +836,49 @@ fn validateGeneratedPath(path: []const u8) void {
}
}

/// Adds the ghostty-vt module for terminal emulation functionality.
/// Ghostty source is cloned to vendor/ghostty by CMake (see BuildGhosttyVt.cmake).
///
/// The ghostty-vt module provides:
/// - VT escape sequence parsing (CSI, OSC, DCS, etc.)
/// - Terminal state management (screen, cursor, colors)
/// - Input encoding for terminal applications
fn addGhosttyModule(b: *Build, mod: *Module) void {

// Create the ghostty-vt module from vendor/ghostty/src/lib_vt.zig
const ghostty_vt = b.createModule(.{
.root_source_file = b.path("vendor/ghostty/src/lib_vt.zig"),
});

// Add terminal_options - build configuration for the terminal module
ghostty_vt.addAnonymousImport("terminal_options", .{
.root_source_file = b.path("src/deps/ghostty/terminal_options.zig"),
});

// Add unicode_tables - stub tables for unicode property lookups
ghostty_vt.addAnonymousImport("unicode_tables", .{
.root_source_file = b.path("src/deps/ghostty/unicode_tables.zig"),
});

// Add symbols_tables - stub tables for symbol detection
ghostty_vt.addAnonymousImport("symbols_tables", .{
.root_source_file = b.path("src/deps/ghostty/symbols_tables.zig"),
});

// Add props module for Properties type used by unicode_tables
ghostty_vt.addAnonymousImport("props", .{
.root_source_file = b.path("vendor/ghostty/src/unicode/props.zig"),
});

// Add build_options for ghostty's SIMD code
ghostty_vt.addAnonymousImport("build_options", .{
.root_source_file = b.path("src/deps/ghostty/build_options.zig"),
});

// Export ghostty module to bun
mod.addImport("ghostty", ghostty_vt);
}

const WindowsShim = struct {
exe: *Compile,
dbg: *Compile,
Expand Down
1 change: 1 addition & 0 deletions cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ set(BUN_DEPENDENCIES
LibArchive # must be loaded after zlib
HdrHistogram # must be loaded after zlib
Zstd
GhosttyVt # libghostty-vt terminal emulator library
)

include(CloneZstd)
Expand Down
22 changes: 22 additions & 0 deletions cmake/targets/BuildGhosttyVt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Build libghostty-vt - Terminal VT emulator library from Ghostty
# This clones the ghostty repository so Bun's build.zig can import it as a Zig module.
#
# libghostty-vt provides:
# - Terminal escape sequence parsing
# - Terminal state management (screen, cursor, scrollback)
# - Input event encoding (Kitty keyboard protocol)
# - OSC/DCS/CSI sequence handling
#
# Usage in Zig: @import("ghostty") gives access to the lib_vt.zig API

register_repository(
NAME
ghostty
REPOSITORY
ghostty-org/ghostty
TAG
v1.1.3
)

# The ghostty source is cloned to ${VENDOR_PATH}/ghostty
# Bun's build.zig will reference it directly as a Zig module
70 changes: 70 additions & 0 deletions src/bun.js/api/Terminal.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,76 @@ export default [
getter: "getControlFlags",
setter: "setControlFlags",
},

// ----- Virtual Terminal (ghostty-vt) API -----
// These methods provide terminal emulation: parsing escape sequences,
// maintaining screen state (cells, cursor, colors), and rendering.

// Feed data to the virtual terminal parser
// This processes escape sequences and updates the screen state
feed: {
fn: "feed",
length: 1,
},

// Get a specific cell from the screen buffer at (x, y)
// Returns { char, wide, styled } or null if out of bounds
at: {
fn: "at",
length: 2,
},

// Get a line of text relative to the bottom of the screen
// line(0) = bottom line, line(1) = one above bottom, etc.
line: {
fn: "line",
length: 1,
},

// Get the full screen content as text (getter)
text: {
getter: "getText",
},

// Get cursor position { x, y, visible, style }
cursor: {
getter: "getCursor",
},

// Get current screen dimensions
cols: {
getter: "getCols",
},
rows: {
getter: "getRows",
},

// Get terminal title (set via OSC sequences)
title: {
getter: "getTitle",
},

// Check if terminal is in alternate screen mode
alternateScreen: {
getter: "getAlternateScreen",
},

// Get scrollback buffer size
scrollbackLines: {
getter: "getScrollbackLines",
},

// Clear the screen
clear: {
fn: "clearScreen",
length: 0,
},

// Reset terminal to initial state
reset: {
fn: "resetTerminal",
length: 0,
},
},
}),
];
Loading