color in format strings

This commit is contained in:
Linus Dierheimer
2021-04-25 09:13:18 +02:00
parent d469120e9b
commit 1b1d28f0ab
2 changed files with 40 additions and 1 deletions
+31
View File
@@ -75,6 +75,7 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFst
uint32_t numOpenIfs = 0;
uint32_t numOpenNotIfs = 0;
uint32_t numOpenColors = 0;
for(uint32_t i = 0; i < formatstr->length; ++i)
{
@@ -153,6 +154,21 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFst
continue;
}
// test for end of a color, if so do nothing
if(placeholderValue.length == 1 && placeholderValue.chars[0] == '#')
{
if(numOpenColors == 0)
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
else
{
ffStrbufAppendS(buffer, "\033[0m");
--numOpenColors;
}
ffStrbufDestroy(&placeholderValue);
continue;
}
// test for if, if so evaluate it
if(placeholderValue.chars[0] == '?')
{
@@ -227,6 +243,18 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFst
continue;
}
//test for color, if so evaluate it
if(placeholderValue.chars[0] == '#')
{
++numOpenColors;
ffStrbufSubstrAfter(&placeholderValue, 0);
ffStrbufAppendS(buffer, "\033[");
ffStrbufAppend(buffer, &placeholderValue);
ffStrbufAppendC(buffer, 'm');
ffStrbufDestroy(&placeholderValue);
continue;
}
uint32_t index = getArgumentIndex(&placeholderValue);
// test for invalid index
@@ -243,4 +271,7 @@ void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, const FFst
}
ffStrbufTrimRight(buffer, ' ');
if(numOpenColors > 0)
ffStrbufAppendS(buffer, "\033[0m");
}
+9 -1
View File
@@ -130,7 +130,7 @@ static inline void printCommandHelpColor()
"usage: fastfetch --color <color>\n"
"\n"
"<color> must be a color encoding for linux terminals. It is inserted between \"ESC[\" and \"m\".\n"
"Infos about them can be found here: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors.\n"
"Infos about them can be found here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters.\n"
"Examples:\n"
" \"--color 35\": sets the color to pink\n"
" \"--color 4;92\": sets the color to bright Green with underline\n"
@@ -169,6 +169,14 @@ static inline void printCommandHelpFormat()
"To stop formating at any point in the format string, use \"{-}\".\n"
"For example to print an error instead of the normal output if it occured, prefix the format string with \"{?e}{e}{-}{?}...\".\n"
"\n"
"To print something with color, start a placeholder with a '#' and then the linux terminal color encoding.\n"
"\"\\033[\" at the start and a 'm' at the end is automatically added, so don't do that.\n"
"A \"{#}\" is equivalent to a \"{#0}\" and resets everything to normal.\n"
"For example to print something pink and underline use \"{#4;35}...{#}\".\n"
"If not in a format string, fastfetch wraps errors with \"{#1;31}error{#}\", so you might want to do that to if you show errors.\n"
"Information about what the numbers mean can be found here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters.\n"
"Which escape codes are supported and how they look like is defined by your terminal.\n"
"\n"
"If an format string evaluates to an empty value, the whole line in the output will be discarded.\n"
"You can therefore use --host-format \" \" to disable host output.\n"
"Note that --host-format \"\" would evaluate as not set, and therefore use the built in host format\n"