Skip to content

Commit de92296

Browse files
committed
TerminalFont: improve ghostty with command ghostty +show-config
Ref: #1972
1 parent 9d23d28 commit de92296

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

src/detection/terminalfont/terminalfont.c

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "common/processing.h"
55
#include "detection/terminalshell/terminalshell.h"
66
#include "util/debug.h"
7+
#include "util/stringUtils.h"
78

89
static void detectAlacritty(FFTerminalFontResult* terminalFont)
910
{
@@ -49,77 +50,67 @@ static void detectAlacritty(FFTerminalFontResult* terminalFont)
4950
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
5051
}
5152

52-
static bool parseGhosttyConfig(FFstrbuf* path, FFstrbuf* fontName, FFstrbuf* fontNameFallback, FFstrbuf* fontSize)
53+
static void detectGhostty(const FFstrbuf* exe, FFTerminalFontResult* terminalFont)
5354
{
54-
FF_DEBUG("parsing config: %s", path->chars);
55+
FF_DEBUG("detectGhostty: start");
56+
FF_STRBUF_AUTO_DESTROY configPath = ffStrbufCreate();
57+
FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
58+
FF_STRBUF_AUTO_DESTROY fontNameFallback = ffStrbufCreate();
59+
FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();
60+
61+
// Try ghostty +show-config first
5562
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
56-
if (!ffAppendFileBuffer(path->chars, &buffer)) {
57-
FF_DEBUG("cannot read config: %s", path->chars);
58-
return false;
63+
const char* error = ffProcessAppendStdOut(&buffer, (char* const[]){
64+
exe->chars,
65+
"+show-config",
66+
NULL,
67+
});
68+
if(error != NULL)
69+
{
70+
FF_DEBUG("`ghostty +show-config` failed: %s", error);
71+
return;
5972
}
6073

6174
char* line = NULL;
6275
size_t len = 0;
6376
while (ffStrbufGetline(&line, &len, &buffer))
6477
{
65-
if (!fontName->length)
78+
if (!fontName.length || !fontNameFallback.length)
6679
{
67-
if (ffParsePropLine(line, "font-family =", fontName)) {
68-
FF_DEBUG("found font-family='%s' in %s", fontName->chars, path->chars);
80+
if (ffStrStartsWith(line, "font-family = ")) {
81+
FF_DEBUG("found %s", line);
82+
ffStrbufSetNS(
83+
!fontName.length ? &fontName : &fontNameFallback,
84+
(uint32_t) (len - strlen("font-family = ")),
85+
line + strlen("font-family = "));
6986
continue;
7087
}
7188
}
72-
else if (!fontNameFallback->length)
89+
if (!fontSize.length)
7390
{
74-
if (ffParsePropLine(line, "font-family =", fontNameFallback)) {
75-
FF_DEBUG("found fallback font-family='%s' in %s", fontNameFallback->chars, path->chars);
91+
if (ffStrStartsWith(line, "font-size = ")) {
92+
FF_DEBUG("found fallback %s", line);
93+
ffStrbufSetNS(
94+
&fontSize,
95+
(uint32_t) (len - strlen("font-size = ")),
96+
line + strlen("font-size = "));
7697
continue;
7798
}
7899
}
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-
90-
static void detectGhostty(FFTerminalFontResult* terminalFont)
91-
{
92-
FF_DEBUG("detectGhostty: start");
93-
FF_STRBUF_AUTO_DESTROY configPath = ffStrbufCreate();
94-
FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
95-
FF_STRBUF_AUTO_DESTROY fontNameFallback = ffStrbufCreate();
96-
FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();
97-
98-
#if __APPLE__
99-
ffStrbufSet(&configPath, &instance.state.platform.homeDir);
100-
ffStrbufAppendS(&configPath, "Library/Application Support/com.mitchellh.ghostty/config");
101-
parseGhosttyConfig(&configPath, &fontName, &fontNameFallback, &fontSize);
102-
#endif
103-
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);
109100
}
110101

111-
if(fontName.length == 0) {
102+
if (fontName.length == 0) {
112103
ffStrbufAppendS(&fontName, "JetBrainsMono Nerd Font");
113104
FF_DEBUG("using default family='%s'", fontName.chars);
114105
}
115106

116-
if(fontSize.length == 0) {
107+
if (fontSize.length == 0) {
117108
ffStrbufAppendS(&fontSize, "13");
118109
FF_DEBUG("using default size='%s'", fontSize.chars);
119110
}
120111

121112
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
122-
if(fontNameFallback.length > 0) {
113+
if (fontNameFallback.length > 0) {
123114
FF_DEBUG("applying fallback family='%s'", fontNameFallback.chars);
124115
ffFontInitValues(&terminalFont->fallback, fontNameFallback.chars, NULL);
125116
}
@@ -333,7 +324,7 @@ static bool detectTerminalFontCommon(const FFTerminalResult* terminal, FFTermina
333324
else if(ffStrbufStartsWithIgnCaseS(&terminal->processName, "contour"))
334325
detectContour(&terminal->exe, terminalFont);
335326
else if(ffStrbufStartsWithIgnCaseS(&terminal->processName, "ghostty"))
336-
detectGhostty(terminalFont);
327+
detectGhostty(&terminal->exe, terminalFont);
337328
else if(ffStrbufStartsWithIgnCaseS(&terminal->processName, "rio"))
338329
detectRio(terminalFont);
339330

0 commit comments

Comments
 (0)