Skip to content

Commit daac1b7

Browse files
Refactored logo choosing logic
1 parent 2e98dd7 commit daac1b7

File tree

14 files changed

+281
-313
lines changed

14 files changed

+281
-313
lines changed

completions/bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ __fastfetch_completion()
131131
"--localip-show-ipv6"
132132
"--localip-show-loop"
133133
"--escape-bedrock"
134+
"--pipe"
134135
)
135136

136137
local FF_OPTIONS_STRING=(

src/common/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ void ffStart(FFinstance* instance)
286286
if(ffDisableLinewrap)
287287
fputs("\033[?7l", stdout);
288288

289-
ffPrintLogo(instance);
289+
ffLogoPrint(instance);
290290
}
291291

292292
void ffFinish(FFinstance* instance)
293293
{
294294
if(instance->config.logoPrintRemaining)
295-
ffPrintRemainingLogo(instance);
295+
ffLogoPrintRemaining(instance);
296296

297297
resetConsole();
298298
}

src/common/printing.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
void ffPrintLogoAndKey(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat)
44
{
5-
ffPrintLogoLine(instance);
5+
ffLogoPrintLine(instance);
66

77
if(!instance->config.pipe)
88
{
99
fputs(FASTFETCH_TEXT_MODIFIER_RESET FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
10-
11-
//If the main color is not set (e.g. none logo), this would reset in \033[m, which resets everything,
12-
//including the wanted bolt from above. So we only print it, if the main color is at least one char.
13-
if(instance->config.mainColor.length > 0)
14-
ffPrintColor(&instance->config.mainColor);
10+
ffPrintColor(&instance->config.mainColor);
1511
}
1612

1713
if(customKeyFormat == NULL || customKeyFormat->length == 0)
@@ -91,6 +87,11 @@ void ffPrintFormatString(FFinstance* instance, const char* moduleName, uint8_t m
9187

9288
void ffPrintColor(const FFstrbuf* colorValue)
9389
{
90+
//If the color is not set, this would reset in \033[m, which resets everything.
91+
//So we only print it, if the main color is at least one char.
92+
if(colorValue->length == 0)
93+
return;
94+
9495
fputs("\033[", stdout);
9596
ffStrbufWriteTo(colorValue, stdout);
9697
fputc('m', stdout);

src/fastfetch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,17 +700,17 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
700700
}
701701
else if(strcasecmp(key, "--list-logos") == 0)
702702
{
703-
ffListBuiltinLogos();
703+
ffLogoBuiltinList();
704704
exit(0);
705705
}
706706
else if(strcasecmp(key, "--list-logos-autocompletion") == 0)
707707
{
708-
ffListBuiltinLogosAutocompletion();
708+
ffLogoBuiltinListAutocompletion();
709709
exit(0);
710710
}
711711
else if(strcasecmp(key, "--print-logos") == 0)
712712
{
713-
ffPrintBuiltinLogos(instance);
713+
ffLogoBuiltinPrint(instance);
714714
exit(0);
715715
}
716716

src/fastfetch.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ typedef struct FFconfig
6161
uint32_t logoPaddingRight;
6262
bool logoPrintRemaining;
6363

64-
FFstrbuf mainColor; //If this is empty, ffPrintLogo will set it to the main color of the logo
64+
FFstrbuf mainColor; //If this is empty, ffLogoPrint will set it to the main color of the logo
6565
FFstrbuf separator;
6666

6767
bool showErrors;
@@ -525,14 +525,13 @@ bool ffSettingsGetAndroidProperty(const char* propName, FFstrbuf* result);
525525
// Logo functions //
526526
////////////////////
527527

528-
void ffPrintLogo(FFinstance* instance);
529-
void ffPrintRemainingLogo(FFinstance* instance);
528+
void ffLogoPrint(FFinstance* instance);
529+
void ffLogoPrintRemaining(FFinstance* instance);
530+
void ffLogoPrintLine(FFinstance* instance);
530531

531-
void ffPrintLogoLine(FFinstance* instance);
532-
533-
void ffPrintBuiltinLogos(FFinstance* instance);
534-
void ffListBuiltinLogos();
535-
void ffListBuiltinLogosAutocompletion();
532+
void ffLogoBuiltinPrint(FFinstance* instance);
533+
void ffLogoBuiltinList();
534+
void ffLogoBuiltinListAutocompletion();
536535

537536
/////////////////////////
538537
// Detection functions //

src/logo/builtin.c

Lines changed: 3 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
#include "logo.h"
22

3-
#include <limits.h>
4-
#include <string.h>
5-
#include <unistd.h>
6-
#include <ctype.h>
7-
8-
typedef struct FFlogo
9-
{
10-
const char* data;
11-
const char** names; //Null terminated
12-
const char** builtinColors; //Null terminated
13-
} FFlogo;
14-
153
#define FF_LOGO_INIT static FFlogo logo; static bool init = false; if(init) return &logo; init = true;
164
#define FF_LOGO_NAMES(...) static const char* names[] = (const char*[]) { __VA_ARGS__, NULL }; logo.names = names;
175
#define FF_LOGO_LINES(x) logo.data = x;
186
#define FF_LOGO_COLORS(...) static const char* colors[] = (const char*[]) { __VA_ARGS__, NULL }; logo.builtinColors = colors;
197
#define FF_LOGO_RETURN return &logo;
208

21-
static const FFlogo* getLogoUnknown()
9+
const FFlogo* ffLogoBuiltinGetUnknown()
2210
{
2311
FF_LOGO_INIT
2412
FF_LOGO_NAMES("unknown", "question mark", "?")
@@ -1441,13 +1429,11 @@ static const FFlogo* getLogoZorin()
14411429
FF_LOGO_RETURN
14421430
}
14431431

1444-
typedef const FFlogo*(*GetLogoMethod)();
1445-
1446-
static GetLogoMethod* getLogos()
1432+
GetLogoMethod* ffLogoBuiltinGetAll()
14471433
{
14481434
static GetLogoMethod logoMethods[] = {
1435+
ffLogoBuiltinGetUnknown,
14491436
getLogoNone,
1450-
getLogoUnknown,
14511437
getLogoAndroid,
14521438
getLogoAndroidSmall,
14531439
getLogoArch,
@@ -1504,187 +1490,3 @@ static GetLogoMethod* getLogos()
15041490

15051491
return logoMethods;
15061492
}
1507-
1508-
static bool logoHasName(const FFlogo* logo, const char* name)
1509-
{
1510-
const char** logoName = logo->names;
1511-
1512-
while(*logoName != NULL)
1513-
{
1514-
if(strcasecmp(*logoName, name) == 0)
1515-
return true;
1516-
++logoName;
1517-
}
1518-
1519-
return false;
1520-
}
1521-
1522-
static const FFlogo* getBuiltinLogo(const char* name)
1523-
{
1524-
GetLogoMethod* methods = getLogos();
1525-
1526-
while(*methods != NULL)
1527-
{
1528-
const FFlogo* logo = (*methods)();
1529-
if(logoHasName(logo, name))
1530-
return logo;
1531-
++methods;
1532-
}
1533-
1534-
return NULL;
1535-
}
1536-
1537-
static const FFlogo* detectBuiltinLogoWithVersion(const FFstrbuf* versionString, const FFstrbuf* name)
1538-
{
1539-
if(versionString->length == 0)
1540-
return getBuiltinLogo(name->chars);
1541-
1542-
#define FF_PRINT_LOGO_VERSIONED_IF_EXISTS(newLogo, oldLogo, ver) \
1543-
if(logoHasName(newLogo, name->chars)) \
1544-
{ \
1545-
long version = strtol(versionString->chars, NULL, 10); \
1546-
return (version == 0 || version == LONG_MAX || version == LONG_MIN || version >= ver) ? newLogo : oldLogo; \
1547-
}
1548-
1549-
FF_PRINT_LOGO_VERSIONED_IF_EXISTS(getLogoFedora(), getLogoFedoraOld(), 34)
1550-
FF_PRINT_LOGO_VERSIONED_IF_EXISTS(getLogoMint(), getLogoMintOld(), 19)
1551-
1552-
return getBuiltinLogo(name->chars);
1553-
}
1554-
1555-
static const FFlogo* detectBuiltinLogo(const FFinstance* instance)
1556-
{
1557-
static const FFlogo* logo;
1558-
static bool detected = false;
1559-
if(detected)
1560-
return logo;
1561-
1562-
detected = true;
1563-
1564-
const FFOSResult* os = ffDetectOS(instance);
1565-
1566-
logo = detectBuiltinLogoWithVersion(&os->version, &os->name);
1567-
if(logo != NULL)
1568-
return logo;
1569-
1570-
logo = detectBuiltinLogoWithVersion(&os->version, &os->id);
1571-
if(logo != NULL)
1572-
return logo;
1573-
1574-
logo = detectBuiltinLogoWithVersion(&os->version, &os->idLike);
1575-
if(logo != NULL)
1576-
return logo;
1577-
1578-
logo = detectBuiltinLogoWithVersion(&os->version, &os->systemName);
1579-
if(logo != NULL)
1580-
return logo;
1581-
1582-
return getLogoUnknown();
1583-
}
1584-
1585-
void ffLogoSetMainColor(FFinstance* instance)
1586-
{
1587-
const FFlogo* logo = NULL;
1588-
1589-
if(instance->config.logoSource.length > 0)
1590-
logo = getBuiltinLogo(instance->config.logoSource.chars);
1591-
1592-
if(logo == NULL)
1593-
logo = detectBuiltinLogo(instance);
1594-
1595-
ffStrbufAppendS(&instance->config.mainColor, logo->builtinColors[0]);
1596-
}
1597-
1598-
static void printLogoStruct(FFinstance* instance, const FFlogo* logo, bool doColorReplacement)
1599-
{
1600-
if(!doColorReplacement)
1601-
{
1602-
ffLogoPrint(instance, logo->data, false);
1603-
return;
1604-
}
1605-
1606-
const char** colors = logo->builtinColors;
1607-
for(int i = 0; *colors != NULL && i < FASTFETCH_LOGO_MAX_COLORS; i++, colors++)
1608-
{
1609-
if(instance->config.logoColors[i].length == 0)
1610-
ffStrbufAppendS(&instance->config.logoColors[i], *colors);
1611-
}
1612-
1613-
ffLogoPrint(instance, logo->data, true);
1614-
}
1615-
1616-
void ffLogoPrintUnknown(FFinstance* instance)
1617-
{
1618-
printLogoStruct(instance, getLogoUnknown(), false);
1619-
}
1620-
1621-
bool ffLogoPrintBuiltinIfExists(FFinstance* instance)
1622-
{
1623-
const FFlogo* logo = getBuiltinLogo(instance->config.logoSource.chars);
1624-
if(logo == NULL)
1625-
return false;
1626-
1627-
printLogoStruct(instance, logo, true);
1628-
return true;
1629-
}
1630-
1631-
void ffLogoPrintBuiltinDetected(FFinstance* instance)
1632-
{
1633-
printLogoStruct(instance, detectBuiltinLogo(instance), true);
1634-
}
1635-
1636-
void ffPrintBuiltinLogos(FFinstance* instance)
1637-
{
1638-
GetLogoMethod* methods = getLogos();
1639-
1640-
while(*methods != NULL)
1641-
{
1642-
const FFlogo* logo = (*methods)();
1643-
printf("\033[%sm%s:\033[0m\n", logo->builtinColors[0], logo->names[0]);
1644-
printLogoStruct(instance, logo, true);
1645-
ffPrintRemainingLogo(instance);
1646-
1647-
instance->state.logoHeight = 0;
1648-
for(uint8_t i = 0; i < FASTFETCH_LOGO_MAX_COLORS; i++)
1649-
ffStrbufClear(&instance->config.logoColors[i]);
1650-
1651-
puts("\n");
1652-
++methods;
1653-
}
1654-
}
1655-
1656-
void ffListBuiltinLogos()
1657-
{
1658-
GetLogoMethod* methods = getLogos();
1659-
1660-
uint32_t counter = 0;
1661-
1662-
while(*methods != NULL)
1663-
{
1664-
const FFlogo* logo = (*methods)();
1665-
const char** names = logo->names;
1666-
1667-
printf("%u)%s ", counter, counter < 10 ? " " : "");
1668-
++counter;
1669-
1670-
while(*names != NULL)
1671-
{
1672-
printf("\"%s\" ", *names);
1673-
++names;
1674-
}
1675-
1676-
putchar('\n');
1677-
++methods;
1678-
}
1679-
}
1680-
1681-
void ffListBuiltinLogosAutocompletion()
1682-
{
1683-
GetLogoMethod* methods = getLogos();
1684-
1685-
while(*methods != NULL)
1686-
{
1687-
printf("%s\n", (*methods)()->names[0]);
1688-
++methods;
1689-
}
1690-
}

0 commit comments

Comments
 (0)