summaryrefslogtreecommitdiff
path: root/paint.zig
blob: 97efc334d996655fe6f5e4af8c9e564d338ea8b1 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
const std = @import("std");
const ui = @import("ui");
const app_config = @import("app_config");
const ghostty_vt = @import("ghostty-vt");
const syntax_highlighting = app_config.syntax_highlighting;
const syntax_minimal_grammars = app_config.syntax_minimal_grammars;
const syntax_full_grammars = app_config.syntax_full_grammars;
const ts = if (syntax_highlighting) @import("tree-sitter") else struct {
    pub const Language = opaque {};
    pub const Query = opaque {};
};
const ts_queries = if (syntax_highlighting) @import("ts_queries") else struct {};

extern fn tree_sitter_ada() callconv(.c) *const ts.Language;
extern fn tree_sitter_bash() callconv(.c) *const ts.Language;
extern fn tree_sitter_c() callconv(.c) *const ts.Language;
extern fn tree_sitter_c_sharp() callconv(.c) *const ts.Language;
extern fn tree_sitter_clojure() callconv(.c) *const ts.Language;
extern fn tree_sitter_cpp() callconv(.c) *const ts.Language;
extern fn tree_sitter_css() callconv(.c) *const ts.Language;
extern fn tree_sitter_elixir() callconv(.c) *const ts.Language;
extern fn tree_sitter_erlang() callconv(.c) *const ts.Language;
extern fn tree_sitter_fortran() callconv(.c) *const ts.Language;
extern fn tree_sitter_go() callconv(.c) *const ts.Language;
extern fn tree_sitter_haskell() callconv(.c) *const ts.Language;
extern fn tree_sitter_html() callconv(.c) *const ts.Language;
extern fn tree_sitter_java() callconv(.c) *const ts.Language;
extern fn tree_sitter_javascript() callconv(.c) *const ts.Language;
extern fn tree_sitter_json() callconv(.c) *const ts.Language;
extern fn tree_sitter_kotlin() callconv(.c) *const ts.Language;
extern fn tree_sitter_ocaml() callconv(.c) *const ts.Language;
extern fn tree_sitter_pascal() callconv(.c) *const ts.Language;
extern fn tree_sitter_php() callconv(.c) *const ts.Language;
extern fn tree_sitter_powershell() callconv(.c) *const ts.Language;
extern fn tree_sitter_python() callconv(.c) *const ts.Language;
extern fn tree_sitter_ruby() callconv(.c) *const ts.Language;
extern fn tree_sitter_rust() callconv(.c) *const ts.Language;
extern fn tree_sitter_scala() callconv(.c) *const ts.Language;
extern fn tree_sitter_zig() callconv(.c) *const ts.Language;

pub const accent = if (app_config.app_kind == .replay) [3]u8{ 0xc8, 0x58, 0x58 } else [3]u8{ 0x6e, 0x4f, 0xd0 };
pub const accent_dim = if (app_config.app_kind == .replay) [3]u8{ 0x54, 0x38, 0x38 } else [3]u8{ 0x38, 0x3c, 0x54 };
const dark_tag_bg = if (app_config.app_kind == .replay) [3]u8{ 0x3e, 0x2a, 0x2a } else [3]u8{ 0x2c, 0x2a, 0x3e };

pub const Theme = struct {
    name: []const u8,
    bg: ?[3]u8,
    fg: ?[3]u8,
    tag_bg: [3]u8,
    tag_fg: [3]u8,
    kw: [3]u8,
    str: [3]u8,
    num: [3]u8,
    comment: [3]u8,
    lineno: [3]u8,
    scroll_track: [3]u8,
    scroll_thumb: [3]u8,
    palette: ?[16][3]u8,
};

pub const themes = [_]Theme{
    .{
        .name = "dark",
        .bg = null,
        .fg = null,
        .tag_bg = dark_tag_bg,
        .tag_fg = .{ 0x96, 0x96, 0x96 },
        .kw = .{ 0xcc, 0x70, 0xd8 },
        .str = .{ 0x8a, 0xb8, 0x7a },
        .num = .{ 0xd0, 0xc0, 0x70 },
        .comment = .{ 0xf0, 0xf0, 0xf0 },
        .lineno = .{ 0x5a, 0x5a, 0x5a },
        .scroll_track = .{ 0x2a, 0x2a, 0x2a },
        .scroll_thumb = .{ 0x52, 0x52, 0x52 },
        .palette = null,
    },
    .{
        .name = "acme",
        .bg = .{ 0xff, 0xff, 0xea },
        .fg = .{ 0x00, 0x00, 0x00 },
        .tag_bg = .{ 0xea, 0xff, 0xff },
        .tag_fg = .{ 0x00, 0x00, 0x00 },
        .kw = .{ 0x88, 0x22, 0x99 },
        .str = .{ 0x00, 0x77, 0x33 },
        .num = .{ 0xa0, 0x52, 0x00 },
        .comment = .{ 0x55, 0x55, 0x55 },
        .lineno = .{ 0x99, 0x99, 0x80 },
        .scroll_track = .{ 0x99, 0x99, 0x4c },
        .scroll_thumb = .{ 0xff, 0xff, 0xea },
        .palette = .{
            .{ 0x00, 0x00, 0x00 }, .{ 0xaa, 0x22, 0x22 }, .{ 0x22, 0x80, 0x22 }, .{ 0x88, 0x66, 0x00 },
            .{ 0x22, 0x44, 0xbb }, .{ 0x99, 0x33, 0x99 }, .{ 0x11, 0x88, 0x88 }, .{ 0x55, 0x55, 0x55 },
            .{ 0x88, 0x88, 0x88 }, .{ 0xcc, 0x33, 0x33 }, .{ 0x33, 0xaa, 0x33 }, .{ 0xaa, 0x77, 0x00 },
            .{ 0x33, 0x55, 0xdd }, .{ 0xbb, 0x44, 0xbb }, .{ 0x22, 0xaa, 0xaa }, .{ 0x00, 0x00, 0x00 },
        },
    },
};

var theme_idx: usize = 0;

pub fn T() *const Theme {
    return &themes[theme_idx];
}

pub fn nextTheme() void {
    theme_idx = (theme_idx + 1) % themes.len;
}

pub fn currentThemeName() []const u8 {
    return T().name;
}

pub fn setThemeByName(name: []const u8) bool {
    for (themes, 0..) |theme, i| {
        if (std.mem.eql(u8, theme.name, name)) {
            theme_idx = i;
            return true;
        }
    }
    return false;
}

pub fn rgbColor(c: [3]u8) ui.Color {
    return .{ .rgb = c };
}

pub fn colorOpt(c: ?[3]u8) ui.Color {
    return if (c) |x| .{ .rgb = x } else .default;
}

pub const Syn = enum(u8) { none, keyword, string, number, comment };

pub const SynStyle = struct {
    fg: [3]u8,
    bold: bool,
};

const HighlightLanguage = struct {
    lang: *const ts.Language,
    query: *ts.Query,
};

const HighlightTier = enum { minimal, full };

const HighlightSpec = struct {
    tier: HighlightTier,
    exts: []const []const u8,
    language: *const fn () callconv(.c) *const ts.Language,
    query_src: []const u8,
    compiled_query: ?*ts.Query = null,
};

var highlight_specs = if (syntax_minimal_grammars) [_]HighlightSpec{
    .{ .tier = .minimal, .exts = &[_][]const u8{ ".c", ".h" }, .language = tree_sitter_c, .query_src = ts_queries.c_highlights },
    .{ .tier = .minimal, .exts = &[_][]const u8{ ".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx" }, .language = tree_sitter_cpp, .query_src = ts_queries.cpp_highlights },
    .{ .tier = .minimal, .exts = &[_][]const u8{ ".zig", ".zon" }, .language = tree_sitter_zig, .query_src = ts_queries.zig_highlights },
} else [_]HighlightSpec{};

var full_highlight_specs = if (syntax_full_grammars) [_]HighlightSpec{
    .{ .tier = .full, .exts = &[_][]const u8{ ".adb", ".ads", ".ada" }, .language = tree_sitter_ada, .query_src = ts_queries.ada_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".sh", ".bash", ".zsh" }, .language = tree_sitter_bash, .query_src = ts_queries.bash_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".cs", ".csx" }, .language = tree_sitter_c_sharp, .query_src = ts_queries.c_sharp_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".clj", ".cljs", ".cljc", ".edn" }, .language = tree_sitter_clojure, .query_src = ts_queries.clojure_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{".css"}, .language = tree_sitter_css, .query_src = ts_queries.css_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".ex", ".exs" }, .language = tree_sitter_elixir, .query_src = ts_queries.elixir_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".erl", ".hrl" }, .language = tree_sitter_erlang, .query_src = ts_queries.erlang_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".f", ".for", ".ftn", ".f90", ".f95", ".f03", ".f08" }, .language = tree_sitter_fortran, .query_src = ts_queries.fortran_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{".go"}, .language = tree_sitter_go, .query_src = ts_queries.go_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".hs", ".lhs" }, .language = tree_sitter_haskell, .query_src = ts_queries.haskell_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".html", ".htm" }, .language = tree_sitter_html, .query_src = ts_queries.html_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{".java"}, .language = tree_sitter_java, .query_src = ts_queries.java_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".js", ".jsx", ".mjs", ".cjs" }, .language = tree_sitter_javascript, .query_src = ts_queries.javascript_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{".json"}, .language = tree_sitter_json, .query_src = ts_queries.json_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".kt", ".kts" }, .language = tree_sitter_kotlin, .query_src = ts_queries.kotlin_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".ml", ".mli" }, .language = tree_sitter_ocaml, .query_src = ts_queries.ocaml_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".pas", ".pp", ".p" }, .language = tree_sitter_pascal, .query_src = ts_queries.pascal_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".php", ".phtml", ".php3", ".php4", ".php5" }, .language = tree_sitter_php, .query_src = ts_queries.php_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".ps1", ".psm1", ".psd1" }, .language = tree_sitter_powershell, .query_src = ts_queries.powershell_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".py", ".pyw" }, .language = tree_sitter_python, .query_src = ts_queries.python_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".rb", ".rake" }, .language = tree_sitter_ruby, .query_src = ts_queries.ruby_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{".rs"}, .language = tree_sitter_rust, .query_src = ts_queries.rust_highlights },
    .{ .tier = .full, .exts = &[_][]const u8{ ".scala", ".sc" }, .language = tree_sitter_scala, .query_src = ts_queries.scala_highlights },
} else [_]HighlightSpec{};

fn tierEnabled(tier: HighlightTier) bool {
    return switch (tier) {
        .minimal => syntax_minimal_grammars,
        .full => syntax_full_grammars,
    };
}

fn specMatchesExt(spec: HighlightSpec, ext: []const u8) bool {
    for (spec.exts) |choice| {
        if (std.ascii.eqlIgnoreCase(ext, choice)) return true;
    }
    return false;
}

fn ensureHighlightLanguage(spec: *HighlightSpec) !HighlightLanguage {
    const lang = spec.language();
    if (spec.compiled_query) |query| return .{ .lang = lang, .query = query };
    var error_offset: u32 = 0;
    const query = try ts.Query.create(lang, spec.query_src, &error_offset);
    spec.compiled_query = query;
    return .{ .lang = lang, .query = query };
}

fn highlightLanguageForExt(ext: []const u8) !?HighlightLanguage {
    for (&highlight_specs) |*spec| {
        if (!tierEnabled(spec.tier)) continue;
        if (!specMatchesExt(spec.*, ext)) continue;
        return try ensureHighlightLanguage(spec);
    }
    for (&full_highlight_specs) |*spec| {
        if (!tierEnabled(spec.tier)) continue;
        if (!specMatchesExt(spec.*, ext)) continue;
        return try ensureHighlightLanguage(spec);
    }
    return null;
}

pub fn compileStartupHighlightQueries() void {
    if (!syntax_highlighting) return;
    for (&highlight_specs) |*spec| {
        if (spec.tier != .minimal) continue;
        if (!tierEnabled(spec.tier)) continue;
        _ = ensureHighlightLanguage(spec) catch {};
    }
}

pub fn synStyle(s: Syn) ?SynStyle {
    return switch (s) {
        .none => null,
        .keyword => .{ .fg = T().kw, .bold = true },
        .string => .{ .fg = T().str, .bold = false },
        .number => .{ .fg = T().num, .bold = false },
        .comment => .{ .fg = T().comment, .bold = true },
    };
}

fn synFor(name: []const u8) Syn {
    for ([_]struct { []const u8, Syn }{
        .{ "comment", .comment },
        .{ "string", .string },
        .{ "character", .string },
        .{ "number", .number },
        .{ "float", .number },
        .{ "boolean", .number },
        .{ "keyword", .keyword },
        .{ "include", .keyword },
        .{ "conditional", .keyword },
        .{ "repeat", .keyword },
    }) |m| {
        if (std.mem.indexOf(u8, name, m[0]) != null) return m[1];
    }
    return .none;
}

pub fn highlightFile(gpa: std.mem.Allocator, path: []const u8, content: []const u8) ![]u8 {
    return highlightFileRange(gpa, path, content, 0, content.len);
}

pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []const u8, start_byte_raw: usize, end_byte_raw: usize) ![]u8 {
    if (!syntax_highlighting) return &.{};
    const ext = std.fs.path.extension(path);
    const selected = (highlightLanguageForExt(ext) catch return &.{}) orelse return &.{};

    const start_byte = @min(start_byte_raw, content.len);
    const end_byte = @max(start_byte, @min(end_byte_raw, content.len));
    const source = content[start_byte..end_byte];
    const styles = try gpa.alloc(u8, source.len);
    errdefer gpa.free(styles);
    @memset(styles, 0);

    const parser = ts.Parser.create();
    defer parser.destroy();
    parser.setLanguage(selected.lang) catch return styles;
    const tree = parser.parseString(source, null) orelse return styles;
    defer tree.destroy();

    const cursor = ts.QueryCursor.create();
    defer cursor.destroy();
    cursor.exec(selected.query, tree.rootNode());

    while (cursor.nextMatch()) |match| {
        for (match.captures) |cap| {
            const syn = synFor(selected.query.captureNameForId(cap.index) orelse "");
            if (syn == .none) continue;
            var b: usize = cap.node.startByte();
            const end = @min(@as(usize, cap.node.endByte()), styles.len);
            while (b < end) : (b += 1) styles[b] = @intFromEnum(syn);
        }
    }
    return styles;
}

fn palColor(idx: u8) ui.Color {
    if (T().palette) |p| if (idx < 16) return rgbColor(p[idx]);
    return .{ .index = idx };
}

fn ghostColor(color: ghostty_vt.Style.Color, is_bg: bool) ui.Color {
    return switch (color) {
        .none => colorOpt(if (is_bg) T().bg else T().fg),
        .palette => |idx| palColor(idx),
        .rgb => |rgb| .{ .rgb = .{ rgb.r, rgb.g, rgb.b } },
    };
}

pub fn cellStyle(ci: ghostty_vt.PageList.Cell) ui.Style {
    const style = ci.style();
    var vs: ui.Style = .{
        .fg = ghostColor(style.fg_color, false),
        .bg = ghostColor(style.bg_color, true),
        .bold = style.flags.bold,
        .dim = style.flags.faint,
        .italic = style.flags.italic,
        .blink = style.flags.blink,
        .reverse = style.flags.inverse,
        .invisible = style.flags.invisible,
        .strikethrough = style.flags.strikethrough,
        .ul_style = switch (style.flags.underline) {
            .none => .off,
            .single => .single,
            .double => .double,
            .curly => .curly,
            .dotted => .dotted,
            .dashed => .dashed,
        },
    };
    switch (ci.cell.content_tag) {
        .bg_color_palette => vs.bg = palColor(ci.cell.content.color_palette.data),
        .bg_color_rgb => {
            const rgb = ci.cell.content.color_rgb;
            vs.bg = .{ .rgb = .{ rgb.r, rgb.g, rgb.b } };
        },
        else => {},
    }
    return vs;
}