Files
fastfetch/src/common/format.c
T

413 lines
14 KiB
C
Raw Normal View History

2021-04-09 14:07:05 +02:00
#include "fastfetch.h"
#include "common/format.h"
2022-09-05 17:42:42 +02:00
#include "common/parsing.h"
2022-12-06 17:03:31 +08:00
#include "util/textModifier.h"
2023-02-01 15:56:25 +01:00
#include "util/stringUtils.h"
2021-04-09 14:07:05 +02:00
2022-10-13 18:12:54 +08:00
#include <inttypes.h>
2021-04-16 17:46:50 +02:00
void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg)
{
switch(formatarg->type)
2021-05-23 19:40:34 +02:00
{
case FF_FORMAT_ARG_TYPE_INT:
ffStrbufAppendF(buffer, "%" PRIi32, *(int32_t*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_UINT:
ffStrbufAppendF(buffer, "%" PRIu32, *(uint32_t*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_UINT64:
ffStrbufAppendF(buffer, "%" PRIu64, *(uint64_t*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_UINT16:
ffStrbufAppendF(buffer, "%" PRIu16, *(uint16_t*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_UINT8:
ffStrbufAppendF(buffer, "%" PRIu8, *(uint8_t*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_STRING:
ffStrbufAppendS(buffer, (const char*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_STRBUF:
ffStrbufAppend(buffer, (FFstrbuf*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_FLOAT:
ffStrbufAppendF(buffer, "%f", *(float*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_DOUBLE:
ffStrbufAppendF(buffer, "%g", *(double*)formatarg->value);
break;
case FF_FORMAT_ARG_TYPE_BOOL:
ffStrbufAppendS(buffer, *(bool*)formatarg->value ? "true" : "false");
break;
case FF_FORMAT_ARG_TYPE_LIST:
2021-05-23 19:40:34 +02:00
{
2024-10-25 14:48:11 +08:00
const FFlist* list = (const FFlist*) formatarg->value;
for(uint32_t i = 0; i < list->length; i++)
{
2024-10-25 14:48:11 +08:00
ffStrbufAppend(buffer, FF_LIST_GET(FFstrbuf, *list, i));
if(i < list->length - 1)
ffStrbufAppendS(buffer, ", ");
}
break;
2021-05-23 19:40:34 +02:00
}
default:
if(formatarg->type != FF_FORMAT_ARG_TYPE_NULL)
fprintf(stderr, "Error: format string \"%s\": argument is not implemented: %i\n", buffer->chars, formatarg->type);
break;
2021-04-16 17:46:50 +02:00
}
}
/**
* @brief parses a string to a uint32_t
*
* If the string can't be parsed, or is < 1, uint32_t max is returned.
*
* @param placeholderValue the string to parse
* @return uint32_t the parsed value
*/
2024-06-26 16:14:34 +08:00
static uint32_t getArgumentIndex(const char* placeholderValue, uint32_t numArgs, const FFformatarg* arguments)
2021-04-24 21:42:48 +02:00
{
2024-06-26 16:14:34 +08:00
char firstChar = placeholderValue[0];
if (firstChar == '\0')
return 0; // use arg counter
2021-04-24 21:42:48 +02:00
2024-06-26 16:14:34 +08:00
if (firstChar >= '0' && firstChar <= '9')
2024-05-27 16:41:37 +08:00
{
char* pEnd = NULL;
2024-06-26 16:14:34 +08:00
uint32_t result = (uint32_t) strtoul(placeholderValue, &pEnd, 10);
if (result > numArgs)
2024-05-27 16:41:37 +08:00
return UINT32_MAX;
2024-06-26 16:14:34 +08:00
if (*pEnd != '\0')
return UINT32_MAX;
return result;
2024-05-27 16:41:37 +08:00
}
else if (ffCharIsEnglishAlphabet(firstChar))
{
for (uint32_t i = 0; i < numArgs; ++i)
{
const FFformatarg* arg = &arguments[i];
2024-06-26 16:14:34 +08:00
if (arg->name && strcasecmp(placeholderValue, arg->name) == 0)
2024-05-27 16:41:37 +08:00
return i + 1;
}
}
2021-04-24 21:42:48 +02:00
2024-05-27 16:41:37 +08:00
return UINT32_MAX;
2021-04-24 21:42:48 +02:00
}
static inline void appendInvalidPlaceholder(FFstrbuf* buffer, const char* start, const FFstrbuf* placeholderValue, uint32_t index, uint32_t formatStringLength)
{
ffStrbufAppendS(buffer, start);
ffStrbufAppend(buffer, placeholderValue);
if(index < formatStringLength)
ffStrbufAppendC(buffer, '}');
}
static inline bool formatArgSet(const FFformatarg* arg)
{
return arg->value != NULL && (
2022-09-05 17:42:42 +02:00
(arg->type == FF_FORMAT_ARG_TYPE_DOUBLE && *(double*)arg->value > 0.0) || //Also is false for NaN
2021-04-24 21:42:48 +02:00
(arg->type == FF_FORMAT_ARG_TYPE_INT && *(int*)arg->value > 0) ||
(arg->type == FF_FORMAT_ARG_TYPE_STRBUF && ((FFstrbuf*)arg->value)->length > 0) ||
2024-10-25 14:48:11 +08:00
(arg->type == FF_FORMAT_ARG_TYPE_STRING && ffStrSet((char*)arg->value)) ||
2021-04-24 21:42:48 +02:00
(arg->type == FF_FORMAT_ARG_TYPE_UINT8 && *(uint8_t*)arg->value > 0) ||
2021-10-18 22:38:00 +08:00
(arg->type == FF_FORMAT_ARG_TYPE_UINT16 && *(uint16_t*)arg->value > 0) ||
2022-10-17 13:19:48 +02:00
(arg->type == FF_FORMAT_ARG_TYPE_UINT && *(uint32_t*)arg->value > 0) ||
(arg->type == FF_FORMAT_ARG_TYPE_BOOL && *(bool*)arg->value) ||
(arg->type == FF_FORMAT_ARG_TYPE_LIST && ((FFlist*)arg->value)->length > 0)
2021-04-24 21:42:48 +02:00
);
}
2022-06-17 18:15:36 +02:00
void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, uint32_t numArgs, const FFformatarg* arguments)
2021-04-09 14:07:05 +02:00
{
2021-04-24 21:42:48 +02:00
uint32_t argCounter = 0;
uint32_t numOpenIfs = 0;
uint32_t numOpenNotIfs = 0;
2021-04-09 14:07:05 +02:00
2024-05-27 16:41:37 +08:00
FF_STRBUF_AUTO_DESTROY placeholderValue = ffStrbufCreate();
2021-04-09 14:07:05 +02:00
for(uint32_t i = 0; i < formatstr->length; ++i)
{
2021-04-24 21:42:48 +02:00
// if we don't have a placeholder start just copy the chars over to output buffer
2021-04-09 14:07:05 +02:00
if(formatstr->chars[i] != '{')
{
ffStrbufAppendC(buffer, formatstr->chars[i]);
continue;
}
2021-04-24 21:42:48 +02:00
// jump to next char, the start of the placeholder value
2021-04-09 14:07:05 +02:00
++i;
2021-04-24 21:42:48 +02:00
// double {{ elvaluates to a single { and doesn't count as start
2021-04-09 14:07:05 +02:00
if(formatstr->chars[i] == '{')
{
ffStrbufAppendC(buffer, '{');
continue;
}
2024-05-27 16:41:37 +08:00
ffStrbufClear(&placeholderValue);
2021-04-24 21:42:48 +02:00
{
2024-05-27 16:41:37 +08:00
uint32_t iEnd = ffStrbufNextIndexC(formatstr, i, '}');
ffStrbufAppendNS(&placeholderValue, iEnd - i, &formatstr->chars[i]);
i = iEnd;
2021-04-24 21:42:48 +02:00
}
2024-05-27 16:41:37 +08:00
char firstChar = placeholderValue.chars[0];
if (placeholderValue.length == 1)
2021-04-24 21:42:48 +02:00
{
2024-05-27 16:41:37 +08:00
// test if for stop, if so break the loop
if (firstChar == '-')
break;
2021-04-24 21:42:48 +02:00
2024-05-27 16:41:37 +08:00
// test for end of an if, if so do nothing
if (firstChar == '?')
{
if(numOpenIfs == 0)
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
else
--numOpenIfs;
2021-04-24 21:42:48 +02:00
2024-05-27 16:41:37 +08:00
continue;
}
// test for end of a not if, if so do nothing
if (firstChar == '/')
2021-04-25 09:13:18 +02:00
{
2024-05-27 16:41:37 +08:00
if(numOpenNotIfs == 0)
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
else
--numOpenNotIfs;
continue;
2021-04-25 09:13:18 +02:00
}
2024-05-27 16:41:37 +08:00
// test for end of a color, if so do nothing
if (firstChar == '#')
{
2024-05-28 10:56:01 +08:00
if (!instance.config.display.pipe)
2024-05-27 16:41:37 +08:00
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
continue;
}
2021-04-25 09:13:18 +02:00
}
2021-04-24 21:42:48 +02:00
// test for if, if so evaluate it
2024-05-27 16:41:37 +08:00
if (firstChar == '?')
2021-04-24 21:42:48 +02:00
{
ffStrbufSubstrAfter(&placeholderValue, 0);
2024-06-26 16:14:34 +08:00
uint32_t index = getArgumentIndex(placeholderValue.chars, numArgs, arguments);
2021-04-24 21:42:48 +02:00
// testing for an invalid index
2024-05-27 16:41:37 +08:00
if (index > numArgs)
2021-04-24 21:42:48 +02:00
{
appendInvalidPlaceholder(buffer, "{?", &placeholderValue, i, formatstr->length);
continue;
}
// continue normally if an format arg is set and the value is > 0
2024-05-27 16:41:37 +08:00
if (formatArgSet(&arguments[index - 1]))
2021-04-09 14:07:05 +02:00
{
2021-04-24 21:42:48 +02:00
++numOpenIfs;
2021-04-09 14:07:05 +02:00
continue;
}
2021-04-24 21:42:48 +02:00
// fastforward to the end of the if without printing the in between
i = ffStrbufNextIndexS(formatstr, i, "{?}") + 2; // 2 is the length of "{?}" - 1 because the loop will increment it again directly after continue
2021-04-24 21:42:48 +02:00
continue;
2021-04-09 14:07:05 +02:00
}
2021-04-24 21:42:48 +02:00
// test for not if, if so evaluate it
2024-05-27 16:41:37 +08:00
if (firstChar == '/')
2021-04-09 14:07:05 +02:00
{
2021-04-24 21:42:48 +02:00
ffStrbufSubstrAfter(&placeholderValue, 0);
2024-06-26 16:14:34 +08:00
uint32_t index = getArgumentIndex(placeholderValue.chars, numArgs, arguments);
2021-04-24 21:42:48 +02:00
// testing for an invalid index
2024-05-27 16:41:37 +08:00
if (index > numArgs)
2021-04-24 21:42:48 +02:00
{
appendInvalidPlaceholder(buffer, "{/", &placeholderValue, i, formatstr->length);
2021-04-09 14:07:05 +02:00
continue;
}
2021-04-24 21:42:48 +02:00
//continue normally if an format arg is not set or the value is 0
2024-05-27 16:41:37 +08:00
if (!formatArgSet(&arguments[index - 1]))
2021-04-24 21:42:48 +02:00
{
++numOpenNotIfs;
continue;
}
// fastforward to the end of the if without printing the in between
i = ffStrbufNextIndexS(formatstr, i, "{/}") + 2; // 2 is the length of "{/}" - 1 because the loop will increment it again directly after continue
2021-04-24 21:42:48 +02:00
continue;
}
2021-04-25 09:13:18 +02:00
//test for color, if so evaluate it
2024-05-27 16:41:37 +08:00
if (firstChar == '#')
2021-04-25 09:13:18 +02:00
{
2024-05-28 10:56:01 +08:00
if (!instance.config.display.pipe)
{
ffStrbufAppendS(buffer, "\e[");
2024-05-28 15:38:45 +08:00
ffOptionParseColorNoClear(placeholderValue.chars + 1, buffer);
2024-05-28 10:56:01 +08:00
ffStrbufAppendC(buffer, 'm');
}
2021-04-25 09:13:18 +02:00
continue;
}
//test for constant or env var, if so evaluate it
if (firstChar == '$')
{
char* pend = NULL;
int32_t indexSigned = (int32_t) strtol(placeholderValue.chars + 1, &pend, 10);
if (pend == placeholderValue.chars + 1)
{
// treat placeholder as an environment variable
char* envValue = getenv(placeholderValue.chars + 1);
if (envValue)
ffStrbufAppendS(buffer, envValue);
else
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
}
else
{
// treat placeholder as a constant
uint32_t index = (uint32_t) (indexSigned < 0 ? (int32_t) instance.config.display.constants.length + indexSigned : indexSigned - 1);
if (*pend != '\0' || instance.config.display.constants.length <= index)
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
else
{
FFstrbuf* item = FF_LIST_GET(FFstrbuf, instance.config.display.constants, index);
ffStrbufAppend(buffer, item);
}
}
continue;
}
2024-09-11 11:07:00 +08:00
char* pSep = placeholderValue.chars;
char cSep = '\0';
while (*pSep && *pSep != ':' && *pSep != '<' && *pSep != '>' && *pSep != '~')
++pSep;
if (*pSep)
2024-09-02 15:54:22 +08:00
{
2024-09-11 11:07:00 +08:00
cSep = *pSep;
*pSep = '\0';
2024-09-02 15:54:22 +08:00
}
2024-09-11 11:07:00 +08:00
else
2024-06-26 16:14:34 +08:00
{
2024-09-11 11:07:00 +08:00
pSep = NULL;
2024-06-26 16:14:34 +08:00
}
uint32_t index = getArgumentIndex(placeholderValue.chars, numArgs, arguments);
2021-04-24 21:42:48 +02:00
// test for invalid index
2024-06-26 16:14:34 +08:00
if (index == 0)
index = ++argCounter;
2024-05-27 16:41:37 +08:00
if (index > numArgs)
2021-04-24 21:42:48 +02:00
{
2024-09-11 11:07:00 +08:00
if (pSep) *pSep = cSep;
2021-04-24 21:42:48 +02:00
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
continue;
2021-04-09 14:07:05 +02:00
}
2024-09-11 11:07:00 +08:00
if (!cSep)
ffFormatAppendFormatArg(buffer, &arguments[index - 1]);
else if (cSep == '~')
2024-06-26 16:14:34 +08:00
{
2024-09-11 11:07:00 +08:00
FF_STRBUF_AUTO_DESTROY tempString = ffStrbufCreate();
ffFormatAppendFormatArg(&tempString, &arguments[index - 1]);
2024-06-26 16:14:34 +08:00
2024-09-11 11:07:00 +08:00
char* pEnd = NULL;
int32_t start = (int32_t) strtol(pSep + 1, &pEnd, 10);
if (start < 0)
start = (int32_t) tempString.length + start;
if (start >= 0 && (uint32_t) start < tempString.length)
{
if (*pEnd == '\0')
ffStrbufAppendNS(buffer, tempString.length - (uint32_t) start, &tempString.chars[start]);
else if (*pEnd == ',')
{
int32_t end = (int32_t) strtol(pEnd + 1, &pEnd, 10);
if (!*pEnd)
{
if (end < 0)
end = (int32_t) tempString.length + end;
if ((uint32_t) end > tempString.length)
end = (int32_t) tempString.length;
if (end > start)
ffStrbufAppendNS(buffer, (uint32_t) (end - start), &tempString.chars[start]);
}
}
}
if (*pEnd)
{
*pSep = cSep;
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
continue;
}
}
2024-09-02 15:54:22 +08:00
else
2024-06-26 16:14:34 +08:00
{
2024-09-11 11:07:00 +08:00
char* pEnd = NULL;
int32_t truncLength = (int32_t) strtol(pSep + 1, &pEnd, 10);
if (*pEnd != '\0')
{
*pSep = cSep;
appendInvalidPlaceholder(buffer, "{", &placeholderValue, i, formatstr->length);
continue;
}
bool ellipsis = false;
if (truncLength < 0)
{
ellipsis = true;
truncLength = -truncLength;
}
FF_STRBUF_AUTO_DESTROY tempString = ffStrbufCreate();
ffFormatAppendFormatArg(&tempString, &arguments[index - 1]);
if (tempString.length == (uint32_t) truncLength)
ffStrbufAppend(buffer, &tempString);
else if (tempString.length > (uint32_t) truncLength)
2024-09-02 15:54:22 +08:00
{
2024-09-11 11:07:00 +08:00
if (cSep == ':')
2024-09-02 15:54:22 +08:00
{
2024-09-11 11:07:00 +08:00
ffStrbufSubstrBefore(&tempString, (uint32_t) truncLength);
ffStrbufTrimRightSpace(&tempString);
2024-09-02 15:54:22 +08:00
}
else
2024-09-11 11:07:00 +08:00
ffStrbufSubstrBefore(&tempString, (uint32_t) (!ellipsis? truncLength : truncLength - 1));
ffStrbufAppend(buffer, &tempString);
2024-09-02 15:54:22 +08:00
if (ellipsis)
ffStrbufAppendS(buffer, "…");
}
2024-09-11 11:07:00 +08:00
else if (cSep == ':')
ffStrbufAppend(buffer, &tempString);
2024-09-02 15:54:22 +08:00
else
{
2024-09-11 11:07:00 +08:00
if (cSep == '<')
2024-09-02 15:54:22 +08:00
{
2024-09-11 11:07:00 +08:00
ffStrbufAppend(buffer, &tempString);
ffStrbufAppendNC(buffer, (uint32_t) truncLength - tempString.length, ' ');
2024-09-02 15:54:22 +08:00
}
else
{
2024-09-11 11:07:00 +08:00
ffStrbufAppendNC(buffer, (uint32_t) truncLength - tempString.length, ' ');
ffStrbufAppend(buffer, &tempString);
2024-09-02 15:54:22 +08:00
}
}
2024-06-26 16:14:34 +08:00
}
2021-04-09 14:07:05 +02:00
}
2024-05-28 10:56:01 +08:00
if (!instance.config.display.pipe)
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
2021-04-09 14:07:05 +02:00
}