Files
fastfetch/src/common/printing.c
T

175 lines
5.2 KiB
C
Raw Normal View History

2022-04-08 15:50:49 +02:00
#include "fastfetch.h"
#include "common/printing.h"
2022-12-06 17:03:31 +08:00
#include "util/textModifier.h"
2022-04-08 15:50:49 +02:00
2022-06-06 14:30:06 +02:00
void ffPrintLogoAndKey(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat)
{
2022-06-08 10:59:33 +02:00
ffLogoPrintLine(instance);
2022-06-06 14:30:06 +02:00
2022-08-18 10:44:21 +02:00
//This is used by --set-keyless, in this case we wan't neither the module name nor the separator
if(moduleName == NULL)
return;
2022-06-06 14:30:06 +02:00
if(!instance->config.pipe)
{
fputs(FASTFETCH_TEXT_MODIFIER_RESET FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
2022-09-07 19:50:10 +02:00
ffPrintColor(&instance->config.colorKeys);
2022-06-06 14:30:06 +02:00
}
2022-06-17 18:15:36 +02:00
//NULL check is required for modules with custom keys, e.g. disk with the folder path
2022-06-06 14:30:06 +02:00
if(customKeyFormat == NULL || customKeyFormat->length == 0)
{
fputs(moduleName, stdout);
if(moduleIndex > 0)
printf(" %hhu", moduleIndex);
}
else
{
FFstrbuf key;
ffStrbufInit(&key);
2022-06-17 18:15:36 +02:00
ffParseFormatString(&key, customKeyFormat, 1, (FFformatarg[]){
2022-06-06 14:30:06 +02:00
{FF_FORMAT_ARG_TYPE_UINT8, &moduleIndex}
});
2022-08-18 11:36:30 +02:00
ffPrintUserString(key.chars);
2022-06-06 14:30:06 +02:00
ffStrbufDestroy(&key);
}
if(!instance->config.pipe)
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
ffStrbufWriteTo(&instance->config.separator, stdout);
if(!instance->config.pipe)
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
}
2022-06-17 18:15:36 +02:00
void ffPrintFormatString(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* format, uint32_t numArgs, const FFformatarg* arguments)
2022-04-08 15:50:49 +02:00
{
2022-06-17 18:15:36 +02:00
FFstrbuf buffer;
ffStrbufInitA(&buffer, 256);
2022-04-08 15:50:49 +02:00
2022-06-17 18:15:36 +02:00
ffParseFormatString(&buffer, format, numArgs, arguments);
2022-04-08 15:50:49 +02:00
2022-06-17 18:15:36 +02:00
if(buffer.length > 0)
2022-04-08 15:50:49 +02:00
{
ffPrintLogoAndKey(instance, moduleName, moduleIndex, customKeyFormat);
2022-08-18 11:36:30 +02:00
ffPrintUserString(buffer.chars);
putchar('\n');
2022-04-08 15:50:49 +02:00
}
2022-06-17 18:15:36 +02:00
ffStrbufDestroy(&buffer);
}
2022-04-08 15:50:49 +02:00
2022-06-17 18:15:36 +02:00
void ffPrintFormat(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, uint32_t numArgs, const FFformatarg* arguments)
{
ffPrintFormatString(instance, moduleName, moduleIndex, &moduleArgs->key, &moduleArgs->outputFormat, numArgs, arguments);
2022-04-08 15:50:49 +02:00
}
2022-06-17 18:15:36 +02:00
static void printError(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* customErrorFormat, const char* message, va_list arguments)
2022-04-08 15:50:49 +02:00
{
2022-06-17 18:15:36 +02:00
bool hasCustomErrorFormat = customErrorFormat != NULL && customErrorFormat->length > 0;
if(!hasCustomErrorFormat && !instance->config.showErrors)
return;
2022-04-08 15:50:49 +02:00
2022-06-17 18:15:36 +02:00
if(hasCustomErrorFormat)
{
FFstrbuf error;
ffStrbufInit(&error);
ffStrbufAppendVF(&error, message, arguments);
2022-04-08 15:50:49 +02:00
2022-06-17 18:15:36 +02:00
ffPrintFormatString(instance, moduleName, moduleIndex, customKeyFormat, customErrorFormat, 1, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &error}
});
ffStrbufDestroy(&error);
}
else
2022-04-08 15:50:49 +02:00
{
ffPrintLogoAndKey(instance, moduleName, moduleIndex, customKeyFormat);
2022-06-17 18:15:36 +02:00
if(!instance->config.pipe)
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stdout);
vprintf(message, arguments);
if(!instance->config.pipe)
2022-10-17 13:19:48 +02:00
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
putchar('\n');
2022-04-08 15:50:49 +02:00
}
2022-06-17 18:15:36 +02:00
}
2022-04-08 15:50:49 +02:00
2022-06-17 18:15:36 +02:00
void ffPrintErrorString(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat, const FFstrbuf* customErrorFormat, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
printError(instance, moduleName, moduleIndex, customKeyFormat, customErrorFormat, message, arguments);
va_end(arguments);
}
void ffPrintError(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, const char* message, ...)
{
va_list arguments;
va_start(arguments, message);
printError(instance, moduleName, moduleIndex, &moduleArgs->key, &moduleArgs->errorFormat, message, arguments);
va_end(arguments);
2022-04-08 15:50:49 +02:00
}
void ffPrintColor(const FFstrbuf* colorValue)
{
2022-06-08 10:59:33 +02:00
//If the color is not set, this would reset in \033[m, which resets everything.
//So we only print it, if the main color is at least one char.
if(colorValue->length == 0)
return;
2022-04-08 15:50:49 +02:00
fputs("\033[", stdout);
ffStrbufWriteTo(colorValue, stdout);
fputc('m', stdout);
}
void ffPrintCharTimes(char c, uint32_t times)
{
2023-01-01 17:24:37 +08:00
if(times == 0)
return;
char str[32];
memset(str, c, sizeof(str)); //2 instructions when compiling with AVX2 enabled
for(uint32_t i = sizeof(str); i <= times; i += sizeof(str))
fwrite(str, 1, sizeof(str), stdout);
uint32_t remaining = times % sizeof(str);
if(remaining > 0)
fwrite(str, 1, remaining, stdout);
2022-04-08 15:50:49 +02:00
}
2022-08-18 11:36:30 +02:00
void ffPrintUserString(const char* value)
{
while(*value != '\0')
{
if(*value != '\\')
{
putchar(*value);
++value;
continue;
}
++value;
if(*value == 'n')
putchar('\n');
else if(*value == 't')
putchar('\t');
else if(*value == 'e')
putchar('\e');
else if(*value == '\\')
putchar('\\');
else
{
putchar('\\');
putchar(*value);
}
++value;
}
}