summaryrefslogtreecommitdiffstats
path: root/src/detection/terminalfont/terminalfont.c
blob: 7ef71206af67b157e726d41bde4dd8c497ce9e2b (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "terminalfont.h"
#include "common/io.h"
#include "common/properties.h"
#include "common/processing.h"
#include "common/debug.h"
#include "common/strutil.h"
#include "detection/terminalshell/terminalshell.h"

static void detectAlacritty(FFTerminalFontResult* terminalFont) {
    // Maybe using a toml parser to read the config file is better?
    // https://github.com/cktan/tomlc17

    // Doc: https://alacritty.org/config-alacritty.html#s26
    FF_STRBUF_AUTO_DESTROY fontNormal = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontFamily = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontStyle = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();

    do {
        FFpropquery fontQueryToml[] = {
            { "normal =", &fontNormal },
            { "size =", &fontSize },
        };

        // alacritty parses config files in this order
        if (ffParsePropFileConfigValues("alacritty/alacritty.toml", 2, fontQueryToml)) {
            break;
        }
        if (ffParsePropFileConfigValues("alacritty.toml", 2, fontQueryToml)) {
            break;
        }
        if (ffParsePropFileConfigValues(".alacritty.toml", 2, fontQueryToml)) {
            break;
        }
    } while (false);

    if (fontNormal.length > 0) {
        // { family = "Fira Code", style = "Medium" }
        ffStrbufTrimSpace(&fontNormal);
        ffStrbufTrimRight(&fontNormal, '}');
        ffStrbufTrimLeft(&fontNormal, '{');
        ffStrbufTrimSpace(&fontNormal);

        // family = "Fira Code", style = "Medium"
        ffStrbufReplaceAllC(&fontNormal, ',', '\n'); // Assume no commas in font names
        ffParsePropLines(fontNormal.chars, "family =", &fontFamily);
        ffParsePropLines(fontNormal.chars, "style =", &fontStyle);
    }

    if (fontFamily.length == 0) {
#if __APPLE__
        ffStrbufSetStatic(&fontFamily, "Menlo");
#elif _WIN32
        ffStrbufSetStatic(&fontFamily, "Consolas");
#else
        ffStrbufSetStatic(&fontFamily, "monospace");
#endif
    }
    if (fontStyle.length == 0) {
        ffStrbufSetStatic(&fontStyle, "Regular");
    }

    if (fontSize.length == 0) {
        ffStrbufSetStatic(&fontSize, "11.25");
    }

    ffFontInitMoveValues(&terminalFont->font, &fontFamily, &fontSize, &fontStyle);
}

static bool parseGhosttyConfig(FFstrbuf* path, FFstrbuf* fontName, FFstrbuf* fontNameFallback, FFstrbuf* fontSize) {
    FF_DEBUG("parsing config: %s", path->chars);

    FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY temp = ffStrbufCreate();
    if (!ffAppendFileBuffer(path->chars, &buffer)) {
        FF_DEBUG("cannot read config: %s", path->chars);
        return false;
    }

    char* line = NULL;
    size_t len = 0;
    while (ffStrbufGetline(&line, &len, &buffer)) {
        if (ffParsePropLine(line, "font-family =", &temp)) {
            FF_DEBUG("found font-family='%s' in %s", temp.chars, path->chars);
            if (fontName->length > 0) {
                ffStrbufDestroy(fontNameFallback);
                ffStrbufInitMove(fontNameFallback, fontName);
            }
            ffStrbufDestroy(fontName);
            ffStrbufInitMove(fontName, &temp);
        } else if (ffParsePropLine(line, "font-size =", fontSize)) {
            FF_DEBUG("found font-size='%s' in %s", temp.chars, path->chars);
            // Latter overrides former
            ffStrbufDestroy(fontSize);
            ffStrbufInitMove(fontSize, &temp);
        }
    }
    return true;
}

static void detectGhostty(const FFstrbuf* exe, FFTerminalFontResult* terminalFont, const char* configPathMac, const char* configPathUnix) {
    FF_DEBUG("detectGhostty: start");
    FF_STRBUF_AUTO_DESTROY configPath = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontNameFallback = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();

    if (configPathMac && configPathUnix) {
#if __APPLE__
        ffStrbufSet(&configPath, &instance.state.platform.homeDir);
        ffStrbufAppendS(&configPath, "Library/Application Support/");
        ffStrbufAppendS(&configPath, configPathMac); // com.mitchellh.ghostty/config
        parseGhosttyConfig(&configPath, &fontName, &fontNameFallback, &fontSize);
#endif

        if (instance.state.platform.configDirs.length > 0) {
            ffStrbufSet(&configPath, FF_LIST_FIRST(FFstrbuf, instance.state.platform.configDirs));
            ffStrbufAppendS(&configPath, configPathUnix); // ghostty/config
            parseGhosttyConfig(&configPath, &fontName, &fontNameFallback, &fontSize);
        }
    } else {
        // Try ghostty +show-config first
        FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
        const char* error = ffProcessAppendStdOut(&buffer, (char* const[]){
                                                               exe->chars,
                                                               "+show-config",
                                                               NULL,
                                                           });
        if (error == NULL) {
            char* line = NULL;
            size_t len = 0;
            while (ffStrbufGetline(&line, &len, &buffer)) {
                if (ffStrStartsWith(line, "font-family = ")) {
                    FF_DEBUG("found %s", line);
                    if (fontName.length > 0) {
                        ffStrbufDestroy(&fontNameFallback);
                        ffStrbufInitMove(&fontNameFallback, &fontName);
                    }
                    ffStrbufSetNS(&fontName, (uint32_t) (len - strlen("font-family = ")), line + strlen("font-family = "));
                } else if (ffStrStartsWith(line, "font-size = ")) {
                    FF_DEBUG("found %s", line);
                    // `ghostty +show-config` reports only one font size even if the config has multiple font sizes
                    ffStrbufSetNS(&fontSize, (uint32_t) (len - strlen("font-size = ")), line + strlen("font-size = "));
                }
            }
        } else {
            FF_DEBUG("`ghostty +show-config` failed: %s", error);
        }
    }

    if (fontName.length == 0) {
        ffStrbufAppendS(&fontName, "JetBrainsMono Nerd Font");
        FF_DEBUG("using default family='%s'", fontName.chars);
    }

    if (fontSize.length == 0) {
        ffStrbufAppendS(&fontSize,
#if __APPLE__
            "13"
#else
            "12"
#endif
        );
        FF_DEBUG("using default size='%s'", fontSize.chars);
    }

    ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
    if (fontNameFallback.length > 0) {
        FF_DEBUG("applying fallback family='%s'", fontNameFallback.chars);
        ffFontInitValues(&terminalFont->fallback, fontNameFallback.chars, NULL);
    }
    FF_DEBUG("result family='%s' size='%s'%s", fontName.chars, fontSize.chars, fontNameFallback.length ? " (with fallback)" : "");
    FF_DEBUG("detectGhostty: end");
}

FF_A_UNUSED static void detectTTY(FFTerminalFontResult* terminalFont) {
    FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();

    ffParsePropFile(FASTFETCH_TARGET_DIR_ETC "/vconsole.conf", "Font =", &fontName);

    if (fontName.length == 0) {
        ffStrbufAppendS(&fontName, "VGA default kernel font ");
        ffProcessAppendStdOut(&fontName, (char* const[]){ "showconsolefont", "--info", NULL });

        ffStrbufTrimRight(&fontName, ' ');
    }

    if (fontName.length > 0) {
        ffFontInitCopy(&terminalFont->font, fontName.chars);
    } else {
        ffStrbufAppendS(&terminalFont->error, "Couldn't find Font in " FASTFETCH_TARGET_DIR_ETC "/vconsole.conf");
    }
}

FF_A_UNUSED static bool detectKitty(const FFstrbuf* exe, FFTerminalFontResult* result) {
    FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();

    char fontHex[512] = "", sizeHex[512] = "";
    // https://github.com/fastfetch-cli/fastfetch/discussions/1030#discussioncomment-9845233
    if (ffGetTerminalResponse(
            "\eP+q6b697474792d71756572792d666f6e745f66616d696c79;6b697474792d71756572792d666f6e745f73697a65\e\\", // kitty-query-font_family;kitty-query-font_size
            2,
            "\eP1+r%*[^=]=%511[^\e]\e\\\eP1+r%*[^=]=%511[^\e]\e\\",
            fontHex,
            sizeHex) == NULL &&
        *fontHex && *sizeHex) {
        // decode hex string
        for (const char* p = fontHex; p[0] && p[1]; p += 2) {
            unsigned value;
            if (sscanf(p, "%2x", &value) == 1) {
                ffStrbufAppendC(&fontName, (char) value);
            }
        }
        for (const char* p = sizeHex; p[0] && p[1]; p += 2) {
            unsigned value;
            if (sscanf(p, "%2x", &value) == 1) {
                ffStrbufAppendC(&fontSize, (char) value);
            }
        }
    } else {
        FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
        if (!ffProcessAppendStdOut(&buf, (char* const[]){
                                             exe->chars,
                                             "+kitten",
                                             "query-terminal",
                                             NULL,
                                         })) {
            ffParsePropLines(buf.chars, "font_family: ", &fontName);
            ffParsePropLines(buf.chars, "font_size: ", &fontSize);
        } else {
            FFpropquery fontQuery[] = {
                { "font_family ", &fontName },
                { "font_size ", &fontSize },
            };

            ffParsePropFileConfigValues("kitty/kitty.conf", 2, fontQuery);

            if (fontName.length == 0) {
                ffStrbufSetS(&fontName, "monospace");
            }
            if (fontSize.length == 0) {
                ffStrbufSetS(&fontSize, "11.0");
            }
        }
    }

    ffFontInitValues(&result->font, fontName.chars, fontSize.chars);

    return true;
}

static bool detectWezterm(const FFstrbuf* exe, FFTerminalFontResult* result) {
    FF_STRBUF_AUTO_DESTROY cli = ffStrbufCreateCopy(exe);
    ffStrbufSubstrBeforeLastC(&cli, '-');

#ifdef _WIN32
    ffStrbufAppendS(&cli, ".exe");
#endif

    FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();

    ffStrbufSetS(&result->error, ffProcessAppendStdOut(&fontName, (char* const[]){ cli.chars, "ls-fonts", "--text", "a", NULL }));
    if (result->error.length) {
        return false;
    }

    // LeftToRight
    //  0 a    \u{61}       x_adv=7  cells=1  glyph=a,180  wezterm.font("JetBrains Mono", {weight="Regular", stretch="Normal", style="Normal"})
    //                                       <built-in>, BuiltIn
    ffStrbufSubstrAfterFirstC(&fontName, '"');
    ffStrbufSubstrBeforeFirstC(&fontName, '"');

    if (!fontName.length) {
        return false;
    }

    ffFontInitCopy(&result->font, fontName.chars);
    return true;
}

static bool detectTabby(FFTerminalFontResult* result) {
    FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();

    FFpropquery fontQuery[] = {
        { "font: ", &fontName },
        { "fontSize: ", &fontSize },
    };

    if (!ffParsePropFileConfigValues("tabby/config.yaml", 2, fontQuery)) {
        return false;
    }

    if (fontName.length == 0) {
        ffStrbufSetS(&fontName, "monospace");
    }
    if (fontSize.length == 0) {
        ffStrbufSetS(&fontSize, "14");
    }

    ffFontInitValues(&result->font, fontName.chars, fontSize.chars);

    return true;
}

static bool detectContour(const FFstrbuf* exe, FFTerminalFontResult* result) {
    FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
    if (ffProcessAppendStdOut(&buf, (char* const[]){ exe->chars, "font-locator", NULL })) {
        ffStrbufAppendS(&result->error, "`contour font-locator` failed");
        return false;
    }

    //[error] Missing key .logging.enabled. Using default: false.
    //[error] ...
    // Matching fonts using  : Fontconfig
    // Font description      : (family=Sarasa Term SC Nerd weight=Regular slant=Roman spacing=Monospace, strict_spacing=yes)
    // Number of fonts found : 49
    //  path /usr/share/fonts/google-noto/NotoSansMono-Regular.ttf Regular Roman
    //  path ...

    uint32_t index = ffStrbufFirstIndexS(&buf, "Font description      : (family=");
    if (index >= buf.length) {
        return false;
    }
    index += (uint32_t) strlen("Font description      : (family=");
    ffStrbufSubstrBefore(&buf, ffStrbufNextIndexS(&buf, index, " weight="));
    ffFontInitCopy(&result->font, buf.chars + index);
    return true;
}

static bool detectRio(FFTerminalFontResult* terminalFont) {
    FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
    FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();

    FFpropquery fontQueryToml[] = {
        { "family =", &fontName },
        { "size =", &fontSize },
    };

    ffParsePropFileConfigValues("rio/config.toml", 2, fontQueryToml);

    if (fontName.length == 0) {
        ffStrbufAppendS(&fontName, "Cascadia Code");
    }

    if (fontSize.length == 0) {
        ffStrbufAppendS(&fontSize, "18");
    }

    ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);

    return true;
}

bool ffDetectTerminalFontPlatform(const FFTerminalResult* terminal, FFTerminalFontResult* terminalFont);

static bool detectTerminalFontCommon(const FFTerminalResult* terminal, FFTerminalFontResult* terminalFont) {
    if (ffStrbufStartsWithIgnCaseS(&terminal->processName, "alacritty")) {
        detectAlacritty(terminalFont);
    } else if (ffStrbufStartsWithIgnCaseS(&terminal->processName, "wezterm-gui")) {
        detectWezterm(&terminal->exe, terminalFont);
    } else if (ffStrbufStartsWithIgnCaseS(&terminal->processName, "tabby")) {
        detectTabby(terminalFont);
    } else if (ffStrbufStartsWithIgnCaseS(&terminal->processName, "contour")) {
        detectContour(&terminal->exe, terminalFont);
    } else if (ffStrbufStartsWithIgnCaseS(&terminal->processName, "ghostty")) {
        detectGhostty(&terminal->exe, terminalFont, NULL, NULL);
    } else if (ffStrbufStartsWithIgnCaseS(&terminal->processName, "Muxy")) {
        detectGhostty(&terminal->exe, terminalFont, "Muxy/ghostty.conf", "muxy/ghostty.conf");
    } else if (ffStrbufStartsWithIgnCaseS(&terminal->processName, "rio")) {
        detectRio(terminalFont);
    }
#ifndef _WIN32
    else if (ffStrbufStartsWithIgnCaseS(&terminal->exe, "/dev/pts/")) {
        ffStrbufAppendS(&terminalFont->error, "Terminal font detection is not supported on PTS");
    } else if (ffStrbufIgnCaseEqualS(&terminal->processName, "kitty")) {
        detectKitty(&terminal->exe, terminalFont);
    } else if (ffStrbufStartsWithIgnCaseS(&terminal->exe, "/dev/tty")) {
        detectTTY(terminalFont);
    }
#endif

    else {
        return false;
    }

    return true;
}

bool ffDetectTerminalFont(FFTerminalFontResult* result) {
    const FFTerminalResult* terminal = ffDetectTerminal();

    if (terminal->processName.length == 0) {
        ffStrbufAppendS(&result->error, "Terminal font needs successful terminal detection");
    }

    else if (!detectTerminalFontCommon(terminal, result)) {
        ffDetectTerminalFontPlatform(terminal, result);
    }

    if (result->error.length == 0 && result->font.pretty.length == 0) {
        ffStrbufAppendF(&result->error, "Unknown terminal: %s", terminal->processName.chars);
    }

    return result->error.length == 0;
}