aboutsummaryrefslogtreecommitdiff
path: root/vendor/libghostty-vt/src/terminal/c/style.zig
diff options
context:
space:
mode:
authorCan Celik <ogulcancelik@gmail.com>2026-09-10 21:51:57 +0300
committerGitHub <noreply@github.com>2026-09-10 21:51:57 +0300
commit425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch)
treee52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/terminal/c/style.zig
parent90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff)
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/terminal/c/style.zig')
-rw-r--r--vendor/libghostty-vt/src/terminal/c/style.zig124
1 files changed, 96 insertions, 28 deletions
diff --git a/vendor/libghostty-vt/src/terminal/c/style.zig b/vendor/libghostty-vt/src/terminal/c/style.zig
index 82729fa9..bea4d074 100644
--- a/vendor/libghostty-vt/src/terminal/c/style.zig
+++ b/vendor/libghostty-vt/src/terminal/c/style.zig
@@ -25,20 +25,32 @@ pub const Color = extern struct {
tag: ColorTag,
value: ColorValue,
+ // fromColor converts the tag as a plain integer, which requires
+ // the internal and C tag values to line up.
+ comptime {
+ const Tag = std.meta.Tag(style.Style.Color);
+ for (@typeInfo(Tag).@"enum".fields) |f| {
+ assert(f.value == @intFromEnum(@field(ColorTag, f.name)));
+ }
+ }
+
pub fn fromColor(c: style.Style.Color) Color {
- return switch (c) {
- .none => .{
- .tag = .none,
- .value = .{ ._padding = 0 },
- },
- .palette => |idx| .{
- .tag = .palette,
- .value = .{ .palette = idx },
- },
- .rgb => |rgb| .{
- .tag = .rgb,
- .value = .{ .rgb = rgb.cval() },
- },
+ // The value is built as a single integer rather than through
+ // the per-variant union fields: the C union layout puts
+ // .palette at byte 0 and .rgb at bytes 0-2, which is exactly
+ // the internal payload representation, so each variant is the
+ // payload zero-extended to the u64 union backing. This keeps
+ // the conversion to two stores (tag + value) per color, which
+ // matters for the per-cell style reads in the render state
+ // API.
+ const value: u64 = switch (c) {
+ .none => 0,
+ .palette => |idx| idx,
+ .rgb => |rgb| @as(u24, @bitCast(rgb)),
+ };
+ return .{
+ .tag = @enumFromInt(@intFromEnum(std.meta.activeTag(c))),
+ .value = .{ ._padding = value },
};
}
};
@@ -59,27 +71,83 @@ pub const Style = extern struct {
overline: bool,
underline: c_int,
- pub fn fromStyle(s: style.Style) Style {
- return .{
- .fg_color = .fromColor(s.fg_color),
- .bg_color = .fromColor(s.bg_color),
- .underline_color = .fromColor(s.underline_color),
- .bold = s.flags.bold,
- .italic = s.flags.italic,
- .faint = s.flags.faint,
- .blink = s.flags.blink,
- .inverse = s.flags.inverse,
- .invisible = s.flags.invisible,
- .strikethrough = s.flags.strikethrough,
- .overline = s.flags.overline,
- .underline = @intFromEnum(s.flags.underline),
+ /// The default (empty) style in C representation. C
+ pub const default: Style = .{
+ .fg_color = .{ .tag = .none, .value = .{ ._padding = 0 } },
+ .bg_color = .{ .tag = .none, .value = .{ ._padding = 0 } },
+ .underline_color = .{ .tag = .none, .value = .{ ._padding = 0 } },
+ .bold = false,
+ .italic = false,
+ .faint = false,
+ .blink = false,
+ .inverse = false,
+ .invisible = false,
+ .strikethrough = false,
+ .overline = false,
+ .underline = 0,
+ };
+
+ // fromStyle writes the eight bool fields as one 8-byte store,
+ // which requires them to be consecutive and to match the low
+ // eight flag bits in order.
+ comptime {
+ const flag_names = [8][:0]const u8{
+ "bold", "italic", "faint", "blink",
+ "inverse", "invisible", "strikethrough", "overline",
};
+ const base = @offsetOf(Style, "bold");
+ for (flag_names, 0..) |name, i| {
+ assert(@offsetOf(Style, name) == base + i);
+ assert(@bitOffsetOf(@FieldType(style.Style, "flags"), name) == i);
+ }
+ }
+
+ /// Write the C representation of the given style directly through
+ /// the out pointer. The hot per-cell style read in the render
+ /// state API uses this rather than `out.* = fromStyle(s)`: the
+ /// by-value form materializes a stack temporary plus a
+ /// @sizeOf(Style)-byte copy that LLVM does not elide.
+ pub fn write(s: style.Style, out: *Style) void {
+ out.size = @sizeOf(Style);
+ out.fg_color = .fromColor(s.fg_color);
+ out.bg_color = .fromColor(s.bg_color);
+ out.underline_color = .fromColor(s.underline_color);
+
+ // I know this looks scary, but this results in truly measurable
+ // performance improvements due to how often styles are written
+ // especially in render loops. We vectorize our 8-bit to 8-byte
+ // write.
+ //
+ // We have to align(1) because we're ptrCasting a u8 pointer.
+ @as(*align(1) u64, @ptrCast(&out.bold)).* = bytes: {
+ // Spread the low eight flag bits (the eight bool fields, in
+ // order) into one byte each and write them with a single
+ // 8-byte store.
+ const flag_bits: u8 = @truncate(@as(u16, @bitCast(s.flags)));
+ const masks: @Vector(8, u8) = .{ 1, 2, 4, 8, 16, 32, 64, 128 };
+ const hits = (@as(@Vector(8, u8), @splat(flag_bits)) & masks) == masks;
+ const bytes: @Vector(8, u8) = @select(
+ u8,
+ hits,
+ @as(@Vector(8, u8), @splat(1)),
+ @as(@Vector(8, u8), @splat(0)),
+ );
+ break :bytes @bitCast(bytes);
+ };
+
+ out.underline = @intFromEnum(s.flags.underline);
+ }
+
+ pub fn fromStyle(s: style.Style) Style {
+ var result: Style = undefined;
+ write(s, &result);
+ return result;
}
};
/// Returns the default style.
pub fn default_style(result: *Style) callconv(lib.calling_conv) void {
- result.* = .fromStyle(.{});
+ result.* = .default;
assert(result.size == @sizeOf(Style));
}