Files
fastfetch/src/common/impl/printing.c
T

158 lines
5.7 KiB
C
Raw Normal View History

2022-04-08 15:50:49 +02:00
#include "fastfetch.h"
2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/textModifier.h"
2026-01-05 16:19:47 +08:00
#include "logo/logo.h"
2022-04-08 15:50:49 +02:00
2026-03-29 09:28:49 +08:00
void ffPrintLogoAndKey(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType) {
ffLogoPrintLine();
2022-06-06 14:30:06 +02:00
2026-03-29 09:28:49 +08:00
// This is used by --set-keyless, in this case we want neither the module name nor the separator
2026-07-10 18:08:04 +08:00
if (moduleName == nullptr) {
2022-08-18 10:44:21 +02:00
return;
2026-03-29 09:28:49 +08:00
}
2022-08-18 10:44:21 +02:00
2026-03-29 09:28:49 +08:00
// This is used as a magic value for hiding keys
if (!(moduleArgs && ffStrbufEqualS(&moduleArgs->key, " ")) && instance.config.display.keyType != FF_MODULE_KEY_TYPE_NONE) {
2024-07-24 15:12:25 +08:00
ffPrintCharTimes(' ', instance.config.display.keyPaddingLeft);
2026-03-29 09:28:49 +08:00
if (!instance.config.display.pipe) {
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
2026-03-29 09:28:49 +08:00
if (instance.config.display.brightColor) {
fputs(FASTFETCH_TEXT_MODIFIER_BOLT, stdout);
2026-03-29 09:28:49 +08:00
}
2023-08-04 16:05:25 +08:00
2026-03-29 09:28:49 +08:00
if (moduleArgs && !(printType & FF_PRINT_TYPE_NO_CUSTOM_KEY_COLOR) && moduleArgs->keyColor.length > 0) {
ffPrintColor(&moduleArgs->keyColor);
2026-03-29 09:28:49 +08:00
} else {
ffPrintColor(&instance.config.display.colorKeys);
2026-03-29 09:28:49 +08:00
}
}
2022-06-06 14:30:06 +02:00
2026-03-29 09:28:49 +08:00
if (instance.config.display.keyType & FF_MODULE_KEY_TYPE_ICON && moduleArgs && moduleArgs->keyIcon.length > 0) {
2024-07-24 14:05:46 +08:00
ffStrbufWriteTo(&moduleArgs->keyIcon, stdout);
2026-03-29 09:28:49 +08:00
}
2024-07-24 11:13:37 +08:00
2026-03-29 09:28:49 +08:00
if (instance.config.display.keyType & FF_MODULE_KEY_TYPE_STRING) {
ffPrintCharTimes(' ', instance.config.display.keyType >> FF_MODULE_KEY_TYPE_SPACE_SHIFT);
2024-07-24 11:13:37 +08:00
2026-07-10 18:08:04 +08:00
// nullptr check is required for modules with custom keys, e.g. disk with the folder path
2026-03-29 09:28:49 +08:00
if ((printType & FF_PRINT_TYPE_NO_CUSTOM_KEY) || !moduleArgs || moduleArgs->key.length == 0) {
2024-07-24 11:13:37 +08:00
fputs(moduleName, stdout);
2026-03-29 09:28:49 +08:00
if (moduleIndex > 0) {
2024-07-24 11:13:37 +08:00
printf(" %hhu", moduleIndex);
2026-03-29 09:28:49 +08:00
}
} else {
2024-07-24 11:13:37 +08:00
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
2024-12-11 10:47:02 +08:00
FF_PARSE_FORMAT_STRING_CHECKED(&key, &moduleArgs->key, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(moduleIndex, "index"),
FF_ARG(moduleArgs->keyIcon, "icon"),
FF_ARG(moduleName, "module-name"),
2026-03-29 09:28:49 +08:00
}));
2024-07-24 11:13:37 +08:00
ffStrbufWriteTo(&key, stdout);
}
}
2022-06-06 14:30:06 +02:00
2026-03-29 09:28:49 +08:00
if (!instance.config.display.pipe) {
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);
2026-03-29 09:28:49 +08:00
if (!instance.config.display.pipe && instance.config.display.colorSeparator.length) {
2024-05-28 16:13:06 +08:00
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
2026-03-29 09:28:49 +08:00
}
2024-05-28 16:13:06 +08:00
2026-03-29 09:28:49 +08:00
if (!(printType & FF_PRINT_TYPE_NO_CUSTOM_KEY_WIDTH)) {
uint32_t keyWidth = moduleArgs && moduleArgs->keyWidth > 0 ? moduleArgs->keyWidth : instance.config.display.keyWidth;
2026-03-29 09:28:49 +08:00
if (keyWidth > 0) {
printf("\e[%uG", (unsigned) (keyWidth + instance.state.logoWidth));
2026-03-29 09:28:49 +08:00
}
}
}
2022-06-06 14:30:06 +02:00
2026-03-29 09:28:49 +08:00
if (!instance.config.display.pipe) {
2022-06-06 14:30:06 +02:00
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
2026-03-29 09:28:49 +08:00
if (moduleArgs && moduleArgs->outputColor.length) {
ffPrintColor(&moduleArgs->outputColor);
2026-03-29 09:28:49 +08:00
} else if (instance.config.display.colorOutput.length) {
ffPrintColor(&instance.config.display.colorOutput);
2026-03-29 09:28:49 +08:00
}
}
2022-06-06 14:30:06 +02:00
}
2026-05-22 09:26:52 +08:00
bool ffPrintFormat(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, uint32_t numArgs, const FFformatarg* arguments) {
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
2026-05-22 09:26:52 +08:00
bool success;
2026-07-10 18:08:04 +08:00
if (__builtin_expect(moduleArgs != nullptr, 1)) {
2026-05-22 09:26:52 +08:00
success = ffParseFormatString(&buffer, &moduleArgs->outputFormat, numArgs, arguments);
2026-03-29 09:28:49 +08:00
} else {
2026-05-22 09:26:52 +08:00
ffStrbufSetStatic(&buffer, "undefined format");
success = false;
2026-03-29 09:28:49 +08:00
}
2022-04-08 15:50:49 +02:00
2026-05-22 09:26:52 +08:00
if (success) {
ffPrintLogoAndKey(moduleName, moduleIndex, moduleArgs, printType);
ffStrbufPutTo(&buffer, stdout);
2026-05-22 09:26:52 +08:00
} else {
ffPrintError(moduleName, moduleIndex, moduleArgs, printType, "%s", buffer.chars);
}
2026-05-22 09:26:52 +08:00
return success;
2022-06-17 18:15:36 +02:00
}
2022-04-08 15:50:49 +02:00
2026-03-29 09:28:49 +08:00
void ffPrintError(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, const char* message, ...) {
if (!instance.config.display.showErrors) {
2022-06-17 18:15:36 +02:00
return;
2026-03-29 09:28:49 +08:00
}
2022-04-08 15:50:49 +02:00
ffPrintLogoAndKey(moduleName, moduleIndex, moduleArgs, printType);
2022-06-17 18:15:36 +02:00
2026-03-29 09:28:49 +08:00
if (!instance.config.display.pipe) {
2023-06-18 11:14:50 +08:00
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stdout);
2026-03-29 09:28:49 +08:00
}
2022-06-17 18:15:36 +02:00
va_list arguments;
va_start(arguments, message);
2023-06-18 11:14:50 +08:00
vprintf(message, arguments);
va_end(arguments);
2022-06-17 18:15:36 +02:00
2026-03-29 09:28:49 +08:00
if (!instance.config.display.pipe) {
2023-06-18 11:14:50 +08:00
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
2026-03-29 09:28:49 +08:00
}
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
2026-03-29 09:28:49 +08:00
void ffPrintColor(const FFstrbuf* colorValue) {
// 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) {
2022-06-08 10:59:33 +02:00
return;
2026-03-29 09:28:49 +08:00
}
2022-06-08 10:59:33 +02:00
2023-09-15 10:53:09 +08:00
printf("\e[%sm", colorValue->chars);
2022-04-08 15:50:49 +02:00
}
2026-03-29 09:28:49 +08:00
void ffPrintCharTimes(char c, uint32_t times) {
if (times == 0) {
2023-01-01 17:24:37 +08:00
return;
2026-03-29 09:28:49 +08:00
}
2023-01-01 17:24:37 +08:00
2026-03-29 09:28:49 +08:00
if (times == 1) {
putchar(c);
return;
}
char str[32];
2026-03-29 09:28:49 +08:00
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);
2026-03-29 09:28:49 +08:00
}
uint32_t remaining = times % sizeof(str);
2026-03-29 09:28:49 +08:00
if (remaining > 0) {
fwrite(str, 1, remaining, stdout);
2026-03-29 09:28:49 +08:00
}
2022-04-08 15:50:49 +02:00
}