Files
fastfetch/src/common/printing.c
T

164 lines
5.8 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
void ffPrintLogoAndKey(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType)
2022-06-06 14:30:06 +02:00
{
ffLogoPrintLine();
2022-06-06 14:30:06 +02:00
//This is used by --set-keyless, in this case we want neither the module name nor the separator
2022-08-18 10:44:21 +02:00
if(moduleName == NULL)
return;
//This is used as a magic value for hiding keys
2024-07-24 11:13:37 +08:00
if (!(moduleArgs && ffStrbufEqualS(&moduleArgs->key, " ")) && instance.config.display.keyType != FF_MODULE_KEY_TYPE_NONE)
2022-06-06 14:30:06 +02:00
{
if(!instance.config.display.pipe)
{
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
if (instance.config.display.brightColor)
fputs(FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
2023-08-04 16:05:25 +08:00
if(moduleArgs && !(printType & FF_PRINT_TYPE_NO_CUSTOM_KEY_COLOR) && moduleArgs->keyColor.length > 0)
ffPrintColor(&moduleArgs->keyColor);
else
ffPrintColor(&instance.config.display.colorKeys);
}
2022-06-06 14:30:06 +02:00
2024-07-24 11:13:37 +08:00
bool hasIcon = false;
if (instance.config.display.keyType & FF_MODULE_KEY_TYPE_ICON && moduleArgs && moduleArgs->icon.length > 0)
{
2024-07-24 11:13:37 +08:00
ffStrbufWriteTo(&moduleArgs->icon, stdout);
hasIcon = true;
}
2024-07-24 11:13:37 +08:00
if (instance.config.display.keyType & FF_MODULE_KEY_TYPE_STRING)
{
2024-07-24 11:13:37 +08:00
if(hasIcon)
putchar(' ');
//NULL check is required for modules with custom keys, e.g. disk with the folder path
if((printType & FF_PRINT_TYPE_NO_CUSTOM_KEY) || !moduleArgs || moduleArgs->key.length == 0)
{
fputs(moduleName, stdout);
if(moduleIndex > 0)
printf(" %hhu", moduleIndex);
}
else
{
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
FF_PARSE_FORMAT_STRING_CHECKED(&key, &moduleArgs->key, 2, ((FFformatarg[]){
{FF_FORMAT_ARG_TYPE_UINT8, &moduleIndex, "index"},
{FF_FORMAT_ARG_TYPE_STRBUF, &moduleArgs->icon, "icon"},
}));
ffStrbufWriteTo(&key, stdout);
}
}
2022-06-06 14:30:06 +02:00
if(!instance.config.display.pipe)
2024-05-28 16:13:06 +08:00
{
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
2024-05-28 16:13:06 +08:00
ffPrintColor(&instance.config.display.colorSeparator);
}
2022-06-06 14:30:06 +02:00
ffStrbufWriteTo(&instance.config.display.keyValueSeparator, stdout);
2024-05-28 16:13:06 +08:00
if(!instance.config.display.pipe && instance.config.display.colorSeparator.length)
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
if (!(printType & FF_PRINT_TYPE_NO_CUSTOM_KEY_WIDTH))
{
uint32_t keyWidth = moduleArgs && moduleArgs->keyWidth > 0 ? moduleArgs->keyWidth : instance.config.display.keyWidth;
if (keyWidth > 0)
printf("\e[%uG", (unsigned) (keyWidth + instance.state.logoWidth));
}
}
2022-06-06 14:30:06 +02:00
2023-10-26 10:26:15 +08:00
if(!instance.config.display.pipe)
{
2022-06-06 14:30:06 +02:00
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
if (moduleArgs && moduleArgs->outputColor.length)
ffPrintColor(&moduleArgs->outputColor);
else if (instance.config.display.colorOutput.length)
ffPrintColor(&instance.config.display.colorOutput);
}
2022-06-06 14:30:06 +02:00
}
void ffPrintFormat(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, uint32_t numArgs, const FFformatarg* arguments)
2022-04-08 15:50:49 +02:00
{
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
if (moduleArgs)
ffParseFormatString(&buffer, &moduleArgs->outputFormat, numArgs, arguments);
else
ffStrbufAppendS(&buffer, "unknown");
2022-04-08 15:50:49 +02:00
ffPrintLogoAndKey(moduleName, moduleIndex, moduleArgs, printType);
ffStrbufPutTo(&buffer, stdout);
2022-06-17 18:15:36 +02:00
}
2022-04-08 15:50:49 +02:00
static void printError(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, const char* message, va_list arguments)
2022-04-08 15:50:49 +02:00
{
2023-10-26 10:26:15 +08:00
if(!instance.config.display.showErrors)
2022-06-17 18:15:36 +02:00
return;
2022-04-08 15:50:49 +02:00
ffPrintLogoAndKey(moduleName, moduleIndex, moduleArgs, printType);
2022-06-17 18:15:36 +02:00
2023-10-26 10:26:15 +08:00
if(!instance.config.display.pipe)
2023-06-18 11:14:50 +08:00
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stdout);
2022-06-17 18:15:36 +02:00
2023-06-18 11:14:50 +08:00
vprintf(message, arguments);
2022-06-17 18:15:36 +02:00
2023-10-26 10:26:15 +08:00
if(!instance.config.display.pipe)
2023-06-18 11:14:50 +08:00
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
2022-10-17 13:19:48 +02:00
2023-06-18 11:14:50 +08:00
putchar('\n');
2022-06-17 18:15:36 +02:00
}
2022-04-08 15:50:49 +02:00
void ffPrintError(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, const char* message, ...)
2022-06-17 18:15:36 +02:00
{
va_list arguments;
va_start(arguments, message);
printError(moduleName, moduleIndex, moduleArgs, printType, message, arguments);
2022-06-17 18:15:36 +02:00
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;
2023-09-15 10:53:09 +08:00
printf("\e[%sm", colorValue->chars);
2022-04-08 15:50:49 +02:00
}
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 += (uint32_t)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
}
2023-10-07 13:40:40 +08:00
void ffPrintModuleFormatHelp(const char* name, const char* def, uint32_t numArgs, const char* args[])
{
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateS(name);
ffStrbufLowerCase(&buffer);
printf("--%s-format:\n", buffer.chars);
printf("Sets the format string for %s output.\n", name);
puts("To see how a format string is constructed, take a look at \"fastfetch --help format\".");
puts("The following values are passed:");
for(unsigned i = 0; i < numArgs; i++)
2023-10-08 09:46:40 +08:00
printf(" {%u}: %s\n", i + 1, args[i]);
2023-10-07 13:40:40 +08:00
printf("The default is something similar to \"%s\".\n", def);
}