Skip to content

Commit e9065e1

Browse files
committed
TerminalFont: Improves Ghostty font config parsing with fallback font detection
Fixes #1967
1 parent 9e78160 commit e9065e1

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

src/detection/terminalfont/terminalfont.c

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "common/properties.h"
44
#include "common/processing.h"
55
#include "detection/terminalshell/terminalshell.h"
6+
#include "util/debug.h"
67

78
static void detectAlacritty(FFTerminalFontResult* terminalFont)
89
{
@@ -48,29 +49,82 @@ static void detectAlacritty(FFTerminalFontResult* terminalFont)
4849
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
4950
}
5051

52+
static bool parseGhosttyConfig(FFstrbuf* path, FFstrbuf* fontName, FFstrbuf* fontNameFallback, FFstrbuf* fontSize)
53+
{
54+
FF_DEBUG("parsing config: %s", path->chars);
55+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
56+
if (!ffAppendFileBuffer(path->chars, &buffer)) {
57+
FF_DEBUG("cannot read config: %s", path->chars);
58+
return false;
59+
}
60+
61+
char* line = NULL;
62+
size_t len = 0;
63+
while (ffStrbufGetline(&line, &len, &buffer))
64+
{
65+
if (!fontName->length)
66+
{
67+
if (ffParsePropLine(line, "font-family =", fontName)) {
68+
FF_DEBUG("found font-family='%s' in %s", fontName->chars, path->chars);
69+
continue;
70+
}
71+
}
72+
else if (!fontNameFallback->length)
73+
{
74+
if (ffParsePropLine(line, "font-family =", fontNameFallback)) {
75+
FF_DEBUG("found fallback font-family='%s' in %s", fontNameFallback->chars, path->chars);
76+
continue;
77+
}
78+
}
79+
if (!fontSize->length)
80+
{
81+
if (ffParsePropLine(line, "font-size =", fontSize)) {
82+
FF_DEBUG("found font-size='%s' in %s", fontSize->chars, path->chars);
83+
continue;
84+
}
85+
}
86+
}
87+
return true;
88+
}
89+
5190
static void detectGhostty(FFTerminalFontResult* terminalFont)
5291
{
92+
FF_DEBUG("detectGhostty: start");
93+
FF_STRBUF_AUTO_DESTROY configPath = ffStrbufCreate();
5394
FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
95+
FF_STRBUF_AUTO_DESTROY fontNameFallback = ffStrbufCreate();
5496
FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();
5597

56-
FFpropquery fontQueryToml[] = {
57-
{"font-family =", &fontName},
58-
{"font-size =", &fontSize},
59-
};
60-
6198
#if __APPLE__
62-
ffParsePropFileConfigValues("com.mitchellh.ghostty/config", 2, fontQueryToml);
99+
ffStrbufSet(&configPath, &instance.state.platform.homeDir);
100+
ffStrbufAppendS(&configPath, "Library/Application Support/com.mitchellh.ghostty/config");
101+
parseGhosttyConfig(&configPath, &fontName, &fontNameFallback, &fontSize);
63102
#endif
64103

65-
ffParsePropFileConfigValues("ghostty/config", 2, fontQueryToml);
104+
if (instance.state.platform.configDirs.length > 0)
105+
{
106+
ffStrbufSet(&configPath, FF_LIST_GET(FFstrbuf, instance.state.platform.configDirs, 0));
107+
ffStrbufAppendS(&configPath, "ghostty/config");
108+
parseGhosttyConfig(&configPath, &fontName, &fontNameFallback, &fontSize);
109+
}
66110

67-
if(fontName.length == 0)
111+
if(fontName.length == 0) {
68112
ffStrbufAppendS(&fontName, "JetBrainsMono Nerd Font");
113+
FF_DEBUG("using default family='%s'", fontName.chars);
114+
}
69115

70-
if(fontSize.length == 0)
116+
if(fontSize.length == 0) {
71117
ffStrbufAppendS(&fontSize, "13");
118+
FF_DEBUG("using default size='%s'", fontSize.chars);
119+
}
72120

73121
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
122+
if(fontNameFallback.length > 0) {
123+
FF_DEBUG("applying fallback family='%s'", fontNameFallback.chars);
124+
ffFontInitValues(&terminalFont->fallback, fontNameFallback.chars, NULL);
125+
}
126+
FF_DEBUG("result family='%s' size='%s'%s", fontName.chars, fontSize.chars, fontNameFallback.length ? " (with fallback)" : "");
127+
FF_DEBUG("detectGhostty: end");
74128
}
75129

76130
FF_MAYBE_UNUSED static void detectTTY(FFTerminalFontResult* terminalFont)

0 commit comments

Comments
 (0)