From ad7e23f64c2c857ca388ebe0bc6003e6ae201704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 28 Nov 2025 16:35:14 +0800 Subject: [PATCH] TerminalFont: supports the newest Alacritty config format Fixes #2070 --- src/common/font.c | 17 +++++++ src/common/font.h | 1 + src/detection/terminalfont/terminalfont.c | 57 ++++++++++++++--------- 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/common/font.c b/src/common/font.c index 13264f577..308e094c4 100644 --- a/src/common/font.c +++ b/src/common/font.c @@ -1,4 +1,6 @@ #include "fastfetch.h" +#include "util/FFlist.h" +#include "util/FFstrbuf.h" #include "common/font.h" #include @@ -192,6 +194,21 @@ void ffFontInitValues(FFfont* font, const char* name, const char* size) fontInitPretty(font); } +void ffFontInitMoveValues(FFfont* font, FFstrbuf* name, FFstrbuf* size, FFstrbuf* style) +{ + ffFontInit(font); + + if (name) ffStrbufInitMove(&font->name, name); + if (size) ffStrbufInitMove(&font->size, size); + if (style) + { + FFstrbuf* styleBuf = FF_LIST_ADD(FFstrbuf, font->styles); + ffStrbufInitMove(styleBuf, style); + } + + fontInitPretty(font); +} + void ffFontInitWithSpace(FFfont* font, const char* rawName) { const char* pspace = strrchr(rawName, ' '); diff --git a/src/common/font.h b/src/common/font.h index 4ba4cfbbd..2b94f463a 100644 --- a/src/common/font.h +++ b/src/common/font.h @@ -15,6 +15,7 @@ void ffFontInit(FFfont* font); void ffFontInitQt(FFfont* font, const char* data); void ffFontInitPango(FFfont* font, const char* data); void ffFontInitValues(FFfont* font, const char* name, const char* size); +void ffFontInitMoveValues(FFfont* font, FFstrbuf* name, FFstrbuf* size, FFstrbuf* style); void ffFontInitWithSpace(FFfont* font, const char* rawName); void ffFontDestroy(FFfont* font); diff --git a/src/detection/terminalfont/terminalfont.c b/src/detection/terminalfont/terminalfont.c index 19df07b02..5e0d6204f 100644 --- a/src/detection/terminalfont/terminalfont.c +++ b/src/detection/terminalfont/terminalfont.c @@ -8,13 +8,18 @@ static void detectAlacritty(FFTerminalFontResult* terminalFont) { - FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate(); + // 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 { - // Latest alacritty uses toml instead of yaml FFpropquery fontQueryToml[] = { - {"family =", &fontName}, + {"normal =", &fontNormal}, {"size =", &fontSize}, }; @@ -25,29 +30,39 @@ static void detectAlacritty(FFTerminalFontResult* terminalFont) break; if(ffParsePropFileConfigValues(".alacritty.toml", 2, fontQueryToml)) break; - - FFpropquery fontQueryYaml[] = { - {"family:", &fontName}, - {"size:", &fontSize}, - }; - - if(ffParsePropFileConfigValues("alacritty/alacritty.yml", 2, fontQueryYaml)) - break; - if(ffParsePropFileConfigValues("alacritty.yml", 2, fontQueryYaml)) - break; - if(ffParsePropFileConfigValues(".alacritty.yml", 2, fontQueryYaml)) - break; } while (false); - //by default alacritty uses its own font called alacritty - if(fontName.length == 0) - ffStrbufAppendS(&fontName, "alacritty"); + 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"); - // the default font size is 11 if(fontSize.length == 0) - ffStrbufAppendS(&fontSize, "11"); + ffStrbufSetStatic(&fontSize, "11.25"); - ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars); + ffFontInitMoveValues(&terminalFont->font, &fontFamily, &fontSize, &fontStyle); } static void detectGhostty(const FFstrbuf* exe, FFTerminalFontResult* terminalFont)