mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
TerminalFont: Improves Ghostty font config parsing with fallback font detection
Fixes #1967
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "common/properties.h"
|
||||
#include "common/processing.h"
|
||||
#include "detection/terminalshell/terminalshell.h"
|
||||
#include "util/debug.h"
|
||||
|
||||
static void detectAlacritty(FFTerminalFontResult* terminalFont)
|
||||
{
|
||||
@@ -48,29 +49,82 @@ static void detectAlacritty(FFTerminalFontResult* terminalFont)
|
||||
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
|
||||
}
|
||||
|
||||
static bool parseGhosttyConfig(FFstrbuf* path, FFstrbuf* fontName, FFstrbuf* fontNameFallback, FFstrbuf* fontSize)
|
||||
{
|
||||
FF_DEBUG("parsing config: %s", path->chars);
|
||||
FF_STRBUF_AUTO_DESTROY buffer = 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 (!fontName->length)
|
||||
{
|
||||
if (ffParsePropLine(line, "font-family =", fontName)) {
|
||||
FF_DEBUG("found font-family='%s' in %s", fontName->chars, path->chars);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (!fontNameFallback->length)
|
||||
{
|
||||
if (ffParsePropLine(line, "font-family =", fontNameFallback)) {
|
||||
FF_DEBUG("found fallback font-family='%s' in %s", fontNameFallback->chars, path->chars);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!fontSize->length)
|
||||
{
|
||||
if (ffParsePropLine(line, "font-size =", fontSize)) {
|
||||
FF_DEBUG("found font-size='%s' in %s", fontSize->chars, path->chars);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void detectGhostty(FFTerminalFontResult* terminalFont)
|
||||
{
|
||||
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();
|
||||
|
||||
FFpropquery fontQueryToml[] = {
|
||||
{"font-family =", &fontName},
|
||||
{"font-size =", &fontSize},
|
||||
};
|
||||
|
||||
#if __APPLE__
|
||||
ffParsePropFileConfigValues("com.mitchellh.ghostty/config", 2, fontQueryToml);
|
||||
ffStrbufSet(&configPath, &instance.state.platform.homeDir);
|
||||
ffStrbufAppendS(&configPath, "Library/Application Support/com.mitchellh.ghostty/config");
|
||||
parseGhosttyConfig(&configPath, &fontName, &fontNameFallback, &fontSize);
|
||||
#endif
|
||||
|
||||
ffParsePropFileConfigValues("ghostty/config", 2, fontQueryToml);
|
||||
if (instance.state.platform.configDirs.length > 0)
|
||||
{
|
||||
ffStrbufSet(&configPath, FF_LIST_GET(FFstrbuf, instance.state.platform.configDirs, 0));
|
||||
ffStrbufAppendS(&configPath, "ghostty/config");
|
||||
parseGhosttyConfig(&configPath, &fontName, &fontNameFallback, &fontSize);
|
||||
}
|
||||
|
||||
if(fontName.length == 0)
|
||||
if(fontName.length == 0) {
|
||||
ffStrbufAppendS(&fontName, "JetBrainsMono Nerd Font");
|
||||
FF_DEBUG("using default family='%s'", fontName.chars);
|
||||
}
|
||||
|
||||
if(fontSize.length == 0)
|
||||
if(fontSize.length == 0) {
|
||||
ffStrbufAppendS(&fontSize, "13");
|
||||
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_MAYBE_UNUSED static void detectTTY(FFTerminalFontResult* terminalFont)
|
||||
|
||||
Reference in New Issue
Block a user