Colors: support --colors-symbol

This commit is contained in:
Carter Li
2023-06-18 14:00:24 +08:00
parent 35e57741d3
commit ba45229db0
12 changed files with 156 additions and 20 deletions
+1
View File
@@ -13,6 +13,7 @@ Features:
* Support MATE Terminal version & terminal font detection
* Add `--no-buffer` option for easier debugging. CMake option `ENABLE_BUFFER` is removed and always enabled.
* Support `--*-key-color` option to change the key color of specified module
* Support `--colors-symbol`
# 1.11.3
+2
View File
@@ -102,6 +102,7 @@ static void defaultConfig(FFinstance* instance)
ffInitSoundOptions(&instance->config.sound);
ffInitSeparatorOptions(&instance->config.separator);
ffInitGamepadOptions(&instance->config.gamepad);
ffInitColorsOptions(&instance->config.colors);
ffStrbufInit(&instance->config.libPCI);
ffStrbufInit(&instance->config.libVulkan);
@@ -306,6 +307,7 @@ static void destroyConfig(FFinstance* instance)
ffDestroySeparatorOptions(&instance->config.separator);
ffDestroySoundOptions(&instance->config.sound);
ffDestroyGamepadOptions(&instance->config.gamepad);
ffDestroyColorsOptions(&instance->config.colors);
ffStrbufDestroy(&instance->config.libPCI);
ffStrbufDestroy(&instance->config.libVulkan);
+1
View File
@@ -136,6 +136,7 @@ Module specific options:
--command-shell <str>: Set the shell program to execute the command text. Default is cmd for Windows, csh for FreeBSD, bash for others
--command-key <str>: Set the module key to display
--command-text <str>: Set the command text to be executed
--colors-symbol <str>: Set the symbol to be printed by Colors module. Default is block
Parsing is not case sensitive. E.g. "--lib-PCI" is equal to "--Lib-Pci"
If a value starts with a ?, it is optional. "true" will be used if not set.
+21 -1
View File
@@ -436,7 +436,7 @@
{
"properties": {
"type": {
"enum": ["Break", "Colors"]
"enum": ["Break"]
}
},
"additionalProperties": false
@@ -547,6 +547,26 @@
},
"additionalProperties": false
},
{
"properties": {
"type": {
"enum": ["Colors"]
},
"symbol": {
"title": "Set the symbol to use",
"type": "string",
"enum": [
"block",
"circle",
"diamond",
"triangle",
"square",
"star"
],
"default": "blocks"
}
}
},
{
"properties": {
"type": {
+1 -1
View File
@@ -1153,7 +1153,7 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
else if(strcasecmp(line, FF_DATETIME_MODULE_NAME) == 0)
ffPrintDateTime(instance, &instance->config.dateTime);
else if(strcasecmp(line, FF_COLORS_MODULE_NAME) == 0)
ffPrintColors(instance);
ffPrintColors(instance, &instance->config.colors);
else if(strcasecmp(line, FF_VULKAN_MODULE_NAME) == 0)
ffPrintVulkan(instance, &instance->config.vulkan);
else if(strcasecmp(line, FF_OPENGL_MODULE_NAME) == 0)
+1
View File
@@ -104,6 +104,7 @@ typedef struct FFconfig
FFSeparatorOptions separator;
FFSoundOptions sound;
FFGamepadOptions gamepad;
FFColorsOptions colors;
FFstrbuf libPCI;
FFstrbuf libVulkan;
+1 -1
View File
@@ -69,7 +69,7 @@ int main(int argc, char** argv)
//ffPrintSound(&instance, &instance.config.sound);
//ffPrintGamepad(&instance);
ffPrintBreak(&instance);
ffPrintColors(&instance);
ffPrintColors(&instance, &instance.config.colors);
ffFinish(&instance);
ffDestroyInstance(&instance);
+102 -16
View File
@@ -3,34 +3,120 @@
#include "util/textModifier.h"
#include "modules/colors/colors.h"
void ffPrintColors(FFinstance* instance)
void ffPrintColors(FFinstance* instance, FFColorsOptions* options)
{
if(instance->config.pipe)
return;
ffLogoPrintLine(instance);
// 4%d: Set the background color
// 3%d: Set the foreground color
for(uint8_t i = 0; i < 8; i++)
printf("\033[4%d;3%dm███", i, i);
if (options->symbol == FF_COLORS_SYMBOL_BLOCK)
{
// 4%d: Set the background color
// 3%d: Set the foreground color
for(uint8_t i = 0; i < 8; i++)
printf("\033[4%d;3%dm███", i, i);
puts(FASTFETCH_TEXT_MODIFIER_RESET);
puts(FASTFETCH_TEXT_MODIFIER_RESET);
ffLogoPrintLine(instance);
ffLogoPrintLine(instance);
// 1: Set everything to bolt. This causes normal colors on some systems to be bright.
// 4%d: Set the backgound to the not bright color
// 3%d: Set the foreground to the not bright color
// 10%d: Set the background to the bright color
// 9%d: Set the foreground to the bright color
for(uint8_t i = 0; i < 8; i++)
printf("\033[1;4%d;3%d;10%d;9%dm███", i, i, i, i);
// 1: Set everything to bolt. This causes normal colors on some systems to be bright.
// 4%d: Set the backgound to the not bright color
// 3%d: Set the foreground to the not bright color
// 10%d: Set the background to the bright color
// 9%d: Set the foreground to the bright color
for(uint8_t i = 0; i < 8; i++)
printf("\033[1;4%d;3%d;10%d;9%dm███", i, i, i, i);
}
else
{
const char* symbol;
switch (options->symbol)
{
case FF_COLORS_SYMBOL_CIRCLE: symbol = ""; break;
case FF_COLORS_SYMBOL_DIAMOND: symbol = ""; break;
case FF_COLORS_SYMBOL_TRIANGLE: symbol = ""; break;
case FF_COLORS_SYMBOL_SQUARE: symbol = ""; break;
case FF_COLORS_SYMBOL_STAR: symbol = ""; break;
default: symbol = "███"; break;
}
for (int i = 8; i >= 1; --i)
printf("\e[3%dm%s ", i, symbol);
}
puts(FASTFETCH_TEXT_MODIFIER_RESET);
}
void ffParseColorsJsonObject(FFinstance* instance, FF_MAYBE_UNUSED yyjson_val* module)
void ffInitColorsOptions(FFColorsOptions* options)
{
return ffPrintColors(instance);
options->moduleName = FF_COLORS_MODULE_NAME;
options->symbol = FF_COLORS_SYMBOL_BLOCK;
}
bool ffParseColorsCommandOptions(FFColorsOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_COLORS_MODULE_NAME);
if (!subKey) return false;
if (strcasecmp(subKey, "symbol") == 0)
{
options->symbol = (FFColorssymbol) ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
{ "block", FF_COLORS_SYMBOL_BLOCK },
{ "circle", FF_COLORS_SYMBOL_CIRCLE },
{ "diamond", FF_COLORS_SYMBOL_DIAMOND },
{ "triangle", FF_COLORS_SYMBOL_TRIANGLE },
{ "square", FF_COLORS_SYMBOL_SQUARE },
{ "star", FF_COLORS_SYMBOL_STAR },
{},
});
return true;
}
return false;
}
void ffDestroyColorsOptions(FF_MAYBE_UNUSED FFColorsOptions* options)
{
}
void ffParseColorsJsonObject(FFinstance* instance, yyjson_val* module)
{
FFColorsOptions __attribute__((__cleanup__(ffDestroyColorsOptions))) options;
ffInitColorsOptions(&options);
if (module)
{
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
{
const char* key = yyjson_get_str(key_);
if(strcasecmp(key, "type") == 0)
continue;
if (strcasecmp(key, "symbol") == 0)
{
int value;
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
{ "block", FF_COLORS_SYMBOL_BLOCK },
{ "circle", FF_COLORS_SYMBOL_CIRCLE },
{ "diamond", FF_COLORS_SYMBOL_DIAMOND },
{ "triangle", FF_COLORS_SYMBOL_TRIANGLE },
{ "square", FF_COLORS_SYMBOL_SQUARE },
{ "star", FF_COLORS_SYMBOL_STAR },
{},
});
if (error)
ffPrintErrorString(instance, FF_COLORS_MODULE_NAME, 0, NULL, NULL, "Invalid %s value: %s", key, error);
else
options.symbol = (FFColorssymbol) value;
continue;
}
ffPrintErrorString(instance, FF_COLORS_MODULE_NAME, 0, NULL, NULL, "Unknown JSON key %s", key);
}
}
ffPrintColors(instance, &options);
}
+3 -1
View File
@@ -4,5 +4,7 @@
#define FF_COLORS_MODULE_NAME "Colors"
void ffPrintColors(FFinstance* instance);
void ffPrintColors(FFinstance* instance, FFColorsOptions* options);
void ffInitColorsOptions(FFColorsOptions* options);
void ffDestroyColorsOptions(FFColorsOptions* options);
void ffParseColorsJsonObject(FFinstance* instance, yyjson_val* module);
+21
View File
@@ -0,0 +1,21 @@
#pragma once
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
#include "common/option.h"
typedef enum FFColorssymbol
{
FF_COLORS_SYMBOL_BLOCK,
FF_COLORS_SYMBOL_CIRCLE,
FF_COLORS_SYMBOL_DIAMOND,
FF_COLORS_SYMBOL_SQUARE,
FF_COLORS_SYMBOL_TRIANGLE,
FF_COLORS_SYMBOL_STAR,
} FFColorssymbol;
typedef struct FFColorsOptions
{
const char* moduleName;
FFColorssymbol symbol;
} FFColorsOptions;
+1
View File
@@ -157,6 +157,7 @@ void ffDestroyDisplayOptions(FFDisplayOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
void ffParseDisplayJsonObject(FFinstance* instance, yyjson_val* module)
{
FFDisplayOptions __attribute__((__cleanup__(ffDestroyDisplayOptions))) options;
+1
View File
@@ -10,6 +10,7 @@
#include "modules/chassis/option.h"
#include "modules/cpu/option.h"
#include "modules/cpuusage/option.h"
#include "modules/colors/option.h"
#include "modules/cursor/option.h"
#include "modules/custom/option.h"
#include "modules/command/option.h"