Repository: fastfetch-cli/fastfetch
File: src/modules/terminalfont.c (C)

Code before:
    FFstrbuf matchText;
    ffStrbufInitAS(&matchText, 64, "<property name=\"MonospaceFontName\" type=\"string\" value=\"");

    char* line = NULL;
    size_t len = 0;

    while(getline(&line, &len, file) != -1)
    {
        ffStrbufAppendS(&fontName, line);
        ffStrbufTrimLeft(&fontName, ' ');

        if(ffStrbufStartsWithS(&fontName, matchText.chars))
        {
            ffStrbufSubstrAfter(&fontName, matchText.length -1);
            ffStrbufSubstrBefore(&fontName, fontName.length - 4); // ["/>\n]
            break;
        }
        ffStrbufClear(&fontName);
    }
    ffStrbufDestroy(&matchText);
    ffStrbufDestroy(&absolutePath);

    if(line != NULL)
        free(line);

    fclose(file);

    if(fontName.chars[0] != '\0')
        printTerminalFont(instance, fontName.chars);
    else
        ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Couldn't find \"MonospaceFontName=%%[^\\n]\" in \"xsettings.xml\"");

    ffStrbufDestroy(&fontName);
}

static void printKonsole(FFinstance* instance)
{
    char profile[128];
    ffParsePropFileConfig(instance, "konsolerc", "DefaultProfile=%[^\n]", profile);
    if(profile[0] == '\0')
    {
        ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Couldn't find \"DefaultProfile=%[^\n]\" in \".config/konsolerc\"");
        return;
    }

    char profilePath[256];
    sprintf(profilePath, ".local/share/konsole/%s", profile);

    char font[128];
    ffParsePropFileHome(instance, profilePath, "Font=%[^\n]", font);
    if(font[0] == '\0')

Review comment (suggestion):
I think this whole section can be replaced by a `ffParsePropFileConfig`, see my implementation in WM theme.

Code after:

    if(!defaultProfile)
    {
        ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Couldn't find \"Default\" profile in Tilix settings");
        return;
    }

    FFstrbuf key;
    ffStrbufInitAS(&key, 64, "/com/gexperts/Tilix/profiles/");
    ffStrbufAppendS(&key, defaultProfile);
    uint32_t keyLen = key.length;
    ffStrbufAppendS(&key, "/use-system-font");

    FFvariant res = ffSettingsGetDConf(instance,key.chars, FF_VARIANT_TYPE_BOOL);

    if(res.boolValueSet && !res.boolValue) // custom font
    {
        ffStrbufSubstrBefore(&key, keyLen);
        ffStrbufAppendS(&key, "/font");
        fontName = ffSettingsGetDConf(instance, key.chars, FF_VARIANT_TYPE_STRING).strValue;
    }
    else if(!res.boolValueSet || res.boolValue) // system font
        fontName = ffSettingsGetDConf(instance, "/org/gnome/desktop/interface/monospace-font-name", FF_VARIANT_TYPE_STRING).strValue;

    if(!fontName)
    {
        ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Couldn't find \"Terminal font\" in Tilix settings");
        ffStrbufDestroy(&key);
        return;
    }

    printTerminalFont(instance, fontName);
    ffStrbufDestroy(&key);
}

static void printXCFETerminal(FFinstance* instance)
{
    char useSysFont[6];
    const char* fontName;

    if(!ffParsePropFileConfig(instance, "xfce4/terminal/terminalrc", "FontUseSystem=%[^\n]", useSysFont))
    {
        ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Couldn't open \"$XDG_CONFIG_HOME/xfce4/terminal/terminalrc\"");
        return;
    }

    if((useSysFont[0] == '\0') || strcasecmp(useSysFont, "FALSE") == 0)
    {
        printTerminalFontFromConfigFile(instance, "xfce4/terminal/terminalrc", "FontName=%[^\n]");
        return;
    }