Skip to content

Commit ad7e23f

Browse files
committed
TerminalFont: supports the newest Alacritty config format
Fixes #2070
1 parent 2738f20 commit ad7e23f

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/common/font.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "fastfetch.h"
2+
#include "util/FFlist.h"
3+
#include "util/FFstrbuf.h"
24
#include "common/font.h"
35

46
#include <string.h>
@@ -192,6 +194,21 @@ void ffFontInitValues(FFfont* font, const char* name, const char* size)
192194
fontInitPretty(font);
193195
}
194196

197+
void ffFontInitMoveValues(FFfont* font, FFstrbuf* name, FFstrbuf* size, FFstrbuf* style)
198+
{
199+
ffFontInit(font);
200+
201+
if (name) ffStrbufInitMove(&font->name, name);
202+
if (size) ffStrbufInitMove(&font->size, size);
203+
if (style)
204+
{
205+
FFstrbuf* styleBuf = FF_LIST_ADD(FFstrbuf, font->styles);
206+
ffStrbufInitMove(styleBuf, style);
207+
}
208+
209+
fontInitPretty(font);
210+
}
211+
195212
void ffFontInitWithSpace(FFfont* font, const char* rawName)
196213
{
197214
const char* pspace = strrchr(rawName, ' ');

src/common/font.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void ffFontInit(FFfont* font);
1515
void ffFontInitQt(FFfont* font, const char* data);
1616
void ffFontInitPango(FFfont* font, const char* data);
1717
void ffFontInitValues(FFfont* font, const char* name, const char* size);
18+
void ffFontInitMoveValues(FFfont* font, FFstrbuf* name, FFstrbuf* size, FFstrbuf* style);
1819
void ffFontInitWithSpace(FFfont* font, const char* rawName);
1920
void ffFontDestroy(FFfont* font);
2021

src/detection/terminalfont/terminalfont.c

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88

99
static void detectAlacritty(FFTerminalFontResult* terminalFont)
1010
{
11-
FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
11+
// Maybe using a toml parser to read the config file is better?
12+
// https://github.com/cktan/tomlc17
13+
14+
// Doc: https://alacritty.org/config-alacritty.html#s26
15+
FF_STRBUF_AUTO_DESTROY fontNormal = ffStrbufCreate();
16+
FF_STRBUF_AUTO_DESTROY fontFamily = ffStrbufCreate();
17+
FF_STRBUF_AUTO_DESTROY fontStyle = ffStrbufCreate();
1218
FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();
1319

1420
do {
15-
// Latest alacritty uses toml instead of yaml
1621
FFpropquery fontQueryToml[] = {
17-
{"family =", &fontName},
22+
{"normal =", &fontNormal},
1823
{"size =", &fontSize},
1924
};
2025

@@ -25,29 +30,39 @@ static void detectAlacritty(FFTerminalFontResult* terminalFont)
2530
break;
2631
if(ffParsePropFileConfigValues(".alacritty.toml", 2, fontQueryToml))
2732
break;
28-
29-
FFpropquery fontQueryYaml[] = {
30-
{"family:", &fontName},
31-
{"size:", &fontSize},
32-
};
33-
34-
if(ffParsePropFileConfigValues("alacritty/alacritty.yml", 2, fontQueryYaml))
35-
break;
36-
if(ffParsePropFileConfigValues("alacritty.yml", 2, fontQueryYaml))
37-
break;
38-
if(ffParsePropFileConfigValues(".alacritty.yml", 2, fontQueryYaml))
39-
break;
4033
} while (false);
4134

42-
//by default alacritty uses its own font called alacritty
43-
if(fontName.length == 0)
44-
ffStrbufAppendS(&fontName, "alacritty");
35+
if(fontNormal.length > 0)
36+
{
37+
// { family = "Fira Code", style = "Medium" }
38+
ffStrbufTrimSpace(&fontNormal);
39+
ffStrbufTrimRight(&fontNormal, '}');
40+
ffStrbufTrimLeft(&fontNormal, '{');
41+
ffStrbufTrimSpace(&fontNormal);
42+
43+
// family = "Fira Code", style = "Medium"
44+
ffStrbufReplaceAllC(&fontNormal, ',', '\n'); // Assume no commas in font names
45+
ffParsePropLines(fontNormal.chars, "family =", &fontFamily);
46+
ffParsePropLines(fontNormal.chars, "style =", &fontStyle);
47+
}
48+
49+
if (fontFamily.length == 0)
50+
{
51+
#if __APPLE__
52+
ffStrbufSetStatic(&fontFamily, "Menlo");
53+
#elif _WIN32
54+
ffStrbufSetStatic(&fontFamily, "Consolas");
55+
#else
56+
ffStrbufSetStatic(&fontFamily, "monospace");
57+
#endif
58+
}
59+
if (fontStyle.length == 0)
60+
ffStrbufSetStatic(&fontStyle, "Regular");
4561

46-
// the default font size is 11
4762
if(fontSize.length == 0)
48-
ffStrbufAppendS(&fontSize, "11");
63+
ffStrbufSetStatic(&fontSize, "11.25");
4964

50-
ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars);
65+
ffFontInitMoveValues(&terminalFont->font, &fontFamily, &fontSize, &fontStyle);
5166
}
5267

5368
static void detectGhostty(const FFstrbuf* exe, FFTerminalFontResult* terminalFont)

0 commit comments

Comments
 (0)