From af2c6a832e9eb6263616ad998184b28aa7ff1d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 27 Jul 2023 15:58:49 +0800 Subject: [PATCH] Fastfetch: add new option `--temperature-unit` --- doc/json_schema.json | 6 ++++++ src/common/init.c | 1 + src/common/jsonconfig.c | 15 +++++++++++++++ src/common/parsing.c | 16 ++++++++++++++++ src/common/parsing.h | 1 + src/data/help.txt | 1 + src/fastfetch.c | 12 ++++++++++++ src/fastfetch.h | 8 ++++++++ src/modules/battery/battery.c | 3 ++- src/modules/cpu/cpu.c | 22 ++++++++++++++-------- src/modules/gpu/gpu.c | 6 +++++- 11 files changed, 81 insertions(+), 10 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index 1d5aa740f..a4eed6218 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -314,6 +314,12 @@ "enum": ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], "default": "YB" }, + "temperatureUnit": { + "type": "string", + "title": "Set the unit of the temperature", + "enum": ["CELSIUS", "C", "FAHRENHEIT", "F", "KELVIN", "K"], + "default": "C" + }, "percentType": { "type": "number", "title": "Set the percentage output type. 1 for percentage number, 2 for bar, 3 for both, 6 for bar only, 9 for colored number", diff --git a/src/common/init.c b/src/common/init.c index ba8c173a2..1b5659f46 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -63,6 +63,7 @@ static void defaultConfig(void) instance.config.binaryPrefixType = FF_BINARY_PREFIX_TYPE_IEC; instance.config.sizeNdigits = 2; instance.config.sizeMaxPrefix = UINT8_MAX; + instance.config.temperatureUnit = FF_TEMPERATURE_UNIT_CELSIUS; instance.config.multithreading = true; instance.config.stat = false; instance.config.noBuffer = false; diff --git a/src/common/jsonconfig.c b/src/common/jsonconfig.c index 5fd490657..0b327ca4f 100644 --- a/src/common/jsonconfig.c +++ b/src/common/jsonconfig.c @@ -409,6 +409,21 @@ const char* ffParseDisplayJsonConfig(void) if (error) return error; config->sizeMaxPrefix = (uint8_t) value; } + else if (ffStrEqualsIgnCase(key, "temperatureUnit")) + { + int value; + const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { + { "CELSIUS", FF_TEMPERATURE_UNIT_CELSIUS }, + { "C", FF_TEMPERATURE_UNIT_CELSIUS }, + { "FAHRENHEIT", FF_TEMPERATURE_UNIT_FAHRENHEIT }, + { "F", FF_TEMPERATURE_UNIT_FAHRENHEIT }, + { "KELVIN", FF_TEMPERATURE_UNIT_KELVIN }, + { "K", FF_TEMPERATURE_UNIT_KELVIN }, + {}, + }); + if (error) return error; + config->temperatureUnit = (FFTemperatureUnit) value; + } else if (ffStrEqualsIgnCase(key, "percentType")) config->percentType = (uint32_t) yyjson_get_uint(val); else if (ffStrEqualsIgnCase(key, "noBuffer")) diff --git a/src/common/parsing.c b/src/common/parsing.c index 9df1d1046..55a2780fe 100644 --- a/src/common/parsing.c +++ b/src/common/parsing.c @@ -96,6 +96,22 @@ void ffParseSize(uint64_t bytes, FFstrbuf* result) } } +void ffParseTemperature(double celsius, FFstrbuf* buffer) +{ + switch (instance.config.temperatureUnit) + { + case FF_TEMPERATURE_UNIT_CELSIUS: + ffStrbufAppendF(buffer, "%.1f°C", celsius); + break; + case FF_TEMPERATURE_UNIT_FAHRENHEIT: + ffStrbufAppendF(buffer, "%.1f°F", celsius * 1.8 + 32); + break; + case FF_TEMPERATURE_UNIT_KELVIN: + ffStrbufAppendF(buffer, "%.1f K", celsius + 273.15); + break; + } +} + void ffParseGTK(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, const FFstrbuf* gtk4) { if(gtk2->length > 0 && gtk3->length > 0 && gtk4->length > 0) diff --git a/src/common/parsing.h b/src/common/parsing.h index 41ac3858e..deb555c01 100644 --- a/src/common/parsing.h +++ b/src/common/parsing.h @@ -23,5 +23,6 @@ void ffVersionToPretty(const FFVersion* version, FFstrbuf* pretty); int8_t ffVersionCompare(const FFVersion* version1, const FFVersion* version2); void ffParseSize(uint64_t bytes, FFstrbuf* result); +void ffParseTemperature(double celsius, FFstrbuf* buffer); #endif diff --git a/src/data/help.txt b/src/data/help.txt index 0870ec5d4..097fc518d 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -69,6 +69,7 @@ Display options: --no-buffer : Set if the stdout application buffer should be disabled. Default is false --size-ndigits : Set the number of digits to keep after the decimal point when formatting sizes --size-max-prefix : Set the largest binary prefix to use when formatting sizes. Default is YB + --temperature-unit : Set the unit of the temperature. Must be one of C, F and K. Default is C General module options: ---format : Set the format string to use for each specific module. diff --git a/src/fastfetch.c b/src/fastfetch.c index f201c7849..50ad5c3d8 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -935,6 +935,18 @@ static void parseOption(FFdata* data, const char* key, const char* value) {} }); } + else if(ffStrEqualsIgnCase(key, "--temperature-unit")) + { + instance.config.temperatureUnit = (FFTemperatureUnit) ffOptionParseEnum(key, value, (FFKeyValuePair[]) { + { "CELSIUS", FF_TEMPERATURE_UNIT_CELSIUS }, + { "C", FF_TEMPERATURE_UNIT_CELSIUS }, + { "FAHRENHEIT", FF_TEMPERATURE_UNIT_FAHRENHEIT }, + { "F", FF_TEMPERATURE_UNIT_FAHRENHEIT }, + { "KELVIN", FF_TEMPERATURE_UNIT_KELVIN }, + { "K", FF_TEMPERATURE_UNIT_KELVIN }, + {}, + }); + } else if(ffStrEqualsIgnCase(key, "--percent-type")) instance.config.percentType = ffOptionParseUInt32(key, value); else if(ffStrEqualsIgnCase(key, "--no-buffer")) diff --git a/src/fastfetch.h b/src/fastfetch.h index 85256e0ed..90cc4e9aa 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -24,6 +24,13 @@ typedef enum FFBinaryPrefixType FF_BINARY_PREFIX_TYPE_JEDEC, // 1024 Bytes = 1 kB, 1024 K = 1 MB, ... } FFBinaryPrefixType; +typedef enum FFTemperatureUnit +{ + FF_TEMPERATURE_UNIT_CELSIUS, + FF_TEMPERATURE_UNIT_FAHRENHEIT, + FF_TEMPERATURE_UNIT_KELVIN, +} FFTemperatureUnit; + typedef struct FFconfig { FFLogoOptions logo; @@ -43,6 +50,7 @@ typedef struct FFconfig FFBinaryPrefixType binaryPrefixType; uint8_t sizeNdigits; uint8_t sizeMaxPrefix; + FFTemperatureUnit temperatureUnit; bool pipe; //disables logo and all escape sequences bool multithreading; bool stat; diff --git a/src/modules/battery/battery.c b/src/modules/battery/battery.c index 297d30d50..5f5a93329 100644 --- a/src/modules/battery/battery.c +++ b/src/modules/battery/battery.c @@ -1,6 +1,7 @@ #include "common/printing.h" #include "common/jsonconfig.h" #include "common/bar.h" +#include "common/parsing.h" #include "detection/battery/battery.h" #include "modules/battery/battery.h" #include "util/stringUtils.h" @@ -54,7 +55,7 @@ static void printBattery(FFBatteryOptions* options, BatteryResult* result, uint8 if(str.length > 0) ffStrbufAppendS(&str, " - "); - ffStrbufAppendF(&str, "%.1f°C", result->temperature); + ffParseTemperature(result->temperature, &str); } ffStrbufPutTo(&str, stdout); diff --git a/src/modules/cpu/cpu.c b/src/modules/cpu/cpu.c index f4b41be5c..14d751685 100644 --- a/src/modules/cpu/cpu.c +++ b/src/modules/cpu/cpu.c @@ -1,5 +1,6 @@ #include "common/printing.h" #include "common/jsonconfig.h" +#include "common/parsing.h" #include "detection/cpu/cpu.h" #include "modules/cpu/cpu.h" #include "util/stringUtils.h" @@ -31,26 +32,31 @@ void ffPrintCPU(FFCPUOptions* options) { ffPrintLogoAndKey(FF_CPU_MODULE_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor); + FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate(); + if(cpu.name.length > 0) - ffStrbufWriteTo(&cpu.name, stdout); + ffStrbufAppend(&str, &cpu.name); else if(cpu.vendor.length > 0) { - ffStrbufWriteTo(&cpu.vendor, stdout); - fputs(" CPU", stdout); + ffStrbufAppend(&str, &cpu.vendor); + ffStrbufAppendS(&str, " CPU"); } else - fputs("CPU", stdout); + ffStrbufAppendS(&str, "Unknown"); if(cpu.coresOnline > 1) - printf(" (%u)", cpu.coresOnline); + ffStrbufAppendF(&str, " (%u)", cpu.coresOnline); if(cpu.frequencyMax > 0.0) - printf(" @ %.9g GHz", cpu.frequencyMax); + ffStrbufAppendF(&str, " @ %.9g GHz", cpu.frequencyMax); if(cpu.temperature == cpu.temperature) //FF_CPU_TEMP_UNSET - printf(" - %.1f°C", cpu.temperature); + { + ffStrbufAppendS(&str, " - "); + ffParseTemperature(cpu.temperature, &str); + } - putchar('\n'); + ffStrbufPutTo(&str, stdout); } else { diff --git a/src/modules/gpu/gpu.c b/src/modules/gpu/gpu.c index b0f03b45c..84a379f29 100644 --- a/src/modules/gpu/gpu.c +++ b/src/modules/gpu/gpu.c @@ -2,6 +2,7 @@ #include "common/parsing.h" #include "common/printing.h" #include "common/jsonconfig.h" +#include "common/parsing.h" #include "detection/host/host.h" #include "detection/gpu/gpu.h" #include "modules/gpu/gpu.h" @@ -39,7 +40,10 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu ffStrbufAppendF(&output, " (%d)", gpu->coreCount); if(gpu->temperature == gpu->temperature) //FF_GPU_TEMP_UNSET - ffStrbufAppendF(&output, " - %.1f°C", gpu->temperature); + { + ffStrbufAppendS(&output, " - "); + ffParseTemperature(gpu->temperature, &output); + } if(gpu->dedicated.total != FF_GPU_VMEM_SIZE_UNSET && gpu->dedicated.total != 0) {