Refactor font output

This commit is contained in:
Linus Dierheimer 2022-09-29 14:15:11 +02:00
parent 32d3b265bb
commit e6e189497d
No known key found for this signature in database
GPG Key ID: 74FA57726CDD7B61
18 changed files with 147 additions and 145 deletions

View File

@ -200,6 +200,7 @@ set(LIBFASTFETCH_SRC
src/detection/cpu/cpu.c
src/detection/gpu/gpu.c
src/detection/memory/memory.c
src/detection/font/font.c
src/detection/displayserver/displayserver.c
src/detection/terminalfont/terminalfont.c
src/detection/media/media.c

View File

@ -180,7 +180,6 @@ __fastfetch_completion()
"--cpu-temp"
"--gpu-temp"
"--battery-temp"
"--font-inline"
"--localip-show-ipv4"
"--localip-show-ipv6"
"--localip-show-loop"

View File

@ -8,5 +8,4 @@
--icons-key I
--icons-format {5}
--font-key F
--font-format {21}
--cursor-key C

View File

@ -11,7 +11,7 @@
--wm-theme-format Name: {}
--theme-format Plasma: {}; Plasma colors: {}; Plasma colors pretty: {}; GTK2: {}; GTK3: {}; GTK4: {}; GTK: {}
--icons-format Plasma: {}; GTK2: {}; GTK3: {}; GTK4: {}; GTK: {}
--font-format Type: {}; Font: {}
--font-format Font1: {}; Font2: {}; Font3: {}; Font4: {}
--cursor-format Theme: {}; Size: {}
--terminal-format Process: {}; Path: {}; Exe: {}
--terminal-font-format Pretty: {}; Name: {}; Size: {}; Styles: {}

View File

@ -236,8 +236,6 @@ static void defaultConfig(FFinstance* instance)
ffStrbufInitA(&instance->config.separatorString, 0);
instance->config.fontInline = true;
instance->config.localIpShowIpV4 = true;
instance->config.localIpShowIpV6 = false;
instance->config.localIpShowLoop = false;

View File

@ -97,7 +97,6 @@ Module specific options:
--cpu-temp <?value>: Detect and display CPU temperature if supported. Default is false
--gpu-temp <?value>: Detect and display GPU temperature if supported. Default is false
--battery-temp <?value>: Detect and display Battery temperature if supported. Default is false
--font-inline <?value>: Display fonts of different DE in one line
--localip-show-ipv4 <?value>: Show ipv4 addresses in local ip module. Default is true
--localip-show-ipv6 <?value>: Show ipv6 addresses in local ip module. Default is false
--localip-show-loop <?value>: Show loop back addresses (127.0.0.1) in local ip module. Default is false

32
src/detection/font/font.c Normal file
View File

@ -0,0 +1,32 @@
#include "font.h"
#include "detection/internal.h"
void ffDetectFontImpl(const FFinstance* instance, FFFontResult* font);
static void detectFont(const FFinstance* instance, FFFontResult* font)
{
ffStrbufInit(&font->error);
for(uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i)
ffStrbufInit(&font->fonts[i]);
ffDetectFontImpl(instance, font);
if(font->error.length > 0)
return;
for(uint32_t i = 0; i < FF_DETECT_FONT_NUM_FONTS; ++i)
{
if(font->fonts[i].length > 0)
return;
}
ffStrbufAppendS(&font->error, "No fonts found");
}
const FFFontResult* ffDetectFont(const FFinstance* instance)
{
FF_DETECTION_INTERNAL_GUARD(FFFontResult,
detectFont(instance, &result);
);
}

View File

@ -4,15 +4,21 @@
#define FF_INCLUDED_detection_font_font
#include "fastfetch.h"
#include "util/FFstrbuf.h"
#include "util/FFlist.h"
#define FF_DETECT_FONT_NUM_FONTS 4
typedef struct FFFontResult
{
const char* type;
FFstrbuf fontPretty;
FFstrbuf error;
/**
* Linux / BSD: QT, GTK2, GTK3, GTK4
* MacOS: System, User, Unset, Unset
* Other: Unset, Unset, Unset, Unset
*/
FFstrbuf fonts[FF_DETECT_FONT_NUM_FONTS];
} FFFontResult;
const char* ffDetectFontImpl(FFinstance* instance, FFlist* result);
const FFFontResult* ffDetectFont(const FFinstance* instance);
#endif

View File

@ -1,8 +1,7 @@
#include "fastfetch.h"
#include "common/font.h"
const char* ffDetectFontImpl(FFinstance* instance, FFlist* result)
void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result)
{
FF_UNUSED(instance, result);
return "Font detection is not supported on Android";
FF_UNUSED(instance);
ffStrbufAppendS(&result->error, "Not implemented");
}

View File

@ -4,38 +4,27 @@
#import <AppKit/AppKit.h>
#define FF_FONT_MODULE_NAME "Font"
static void detectFontForType(CTFontUIFontType uiType, FFFontResult* result)
static void detectFontForType(CTFontUIFontType uiType, FFstrbuf* font)
{
CTFontRef ctFont = CTFontCreateUIFontForLanguage(uiType, 12, NULL);
ffCfStrGetString(CTFontCopyFullName(ctFont), font);
ffStrbufInit(&result->fontPretty);
ffCfStrGetString(CTFontCopyFullName(ctFont), &result->fontPretty);
if(font->length == 0)
return;
FFstrbuf familyName;
ffStrbufInit(&familyName);
ffCfStrGetString(CTFontCopyFamilyName(ctFont), &familyName);
if (ffStrbufComp(&familyName, &result->fontPretty) != 0)
{
ffStrbufAppendF(&result->fontPretty, " (%*s)", familyName.length, familyName.chars);
}
if (ffStrbufComp(&familyName, font) != 0)
ffStrbufAppendF(font, " (%*s)", familyName.length, familyName.chars);
ffStrbufDestroy(&familyName);
}
const char* ffDetectFontImpl(FFinstance* instance, FFlist* result)
void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result)
{
FF_UNUSED(instance);
FFFontResult* resultSystem = (FFFontResult*)ffListAdd(result);
resultSystem->type = "SYS";
detectFontForType(kCTFontUIFontSystem, resultSystem);
FFFontResult* resultUser = (FFFontResult*)ffListAdd(result);
resultUser->type = "USER";
detectFontForType(kCTFontUIFontUser, resultUser);
return NULL;
detectFontForType(kCTFontUIFontSystem, &result->fonts[0]);
detectFontForType(kCTFontUIFontUser, &result->fonts[1]);
}

View File

@ -1,51 +1,27 @@
#include "detection/qt.h"
#include "detection/gtk.h"
#include "common/parsing.h"
#include "common/font.h"
#include "font.h"
const char* ffDetectFontImpl(FFinstance* instance, FFlist* result)
void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result)
{
const FFstrbuf* qtRaw = &ffDetectQt(instance)->font;
const FFstrbuf* gtk2Raw = &ffDetectGTK2(instance)->font;
const FFstrbuf* gtk3Raw = &ffDetectGTK3(instance)->font;
const FFstrbuf* gtk4Raw = &ffDetectGTK4(instance)->font;
if(qtRaw->length > 0)
{
FFfont qt;
ffFontInitQt(&qt, qtRaw->chars);
FFFontResult* resultQt = (FFFontResult *)ffListAdd(result);
resultQt->type = "QT";
ffStrbufInitCopy(&resultQt->fontPretty, &qt.pretty);
ffFontInitQt(&qt, ffDetectQt(instance)->font.chars);
ffStrbufAppend(&result->fonts[0], &qt.pretty);
ffFontDestroy(&qt);
}
if(gtk2Raw->length > 0 || gtk3Raw->length > 0 || gtk4Raw->length > 0)
{
FFfont gtk2;
ffFontInitPango(&gtk2, gtk2Raw->chars);
ffFontInitPango(&gtk2, ffDetectGTK2(instance)->font.chars);
ffStrbufAppend(&result->fonts[1], &gtk2.pretty);
ffFontDestroy(&gtk2);
FFfont gtk3;
ffFontInitPango(&gtk3, gtk3Raw->chars);
ffFontInitPango(&gtk3, ffDetectGTK3(instance)->font.chars);
ffStrbufAppend(&result->fonts[2], &gtk3.pretty);
ffFontDestroy(&gtk3);
FFfont gtk4;
ffFontInitPango(&gtk4, gtk4Raw->chars);
FFFontResult* resultGtk = (FFFontResult *)ffListAdd(result);
resultGtk->type = "GTK";
ffStrbufInit(&resultGtk->fontPretty);
ffParseGTK(&resultGtk->fontPretty, &gtk2.pretty, &gtk3.pretty, &gtk4.pretty);
ffFontDestroy(&gtk2);
ffFontDestroy(&gtk3);
ffFontInitPango(&gtk4, ffDetectGTK4(instance)->font.chars);
ffStrbufAppend(&result->fonts[3], &gtk4.pretty);
ffFontDestroy(&gtk4);
}
if(result->length == 0)
return "No fonts found";
return NULL;
}

View File

@ -32,7 +32,7 @@ static inline void applyGTKSettings(FFGTKResult* result, const char* themeName,
ffStrbufAppendF(&result->cursorSize, "%i", cursorSize);
}
static void detectGTKFromSettings(FFinstance* instance, FFGTKResult* result)
static void detectGTKFromSettings(const FFinstance* instance, FFGTKResult* result)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
@ -144,7 +144,7 @@ static void detectGTKFromConfigDir(FFstrbuf* configDir, const char* version, FFG
ffStrbufSubstrBefore(configDir, configDirLength);
}
static void detectGTK(FFinstance* instance, const char* version, FFGTKResult* result)
static void detectGTK(const FFinstance* instance, const char* version, FFGTKResult* result)
{
//Mate, Cinnamon and Gnome use dconf to save theme config
//On other DEs, this will do nothing
@ -186,17 +186,17 @@ static void detectGTK(FFinstance* instance, const char* version, FFGTKResult* re
pthread_mutex_unlock(&mutex); \
return &result;
const FFGTKResult* ffDetectGTK2(FFinstance* instance)
const FFGTKResult* ffDetectGTK2(const FFinstance* instance)
{
FF_DETECT_GTK_IMPL(2)
}
const FFGTKResult* ffDetectGTK3(FFinstance* instance)
const FFGTKResult* ffDetectGTK3(const FFinstance* instance)
{
FF_DETECT_GTK_IMPL(3)
}
const FFGTKResult* ffDetectGTK4(FFinstance* instance)
const FFGTKResult* ffDetectGTK4(const FFinstance* instance)
{
FF_DETECT_GTK_IMPL(4)
}

View File

@ -14,8 +14,8 @@ typedef struct FFGTKResult
FFstrbuf cursorSize;
} FFGTKResult;
const FFGTKResult* ffDetectGTK2(FFinstance* instance);
const FFGTKResult* ffDetectGTK4(FFinstance* instance);
const FFGTKResult* ffDetectGTK3(FFinstance* instance);
const FFGTKResult* ffDetectGTK2(const FFinstance* instance);
const FFGTKResult* ffDetectGTK4(const FFinstance* instance);
const FFGTKResult* ffDetectGTK3(const FFinstance* instance);
#endif

View File

@ -131,7 +131,7 @@ static void detectLXQt(const FFinstance* instance, FFQtResult* result)
});
}
const FFQtResult* ffDetectQt(FFinstance* instance)
const FFQtResult* ffDetectQt(const FFinstance* instance)
{
static FFQtResult result;

View File

@ -13,6 +13,6 @@ typedef struct FFQtResult
FFstrbuf font;
} FFQtResult;
const FFQtResult* ffDetectQt(FFinstance* instance);
const FFQtResult* ffDetectQt(const FFinstance* instance);
#endif

View File

@ -199,28 +199,11 @@ static inline void printCommandHelp(const char* command)
}
else if(strcasecmp(command, "font-format") == 0)
{
constructAndPrintCommandHelpFormat("font", "{5} [Plasma], {21}", 21,
"Plasma raw",
"Plasma name",
"Plasma size",
"Plasma styles",
"Plasma pretty",
"GTK2 raw",
"GTK2 name",
"GTK2 size",
"GTK2 styles",
"GTK2 pretty",
"GTK3 raw",
"GTK3 name",
"GTK3 size",
"GTK3 styles",
"GTK3 pretty",
"GTK4 raw",
"GTK4 name",
"GTK4 size",
"GTK4 styles",
"GTK4 pretty",
"GTK2/3/4 pretty"
constructAndPrintCommandHelpFormat("font", "{} [QT], {} [GTK2], {} [GTK3], {} [GTK4]", 4,
"Font 1",
"Font 2",
"Font 3",
"Font 4"
);
}
else if(strcasecmp(command, "cursor-format") == 0)
@ -1283,8 +1266,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
optionParseString(key, value, &instance->config.batteryDir);
else if(strcasecmp(key, "--separator-string") == 0)
optionParseString(key, value, &instance->config.separatorString);
else if(strcasecmp(key, "--font-inline") == 0)
instance->config.fontInline = optionParseBoolean(value);
else if(strcasecmp(key, "--localip-show-ipv4") == 0)
instance->config.localIpShowIpV4 = optionParseBoolean(value);
else if(strcasecmp(key, "--localip-show-ipv6") == 0)

View File

@ -168,8 +168,6 @@ typedef struct FFconfig
FFstrbuf separatorString;
bool fontInline;
bool localIpShowLoop;
bool localIpShowIpV4;
bool localIpShowIpV6;

View File

@ -4,10 +4,55 @@
#include "detection/displayserver/displayserver.h"
#define FF_FONT_MODULE_NAME "Font"
#define FF_FONT_NUM_FORMAT_ARGS 2
#define FF_FONT_NUM_FORMAT_ARGS 4
#if defined(__linux__) || defined(__FreeBSD__)
#include "common/parsing.h"
static void printFont(const FFFontResult* font)
{
FFstrbuf gtk;
ffStrbufInit(&gtk);
ffParseGTK(&gtk, &font->fonts[1], &font->fonts[2], &font->fonts[3]);
if(font->fonts[0].length > 0)
{
printf("%s [QT]", font->fonts[0].chars);
if(gtk.length > 0)
fputs(", ", stdout);
}
ffStrbufWriteTo(&gtk, stdout);
}
#elif defined(__APPLE__)
static void printFont(const FFFontResult* font)
{
if(font->fonts[0].length > 0)
{
printf("%s [System]", font->fonts[0].chars);
if(font->fonts[1].length > 0)
fputs(", ", stdout);
}
if(font->fonts[1].length > 0)
printf("%s [User]", font->fonts[1].chars);
}
#else
static void printFont(const FFFontResult* font)
{
FF_UNUSED(font);
}
#endif
void ffPrintFont(FFinstance* instance)
{
assert(FF_DETECT_FONT_NUM_FONTS == FF_FONT_NUM_FORMAT_ARGS);
const FFDisplayServerResult* wmde = ffConnectDisplayServer(instance);
if(ffStrbufIgnCaseCompS(&wmde->wmProtocolName, "TTY") == 0)
@ -16,47 +61,27 @@ void ffPrintFont(FFinstance* instance)
return;
}
FFlist list;
ffListInit(&list, sizeof(FFFontResult));
const char* error = ffDetectFontImpl(instance, &list);
if(error)
const FFFontResult* font = ffDetectFont(instance);
if(font->error.length > 0)
{
ffPrintError(instance, FF_FONT_MODULE_NAME, 0, &instance->config.font, "%s", error);
ffPrintError(instance, FF_FONT_MODULE_NAME, 0, &instance->config.font, "%s", font->error.chars);
return;
}
if (instance->config.font.outputFormat.length == 0 && instance->config.fontInline)
if(instance->config.font.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_FONT_MODULE_NAME, 0, &instance->config.font.key);
for(uint32_t i = 0; i < list.length; ++i)
{
FFFontResult* font = (FFFontResult*)ffListGet(&list, i);
printf("[%s] %*s%s", font->type, font->fontPretty.length, font->fontPretty.chars, i < list.length - 1 ? ", " : "");
ffStrbufDestroy(&font->fontPretty);
}
printFont(font);
putchar('\n');
}
else
{
for(uint32_t i = 0; i < list.length; ++i)
{
FFFontResult* font = (FFFontResult*)ffListGet(&list, i);
if(instance->config.font.outputFormat.length == 0)
{
char keyChars[32];
snprintf(keyChars, sizeof(keyChars), "%s (%s)", FF_FONT_MODULE_NAME, font->type);
ffPrintLogoAndKey(instance, keyChars, 0, &instance->config.font.key);
puts(font->fontPretty.chars);
}
else
{
ffPrintFormat(instance, FF_FONT_MODULE_NAME, 0, &instance->config.font, FF_FONT_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRING, font->type},
{FF_FORMAT_ARG_TYPE_STRBUF, &font->fontPretty}
ffPrintFormat(instance, FF_FONT_MODULE_NAME, 0, &instance->config.font, FF_FONT_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &font->fonts[0]},
{FF_FORMAT_ARG_TYPE_STRBUF, &font->fonts[1]},
{FF_FORMAT_ARG_TYPE_STRBUF, &font->fonts[2]},
{FF_FORMAT_ARG_TYPE_STRBUF, &font->fonts[3]}
});
}
ffStrbufDestroy(&font->fontPretty);
}
}
ffListDestroy(&list);
}