Custom logo colors support

This commit is contained in:
Linus Dierheimer 2021-12-19 22:39:22 +01:00
parent 87eb2659de
commit a011b55692
No known key found for this signature in database
GPG Key ID: 74FA57726CDD7B61
15 changed files with 190 additions and 83 deletions

View File

@ -7,8 +7,8 @@ tab_width = 4
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
[*.{md,fflogo}]
trim_trailing_whitespace = false
[*.txt]
[*.{txt,fflogo}]
insert_final_newline = false

View File

@ -5,9 +5,7 @@ Here i just add things that are easy to forget.
## Features i am planning to implement
- [ ] Support overwriting logo colors (e.g. --color-1 35 --color-2 31)
- [ ] Support for color name mapping (e.g. --color red)
- [ ] Support for colors in user provided logo files (requires point 1 to be implemented)
- [ ] Support for printing images as ascii art logos (using imlib2 and libcaca probably)
- [ ] Support for printing images in terminals that support it
- [ ] Better documentation (especially default config file and the various --help options. Colored output?)

View File

@ -114,6 +114,10 @@ static void defaultConfig(FFinstance* instance)
instance->config.allowSlowOperations = false;
instance->config.disableLinewrap = true;
instance->config.hideCursor = true;
instance->config.userLogoIsRaw = false;
for(uint8_t i = 0; i < (uint8_t) FASTFETCH_LOGO_MAX_COLORS; ++i)
ffStrbufInit(&instance->config.logoColors[i]);
ffLoadLogo(instance);

View File

@ -12,7 +12,7 @@ void ffPrintLogoAndKey(FFinstance* instance, const char* moduleName, uint8_t mod
ffPrintLogoLine(instance);
fputs(FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
ffStrbufWriteTo(&instance->config.color, stdout);
ffPrintColor(&instance->config.color);
if(customKeyFormat == NULL || customKeyFormat->length == 0)
{
@ -490,3 +490,10 @@ void ffSuppressIO(bool suppress)
dup2(suppress ? nullFile : origOut, STDOUT_FILENO);
dup2(suppress ? nullFile : origErr, STDERR_FILENO);
}
void ffPrintColor(const FFstrbuf* colorValue)
{
fputs("\033[", stdout);
ffStrbufWriteTo(colorValue, stdout);
fputc('m', stdout);
}

View File

@ -7,7 +7,7 @@
#define FF_LOGO_INIT static FFlogo logo; static bool init = false; if(init) return &logo; init = true; logo.isFromUser = false;
#define FF_LOGO_NAMES(...) static const char* names[] = (const char*[]) { __VA_ARGS__, NULL }; logo.names = names;
#define FF_LOGO_LINES(x) logo.lines = x;
#define FF_LOGO_COLORS(...) static const char* colors[] = (const char*[]) { __VA_ARGS__, NULL }; logo.colors = colors;
#define FF_LOGO_COLORS(...) static const char* colors[] = (const char*[]) { __VA_ARGS__, NULL }; logo.builtinColors = colors;
#define FF_LOGO_RETURN return &logo;
static const FFlogo* getLogoUnknown()
@ -73,7 +73,7 @@ static const FFlogo* getLogoArch()
"$1.` `/";
)
FF_LOGO_COLORS(
"\033[36m" //cyan
"36" //cyan
)
FF_LOGO_RETURN
}
@ -92,7 +92,7 @@ static const FFlogo* getLogoArchSmall()
"$1/_-'' ''-_\\"
)
FF_LOGO_COLORS(
"\033[36m" //cyan
"36" //cyan
)
FF_LOGO_RETURN
}
@ -124,7 +124,7 @@ static const FFlogo* getLogoArtix()
"$1'` `'";
)
FF_LOGO_COLORS(
"\033[36m" //cyan
"36" //cyan
)
FF_LOGO_RETURN
}
@ -143,7 +143,7 @@ static const FFlogo* getLogoArtixSmall()
"$1/.,'` `'.\\"
)
FF_LOGO_COLORS(
"\033[36m" //cyan
"36" //cyan
)
FF_LOGO_RETURN
}
@ -175,8 +175,8 @@ static const FFlogo* getLogoArcoLinux()
"$1 ooooooooo. $2 -ooooooooo";
)
FF_LOGO_COLORS(
"\033[34m", //blue
"\033[32m" //green
"34", //blue
"32" //green
)
FF_LOGO_RETURN
}
@ -203,8 +203,8 @@ static const FFlogo* getLogoCelOS()
"$1 `.:/+ooooo+:-` "
)
FF_LOGO_COLORS(
"\033[35m", //magenta
"\033[30m" //black
"35", //magenta
"30" //black
)
FF_LOGO_RETURN
}
@ -235,11 +235,11 @@ static const FFlogo* getLogoCentOS()
"$1 '' "
)
FF_LOGO_COLORS(
"\033[33m", //yellow
"\033[32m", //green
"\033[34m", //blue
"\033[35m", //magenta
"\033[37m" //white
"33", //yellow
"32", //green
"34", //blue
"35", //magenta
"37" //white
)
FF_LOGO_RETURN
}
@ -258,10 +258,10 @@ static const FFlogo* getLogoCentOSSmall()
"$2 v "
)
FF_LOGO_COLORS(
"\033[33m", //yellow
"\033[32m", //green
"\033[34m", //blue
"\033[35m" //magenta
"33", //yellow
"32", //green
"34", //blue
"35" //magenta
)
FF_LOGO_RETURN
}
@ -290,8 +290,8 @@ static const FFlogo* getLogoDebian()
"$2 `\"\"\" "
)
FF_LOGO_COLORS(
"\033[31m", //red
"\033[37m" //white
"31", //red
"37" //white
)
FF_LOGO_RETURN
}
@ -309,7 +309,7 @@ static const FFlogo* getLogoDebianSmall()
"$1 --_ "
)
FF_LOGO_COLORS(
"\033[31m" //red
"31" //red
)
FF_LOGO_RETURN
}
@ -340,8 +340,8 @@ static const FFlogo* getLogoFedora()
"$1 ':cccccccccccccccc::;,. "
)
FF_LOGO_COLORS(
"\033[34m", //blue
"\033[37m" //white
"34", //blue
"37" //white
)
FF_LOGO_RETURN
}
@ -362,7 +362,7 @@ static const FFlogo* getLogoFedoraSmall()
"$1 ''''' "
)
FF_LOGO_COLORS(
"\033[34m" //blue
"34" //blue
)
FF_LOGO_RETURN
}
@ -391,8 +391,8 @@ static const FFlogo* getLogoFedoraOld()
"$1:---------------------:// "
)
FF_LOGO_COLORS(
"\033[34m", //blue
"\033[37m" //white
"34", //blue
"37" //white
)
FF_LOGO_RETURN
}
@ -422,7 +422,7 @@ static const FFlogo* getLogoGaruda()
"$1 .d988999889889899dd. "
)
FF_LOGO_COLORS(
"\033[31m" //red
"31" //red
)
FF_LOGO_RETURN
}
@ -452,8 +452,8 @@ static const FFlogo* getLogoGentoo()
"$1 `-//////:--. "
)
FF_LOGO_COLORS(
"\033[35m", //magenta
"\033[37m" //white
"35", //magenta
"37" //white
)
FF_LOGO_RETURN
}
@ -472,8 +472,8 @@ static const FFlogo* getLogoGentooSmall()
"$2\\____- "
)
FF_LOGO_COLORS(
"\033[35m", //magenta
"\033[37m" //white
"35", //magenta
"37" //white
)
FF_LOGO_RETURN
}
@ -499,7 +499,7 @@ static const FFlogo* getLogoManjaro()
"$1████████ ████████ ████████"
)
FF_LOGO_COLORS(
"\033[32m" //green
"32" //green
)
FF_LOGO_RETURN
}
@ -518,7 +518,7 @@ static const FFlogo* getLogoManjaroSmall()
"$1|||| |||| ||||"
)
FF_LOGO_COLORS(
"\033[32m" //green
"32" //green
)
FF_LOGO_RETURN
}
@ -549,8 +549,8 @@ static const FFlogo* getLogoMint()
"$2 ``-:::::-`` "
)
FF_LOGO_COLORS(
"\033[32m", //green
"\033[37m" //white
"32", //green
"37" //white
)
FF_LOGO_RETURN
}
@ -569,8 +569,8 @@ static const FFlogo* getLogoMintSmall()
"$1 \\_________/"
)
FF_LOGO_COLORS(
"\033[32m", //green
"\033[37m" //white
"32", //green
"37" //white
)
FF_LOGO_RETURN
}
@ -598,8 +598,8 @@ static const FFlogo* getLogoMintOld()
"$1 .MMMMMMMMMMMMMMMMMMM "
)
FF_LOGO_COLORS(
"\033[32m", //green
"\033[37m" //white
"32", //green
"37" //white
)
FF_LOGO_RETURN
}
@ -631,8 +631,8 @@ static const FFlogo* getLogoPop()
"$1 ///////////// "
)
FF_LOGO_COLORS(
"\033[36m", //cyan
"\033[37m" //white
"36", //cyan
"37" //white
)
FF_LOGO_RETURN
}
@ -652,7 +652,7 @@ static const FFlogo* getLogoPopSmall()
"$1 (___________)` "
)
FF_LOGO_COLORS(
"\033[36m" //cyan
"36" //cyan
)
FF_LOGO_RETURN
}
@ -684,8 +684,8 @@ static const FFlogo* getLogoUbuntu()
"$1 .-/+oossssoo+/-. "
)
FF_LOGO_COLORS(
"\033[31m", //red
"\033[37m" //white
"31", //red
"37" //white
)
FF_LOGO_RETURN
}
@ -703,7 +703,7 @@ static const FFlogo* getLogoUbuntuSmall()
"$1 ---(_)"
)
FF_LOGO_COLORS(
"\033[31m" //red
"31" //red
)
FF_LOGO_RETURN
}
@ -733,8 +733,8 @@ static const FFlogo* getLogoVoid()
"$1 -~|{*l}*|~ "
)
FF_LOGO_COLORS(
"\033[32m", //green
"\033[30m" //black
"32", //green
"30" //black
)
FF_LOGO_RETURN
}
@ -753,7 +753,7 @@ static const FFlogo* getLogoVoidSmall()
"$1 -_______\\ "
)
FF_LOGO_COLORS(
"\033[32m" //green
"32" //green
)
FF_LOGO_RETURN
}
@ -813,11 +813,24 @@ static bool logoHasName(const FFlogo* logo, const char* name)
return false;
}
static inline void setLogo(FFinstance* instance, const FFlogo* logo)
static void setLogo(FFinstance* instance, const FFlogo* logo)
{
instance->config.logo = logo;
instance->state.logoWidth = 0;
instance->state.logoLinesIndex = logo->lines;
const char** colors = logo->builtinColors;
int counter = 0;
while(!logo->isFromUser && *colors != NULL)
{
ffStrbufSetS(&instance->config.logoColors[counter], *colors);
++counter;
++colors;
}
for(; counter < FASTFETCH_LOGO_MAX_COLORS; ++counter)
ffStrbufClear(&instance->config.logoColors[counter]);
}
static bool loadLogoSet(FFinstance* instance, const char* name)
@ -858,11 +871,7 @@ void ffLoadLogoSet(FFinstance* instance, const char* logo)
static FFlogo dummyLogo;
dummyLogo.isFromUser = true;
dummyLogo.colors = getLogoNone()->colors;
dummyLogo.lines = logoChars.chars;
static const char* NULLSTR = NULL;
dummyLogo.names = &NULLSTR;
setLogo(instance, &dummyLogo);
}
@ -959,18 +968,48 @@ void ffPrintLogoLine(FFinstance* instance)
{
//Get the current char
char current = *instance->state.logoLinesIndex;
++instance->state.logoLinesIndex;
//We have a color encoding directly in the logo. Print it and increase colorPlaceholdersLength
if(instance->config.logo->isFromUser && current == '\033' && *(instance->state.logoLinesIndex + 1) == '[')
{
//At this point we don't know if the start ends with a 'm', so we need to buffer
FFstrbuf colorBuffer;
ffStrbufInit(&colorBuffer);
char* indexCopy = instance->state.logoLinesIndex;
while(*indexCopy != '\n' && *indexCopy != '\0')
{
ffStrbufAppendC(&colorBuffer, *indexCopy);
if(*indexCopy == 'm')
break;
++indexCopy;
}
//It was a color encoding, write it and increase colorPlaceholdersLength
if(*indexCopy == 'm')
{
ffStrbufWriteTo(&colorBuffer, stdout);
colorPlaceholdersLength += colorBuffer.length;
instance->state.logoLinesIndex = indexCopy + 1; // + 1 to skip the 'm'
current = *instance->state.logoLinesIndex;
}
ffStrbufDestroy(&colorBuffer);
}
//Not a color placeholder, just print it. User files don't support colors this way.
if(current != '$' || instance->config.logo->isFromUser)
if(current != '$' || instance->config.userLogoIsRaw)
{
LOGO_LINE_PRINT_CHAR(current, cut);
++instance->state.logoLinesIndex;
continue;
}
//Skip the dollar sign
current = *instance->state.logoLinesIndex;
++instance->state.logoLinesIndex;
current = *instance->state.logoLinesIndex;
//We have a dollar sign at the end of the line. Just print it.
if(current == '\n' || current == '\0')
@ -980,11 +1019,15 @@ void ffPrintLogoLine(FFinstance* instance)
break;
}
//We definitly have something to print and are not at the end of the line.
//Already increase it here, so i don't have to write it multiple times
++instance->state.logoLinesIndex;
//We have two dollar signs. Print one.
if(current == '$')
{
++colorPlaceholdersLength;
LOGO_LINE_PRINT_CHAR('$', cut);
++colorPlaceholdersLength;
continue;
}
@ -992,7 +1035,7 @@ void ffPrintLogoLine(FFinstance* instance)
int index = ((int) current) - 49;
//Invalid index, just print the $ and the following char
if(index < 0 || index > 9)
if(index < 0 || index >= FASTFETCH_LOGO_MAX_COLORS)
{
LOGO_LINE_PRINT_CHAR('$', cut);
LOGO_LINE_PRINT_CHAR(current, cut);
@ -1002,9 +1045,11 @@ void ffPrintLogoLine(FFinstance* instance)
//Index can maximal be 9, so we have exactly two chars for the color
colorPlaceholdersLength += 2;
//Print the color, if color support is on.
if(instance->config.colorLogo)
fputs(instance->config.logo->colors[index], stdout);
// If we don't color our logo, skip it
if(!instance->config.colorLogo)
continue;
ffPrintColor(&instance->config.logoColors[index]);
}
//Reset out bold logo
@ -1045,7 +1090,7 @@ void ffPrintLogos(FFinstance* instance)
while(*methods != NULL)
{
setLogo(instance, (*methods)());
printf(FASTFETCH_TEXT_MODIFIER_BOLT"%s%s"FASTFETCH_TEXT_MODIFIER_RESET":\n", instance->config.colorLogo ? instance->config.logo->colors[0] : "", instance->config.logo->names[0]);
printf(FASTFETCH_TEXT_MODIFIER_BOLT"\033[%sm%s"FASTFETCH_TEXT_MODIFIER_RESET":\n", instance->config.colorLogo ? instance->config.logo->builtinColors[0] : "", instance->config.logo->names[0]);
ffPrintRemainingLogo(instance);
puts("\n");
++methods;

View File

@ -102,6 +102,24 @@
# Default is "-"
#--separator-string -
# Logo color options:
# Overwrite a color in the logo. Also works for user provided logos.
# In the user logo, they replace $[1-9]. Use $$ to print a single $ sign.
# Must be linux console color codes.
# Default is the one specified by the logo.
# Use "fastfetch --help color" to learn more and see examples.
# Valid index range is [1-9].
#--color-1 31
#--color-2 32
# [...]
#--color-9 39
# User logo raw option:
# Sets if color replacements should be performed on the user provided logo.
# Must be true or false.
# Default is true.
#--color-raw true
# Key options:
# Sets the displayed key of a module
# Can be any string. Some of theme take an argument like a format string. See "fastfetch --help format" for help.

View File

@ -16,6 +16,7 @@ General options:
--structure <structure>: sets the structure of the fetch. Must be a colon separated list of keys. Use "fastfetch --list-modules" to see the ones available.
--set <key=value>: hard set the value of a key
-c <color>, --color <color>: sets the color of the keys. Must be a linux console color code (+)
--color-[1-9] <color>: overwrites a color of the logo. Must be a linux console color code
--spacing <width>: sets the distance between logo and text
-s <str>, --separator <str>: sets the separator between key and value. Default is a colon with a space
-x <offset>, --offsetx <offset>: sets the x offset. Can be negative to cut the logo, but no more than logo width.
@ -28,6 +29,7 @@ General options:
--allow-slow-operations <?value>: Allow operations that are usually very slow for more detailed output
--disable-linewrap <?value>: Disable linewrap during the run
--hide-cursor <?value>: Hide the cursor during the run
--logo-raw <?value>: Print a custom logo as is, without any color replacements
Logo options:
-l <name>, --logo <name>: sets the shown logo. Also changes the main color accordingly. This will also load file contents as logo if the given argument is a valid path.

View File

@ -14,6 +14,7 @@ typedef struct FFdata
FFvaluestore valuestore;
FFstrbuf structure;
FFstrbuf logoName;
FFstrbuf logoColors[FASTFETCH_LOGO_MAX_COLORS];
bool multithreading;
} FFdata;
@ -632,6 +633,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
instance->config.disableLinewrap = optionParseBoolean(value);
else if(strcasecmp(key, "--hide-cursor") == 0)
instance->config.hideCursor = optionParseBoolean(value);
else if(strcasecmp(key, "--logo-raw") == 0)
instance->config.userLogoIsRaw = optionParseBoolean(value);
else if(strcasecmp(key, "--structure") == 0)
optionParseString(key, value, &data->structure);
else if(strcasecmp(key, "-l") == 0 || strcasecmp(key, "--logo") == 0)
@ -639,16 +642,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
else if(strcasecmp(key, "-s") == 0 || strcasecmp(key, "--separator") == 0)
optionParseString(key, value, &instance->config.separator);
else if(strcasecmp(key, "-c") == 0 || strcasecmp(key, "--color") == 0)
{
if(value == NULL)
{
fprintf(stderr, "Error: usage: %s <str>\n", key);
exit(477);
}
ffStrbufSetS(&instance->config.color, "\033[");
ffStrbufAppendS(&instance->config.color, value);
ffStrbufAppendC(&instance->config.color, 'm');
}
optionParseString(key, value, &instance->config.color);
else if(strcasecmp(key, "--os-format") == 0)
optionParseString(key, value, &instance->config.osFormat);
else if(strcasecmp(key, "--os-key") == 0)
@ -779,6 +773,20 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
instance->config.localIpShowIpV6 = optionParseBoolean(value);
else if(strcasecmp(key, "--localip-show-loop") == 0)
instance->config.localIpShowLoop = optionParseBoolean(value);
else if(strncasecmp(key, "--color-", 7) == 0 && key[8] != '\0' && key[9] == '\0') // matches "--color-*"
{
//Map the number to an array index, so that '1' -> 0, '2' -> 1, etc.
int index = (int)key[8] - 49;
//Match only --color-[1-9]
if(index < 0 || index >= FASTFETCH_LOGO_MAX_COLORS)
{
fprintf(stderr, "Error: invalid --color-[1-9] index: %c\n", key[8]);
exit(472);
}
optionParseString(key, value, &data->logoColors[index]);
}
else
{
fprintf(stderr, "Error: unknown option: %s\n", key);
@ -922,6 +930,9 @@ int main(int argc, const char** argv)
ffStrbufInitA(&data.logoName, 0);
data.multithreading = true;
for(uint8_t i = 0; i < FASTFETCH_LOGO_MAX_COLORS; i++)
ffStrbufInitA(&data.logoColors[i], 0);
parseDefaultConfigFile(&instance, &data);
parseArguments(&instance, &data, argc, argv);
@ -931,7 +942,14 @@ int main(int argc, const char** argv)
//If we haven't set key color, use primary color of logo
if(instance.config.color.length == 0)
ffStrbufSetS(&instance.config.color, instance.config.logo->colors[0]);
ffStrbufSet(&instance.config.color, &instance.config.logoColors[0]);
//Overwrite logo colors with custom colors
for(uint8_t i = 0; i < FASTFETCH_LOGO_MAX_COLORS; i++)
{
if(data.logoColors[i].length > 0)
ffStrbufSet(&instance.config.logoColors[i], &data.logoColors[i]);
}
//Start detection threads
if(data.multithreading)
@ -943,6 +961,7 @@ int main(int argc, const char** argv)
ffStart(&instance);
//Parse the structure and call the modules
uint32_t startIndex = 0;
while (startIndex < data.structure.length)
{

View File

@ -26,17 +26,20 @@ static inline void ffUnused(int dummy, ...) { (void) dummy; }
#define FASTFETCH_TEXT_MODIFIER_ERROR "\033[1;31m"
#define FASTFETCH_TEXT_MODIFIER_RESET "\033[0m"
#define FASTFETCH_LOGO_MAX_COLORS 9 //two digits would make parsing much more complicated (index 1 - 9)
typedef struct FFlogo
{
const char** names; //Null terminated
char* lines;
const char** colors; // colors[0] is used as key color
bool isFromUser;
const char** builtinColors; // [0] is used as key color, if not user specified
} FFlogo;
typedef struct FFconfig
{
const FFlogo* logo;
FFstrbuf logoColors[FASTFETCH_LOGO_MAX_COLORS];
uint16_t logoKeySpacing;
FFstrbuf separator;
@ -50,6 +53,7 @@ typedef struct FFconfig
bool allowSlowOperations;
bool disableLinewrap;
bool hideCursor;
bool userLogoIsRaw;
FFstrbuf osFormat;
FFstrbuf osKey;
@ -331,6 +335,8 @@ void ffWriteFileContent(const char* fileName, const FFstrbuf* buffer);
// Not thread safe!
void ffSuppressIO(bool suppress);
void ffPrintColor(const FFstrbuf* colorValue);
// They return true if the file was found, independently if start was found
// Buffers which already contain content are not overwritten
// The last occurence of start in the first file will be the one used

View File

@ -9,7 +9,7 @@ int main(int argc, char** argv)
ffInitInstance(&instance); //This also applys default configuration to instance.config
//ffLoadLogoSet(&instance, "my custom logo");
ffStrbufSetS(&instance.config.color, instance.config.logo->colors[0]); //Use the primary color of the logo as key color
ffStrbufSetS(&instance.config.color, instance.config.logoColors[0].chars); //Use the primary color of the logo as key color
//Multithreading --> better performance
ffStartDetectionThreads(&instance);

View File

@ -147,7 +147,7 @@ void ffPrintBattery(FFinstance* instance)
return;
}
for(uint8_t i = 0; i < dirs.length; i++)
for(uint8_t i = 0; i < (uint8_t) dirs.length; i++)
{
FFstrbuf* name = ffListGet(&dirs, i);
ffStrbufAppend(&baseDir, name);

View File

@ -205,7 +205,7 @@ void ffPrintGPU(FFinstance* instance)
vulkanFillGPUs(instance, &gpus);
#endif
for(uint8_t i = 0; i < gpus.length; i++)
for(uint8_t i = 0; i < (uint8_t) gpus.length; i++)
printGPUResult(instance, gpus.length == 1 ? 0 : i + 1, &cache, ffListGet(&gpus, i));
if(gpus.length == 0)

View File

@ -31,7 +31,7 @@ const FFTitleResult* ffDetectTitle(FFinstance* instance)
static inline void printTitlePart(FFinstance* instance, const FFstrbuf* content)
{
fputs(FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
ffStrbufWriteTo(&instance->config.color, stdout);
ffPrintColor(&instance->config.color);
ffStrbufWriteTo(content, stdout);
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
}

View File

@ -0,0 +1,4 @@
 .
 ...
 .-|-.
.-| |-.

View File

@ -0,0 +1,4 @@
$1 .
$1 ...
$1 .-|-.
$1.-| |-.