aboutsummaryrefslogtreecommitdiff
path: root/vendor/libghostty-vt/src/terminal/c/cell.zig
blob: c9746da7254f558a617eb54d9448c55f805bfd10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const std = @import("std");
const testing = std.testing;
const lib = @import("../lib.zig");
const page = @import("../page.zig");
const Cell = page.Cell;
const color = @import("../color.zig");
const style_c = @import("style.zig");
const Result = @import("result.zig").Result;

/// C: GhosttyCell
pub const CCell = u64;

/// C: GhosttyCellContentTag
pub const ContentTag = enum(c_int) {
    codepoint = 0,
    codepoint_grapheme = 1,
    bg_color_palette = 2,
    bg_color_rgb = 3,
};

/// C: GhosttyCellWide
pub const Wide = enum(c_int) {
    narrow = 0,
    wide = 1,
    spacer_tail = 2,
    spacer_head = 3,
};

/// C: GhosttyCellSemanticContent
pub const SemanticContent = enum(c_int) {
    output = 0,
    input = 1,
    prompt = 2,
};

/// C: GhosttyCellData
pub const CellData = enum(c_int) {
    invalid = 0,

    /// The codepoint of the cell (0 if empty or bg-color-only).
    /// Output type: uint32_t * (stored as u21, zero-extended)
    codepoint = 1,

    /// The content tag describing what kind of content is in the cell.
    /// Output type: GhosttyCellContentTag *
    content_tag = 2,

    /// The wide property of the cell.
    /// Output type: GhosttyCellWide *
    wide = 3,

    /// Whether the cell has text to render.
    /// Output type: bool *
    has_text = 4,

    /// Whether the cell has styling (non-default style).
    /// Output type: bool *
    has_styling = 5,

    /// The style ID for the cell (for use with style lookups).
    /// Output type: uint16_t *
    style_id = 6,

    /// Whether the cell has a hyperlink.
    /// Output type: bool *
    has_hyperlink = 7,

    /// Whether the cell is protected.
    /// Output type: bool *
    protected = 8,

    /// The semantic content type of the cell (from OSC 133).
    /// Output type: GhosttyCellSemanticContent *
    semantic_content = 9,

    /// The palette index for the cell's background color.
    /// Only valid when content_tag is bg_color_palette.
    /// Output type: GhosttyColorPaletteIndex *
    color_palette = 10,

    /// The RGB value for the cell's background color.
    /// Only valid when content_tag is bg_color_rgb.
    /// Output type: GhosttyColorRgb *
    color_rgb = 11,

    /// Output type expected for querying the data of the given kind.
    pub fn OutType(comptime self: CellData) type {
        return switch (self) {
            .invalid => void,
            .codepoint => u32,
            .content_tag => ContentTag,
            .wide => Wide,
            .has_text, .has_styling, .has_hyperlink, .protected => bool,
            .style_id => u16,
            .semantic_content => SemanticContent,
            .color_palette => u8,
            .color_rgb => color.RGB.C,
        };
    }
};

pub fn get(
    cell_: CCell,
    data: CellData,
    out: ?*anyopaque,
) callconv(lib.calling_conv) Result {
    if (comptime std.debug.runtime_safety) {
        _ = std.meta.intToEnum(CellData, @intFromEnum(data)) catch {
            return .invalid_value;
        };
    }

    return switch (data) {
        .invalid => .invalid_value,
        inline else => |comptime_data| getTyped(
            cell_,
            comptime_data,
            @ptrCast(@alignCast(out)),
        ),
    };
}

fn getTyped(
    cell_: CCell,
    comptime data: CellData,
    out: *data.OutType(),
) Result {
    const cell: Cell = @bitCast(cell_);
    switch (data) {
        .invalid => return .invalid_value,
        .codepoint => out.* = @intCast(cell.codepoint()),
        .content_tag => out.* = @enumFromInt(@intFromEnum(cell.content_tag)),
        .wide => out.* = @enumFromInt(@intFromEnum(cell.wide)),
        .has_text => out.* = cell.hasText(),
        .has_styling => out.* = cell.hasStyling(),
        .style_id => out.* = cell.style_id,
        .has_hyperlink => out.* = cell.hyperlink,
        .protected => out.* = cell.protected,
        .semantic_content => out.* = @enumFromInt(@intFromEnum(cell.semantic_content)),
        .color_palette => out.* = cell.content.color_palette,
        .color_rgb => {
            const rgb = cell.content.color_rgb;
            out.* = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b };
        },
    }

    return .success;
}

test "get codepoint" {
    const cell: CCell = @bitCast(Cell.init('A'));
    var cp: u32 = 0;
    try testing.expectEqual(Result.success, get(cell, .codepoint, @ptrCast(&cp)));
    try testing.expectEqual(@as(u32, 'A'), cp);
}

test "get has_text" {
    const cell: CCell = @bitCast(Cell.init('A'));
    var has: bool = false;
    try testing.expectEqual(Result.success, get(cell, .has_text, @ptrCast(&has)));
    try testing.expect(has);
}

test "get empty cell" {
    const cell: CCell = @bitCast(Cell.init(0));
    var has: bool = true;
    try testing.expectEqual(Result.success, get(cell, .has_text, @ptrCast(&has)));
    try testing.expect(!has);
}

test "get wide" {
    var zig_cell = Cell.init('A');
    zig_cell.wide = .wide;
    const cell: CCell = @bitCast(zig_cell);
    var w: Wide = .narrow;
    try testing.expectEqual(Result.success, get(cell, .wide, @ptrCast(&w)));
    try testing.expectEqual(Wide.wide, w);
}