vt: expose size_report encoding in the C API

Add ghostty_size_report_encode() to libghostty-vt, following the
same pattern as focus encoding: a single stateless function that
writes a terminal size report escape sequence into a caller-provided
buffer.

The size_report.zig Style enum and Size struct now use lib.Enum and
lib.Struct so the types are automatically C-compatible when building
with c_abi, eliminating the need for duplicate type definitions in
the C wrapper. The C wrapper in c/size_report.zig re-exports these
types directly and provides the callconv(.c) encode entry point.

Supports mode 2048 in-band reports and XTWINOPS responses (CSI 14 t,
CSI 16 t, CSI 18 t).
This commit is contained in:
Mitchell Hashimoto
2026-03-17 16:28:21 -07:00
parent a1d7ad9243
commit 7bf89740dd
6 changed files with 239 additions and 25 deletions

View File

@@ -53,6 +53,7 @@ pub const RenderState = terminal.RenderState;
pub const Screen = terminal.Screen;
pub const ScreenSet = terminal.ScreenSet;
pub const Selection = terminal.Selection;
pub const size_report = terminal.size_report;
pub const SizeReportStyle = terminal.SizeReportStyle;
pub const StringMap = terminal.StringMap;
pub const Style = terminal.Style;
@@ -167,6 +168,7 @@ comptime {
@export(&c.focus_encode, .{ .name = "ghostty_focus_encode" });
@export(&c.mode_report_encode, .{ .name = "ghostty_mode_report_encode" });
@export(&c.paste_is_safe, .{ .name = "ghostty_paste_is_safe" });
@export(&c.size_report_encode, .{ .name = "ghostty_size_report_encode" });
@export(&c.color_rgb_get, .{ .name = "ghostty_color_rgb_get" });
@export(&c.sgr_new, .{ .name = "ghostty_sgr_new" });
@export(&c.sgr_free, .{ .name = "ghostty_sgr_free" });

View File

@@ -9,6 +9,7 @@ pub const mouse_event = @import("mouse_event.zig");
pub const mouse_encode = @import("mouse_encode.zig");
pub const paste = @import("paste.zig");
pub const sgr = @import("sgr.zig");
pub const size_report = @import("size_report.zig");
pub const terminal = @import("terminal.zig");
// The full C API, unexported.
@@ -87,6 +88,8 @@ pub const mouse_encoder_encode = mouse_encode.encode;
pub const paste_is_safe = paste.is_safe;
pub const size_report_encode = size_report.encode;
pub const terminal_new = terminal.new;
pub const terminal_free = terminal.free;
pub const terminal_reset = terminal.reset;
@@ -108,6 +111,7 @@ test {
_ = mouse_encode;
_ = paste;
_ = sgr;
_ = size_report;
_ = terminal;
// We want to make sure we run the tests for the C allocator interface.

View File

@@ -0,0 +1,81 @@
const std = @import("std");
const terminal_size_report = @import("../size_report.zig");
const Result = @import("result.zig").Result;
/// C: GhosttySizeReportStyle
pub const Style = terminal_size_report.Style;
/// C: GhosttySizeReportSize
pub const Size = terminal_size_report.Size;
pub fn encode(
style: Style,
size: Size,
out_: ?[*]u8,
out_len: usize,
out_written: *usize,
) callconv(.c) Result {
var writer: std.Io.Writer = .fixed(if (out_) |out| out[0..out_len] else &.{});
terminal_size_report.encode(&writer, style, size) catch |err| switch (err) {
error.WriteFailed => {
var discarding: std.Io.Writer.Discarding = .init(&.{});
terminal_size_report.encode(&discarding.writer, style, size) catch unreachable;
out_written.* = @intCast(discarding.count);
return .out_of_space;
},
};
out_written.* = writer.end;
return .success;
}
test "encode mode 2048" {
var buf: [64]u8 = undefined;
var written: usize = 0;
const result = encode(.mode_2048, .{
.rows = 24,
.columns = 80,
.cell_width = 9,
.cell_height = 18,
}, &buf, buf.len, &written);
try std.testing.expectEqual(.success, result);
try std.testing.expectEqualStrings("\x1B[48;24;80;432;720t", buf[0..written]);
}
test "encode csi 14 t" {
var buf: [64]u8 = undefined;
var written: usize = 0;
const result = encode(.csi_14_t, .{
.rows = 24,
.columns = 80,
.cell_width = 9,
.cell_height = 18,
}, &buf, buf.len, &written);
try std.testing.expectEqual(.success, result);
try std.testing.expectEqualStrings("\x1b[4;432;720t", buf[0..written]);
}
test "encode with insufficient buffer" {
var buf: [1]u8 = undefined;
var written: usize = 0;
const result = encode(.csi_18_t, .{
.rows = 24,
.columns = 80,
.cell_width = 9,
.cell_height = 18,
}, &buf, buf.len, &written);
try std.testing.expectEqual(.out_of_space, result);
try std.testing.expect(written > 1);
}
test "encode with null buffer" {
var written: usize = 0;
const result = encode(.csi_18_t, .{
.rows = 24,
.columns = 80,
.cell_width = 9,
.cell_height = 18,
}, null, 0, &written);
try std.testing.expectEqual(.out_of_space, result);
try std.testing.expect(written > 0);
}

View File

@@ -1,23 +1,23 @@
const std = @import("std");
const build_options = @import("terminal_options");
const lib = @import("../lib/main.zig");
const lib_target: lib.Target = if (build_options.c_abi) .c else .zig;
const CellCountInt = @import("size.zig").CellCountInt;
/// Output formats for terminal size reports written to the PTY.
pub const Style = enum {
/// In-band size reports (mode 2048)
mode_2048,
/// XTWINOPS: report text area size in pixels
csi_14_t,
/// XTWINOPS: report cell size in pixels
csi_16_t,
/// XTWINOPS: report text area size in characters
csi_18_t,
};
pub const Style = lib.Enum(lib_target, &.{
// In-band size reports (mode 2048)
"mode_2048",
// XTWINOPS: report text area size in pixels
"csi_14_t",
// XTWINOPS: report cell size in pixels
"csi_16_t",
// XTWINOPS: report text area size in characters
"csi_18_t",
});
/// Runtime size values used to encode terminal size reports.
pub const Size = struct {
pub const Size = lib.Struct(lib_target, struct {
/// Terminal row count in cells.
rows: CellCountInt,
@@ -29,15 +29,15 @@ pub const Size = struct {
/// Height of a single terminal cell in pixels.
cell_height: u32,
});
pub fn widthPixels(self: Size) u64 {
return @as(u64, self.columns) * @as(u64, self.cell_width);
}
fn widthPixels(s: Size) u64 {
return @as(u64, s.columns) * @as(u64, s.cell_width);
}
pub fn heightPixels(self: Size) u64 {
return @as(u64, self.rows) * @as(u64, self.cell_height);
}
};
fn heightPixels(s: Size) u64 {
return @as(u64, s.rows) * @as(u64, s.cell_height);
}
/// Encode a terminal size report sequence.
pub fn encode(
@@ -51,16 +51,16 @@ pub fn encode(
.{
size.rows,
size.columns,
size.heightPixels(),
size.widthPixels(),
heightPixels(size),
widthPixels(size),
},
),
.csi_14_t => try writer.print(
"\x1b[4;{};{}t",
.{
size.heightPixels(),
size.widthPixels(),
heightPixels(size),
widthPixels(size),
},
),