Skip to content

Commit a0c4720

Browse files
committed
Temp: Display: support better temperature value formatting
Fix #737
1 parent 1c87693 commit a0c4720

File tree

10 files changed

+227
-51
lines changed

10 files changed

+227
-51
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# 2.8.6
2+
3+
Changes:
4+
* Due to newly introduced configs, JSONC option `{ "temperatureUnit": "C" }` has been changed to `{ "temperature": { "unit": "C" } }`
5+
6+
Bugfixes:
7+
* Fix incorrect GPU name detection for Intel iGPU on Linux (#736, GPU, Linux)
8+
9+
Features:
10+
* Support additional temperature formatting options (#737)
11+
* `{ "temperature": { "ndigits": 1 } }`
12+
* `{ "temperature": { "color": { "green": "green", "yellow": "yellow", "red": "red" } } }`
13+
114
# 2.8.5
215

316
Bugfixes:

doc/json_schema.json

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,42 @@
407407
}
408408
}
409409
},
410-
"temperatureUnit": {
411-
"type": "string",
412-
"description": "Set the unit of the temperature",
413-
"enum": ["CELSIUS", "C", "FAHRENHEIT", "F", "KELVIN", "K"],
414-
"default": "C"
410+
"temperature": {
411+
"type": "object",
412+
"description": "Set how a temperature value should be displayed",
413+
"properties": {
414+
"unit": {
415+
"type": "string",
416+
"description": "Set the unit of the temperature",
417+
"enum": ["CELSIUS", "C", "FAHRENHEIT", "F", "KELVIN", "K"],
418+
"default": "C"
419+
},
420+
"ndigits": {
421+
"type": "integer",
422+
"description": "Set the number of digits to keep after the decimal point when formatting temperature values",
423+
"minimum": 0,
424+
"maximum": 9,
425+
"default": 1
426+
},
427+
"color": {
428+
"type": "object",
429+
"description": "Set color used in different states of temperature values",
430+
"properties": {
431+
"green": {
432+
"description": "Color used in green state",
433+
"$ref": "#/$defs/colors"
434+
},
435+
"yellow": {
436+
"description": "Color used in yellow state",
437+
"$ref": "#/$defs/colors"
438+
},
439+
"red": {
440+
"description": "Color used in red state",
441+
"$ref": "#/$defs/colors"
442+
}
443+
}
444+
}
445+
}
415446
},
416447
"bar": {
417448
"type": "object",

src/common/parsing.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "fastfetch.h"
22
#include "common/parsing.h"
3+
#include "util/textModifier.h"
34

45
#include <ctype.h>
56
#include <inttypes.h>
@@ -98,18 +99,39 @@ void ffParseSize(uint64_t bytes, FFstrbuf* result)
9899

99100
void ffParseTemperature(double celsius, FFstrbuf* buffer)
100101
{
101-
switch (instance.config.display.temperatureUnit)
102+
if (celsius != celsius) // ignores NaN
103+
return;
104+
105+
const FFOptionsDisplay* options = &instance.config.display;
106+
const char* colorGreen = options->temperatureColorGreen.chars;
107+
const char* colorYellow = options->temperatureColorYellow.chars;
108+
const char* colorRed = options->temperatureColorRed.chars;
109+
110+
if (!options->pipe)
111+
{
112+
if (celsius < 50)
113+
ffStrbufAppendF(buffer, "\e[%sm", colorGreen);
114+
else if (celsius < 80)
115+
ffStrbufAppendF(buffer, "\e[%sm", colorYellow);
116+
else
117+
ffStrbufAppendF(buffer, "\e[%sm", colorRed);
118+
}
119+
120+
switch (options->temperatureUnit)
102121
{
103122
case FF_TEMPERATURE_UNIT_CELSIUS:
104-
ffStrbufAppendF(buffer, "%.1f°C", celsius);
123+
ffStrbufAppendF(buffer, "%.*f°C", options->temperatureNdigits, celsius);
105124
break;
106125
case FF_TEMPERATURE_UNIT_FAHRENHEIT:
107-
ffStrbufAppendF(buffer, "%.1f°F", celsius * 1.8 + 32);
126+
ffStrbufAppendF(buffer, "%.*f°F", options->temperatureNdigits, celsius * 1.8 + 32);
108127
break;
109128
case FF_TEMPERATURE_UNIT_KELVIN:
110-
ffStrbufAppendF(buffer, "%.1f K", celsius + 273.15);
129+
ffStrbufAppendF(buffer, "%.*f K", options->temperatureNdigits, celsius + 273.15);
111130
break;
112131
}
132+
133+
if (!options->pipe)
134+
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
113135
}
114136

115137
void ffParseGTK(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, const FFstrbuf* gtk4)

src/data/help.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,41 @@
667667
},
668668
"default": "C"
669669
}
670+
},
671+
{
672+
"long": "temperature-ndigits",
673+
"desc": "Set the number of digits to keep after the decimal point when printing temperature",
674+
"arg": {
675+
"type": "num",
676+
"default": 2
677+
}
678+
},
679+
{
680+
"long": "temperature-color-green",
681+
"desc": "Set color used in green state of temperature values",
682+
"remark": "See `-h color` for the list of available colors",
683+
"arg": {
684+
"type": "color",
685+
"default": "green"
686+
}
687+
},
688+
{
689+
"long": "temperature-color-yellow",
690+
"desc": "Set color used in yellow state of temperature values",
691+
"remark": "See `-h color` for the list of available colors",
692+
"arg": {
693+
"type": "color",
694+
"default": "light_yellow"
695+
}
696+
},
697+
{
698+
"long": "temperature-color-red",
699+
"desc": "Set color used in red state of temperature values",
700+
"remark": "See `-h color` for the list of available colors",
701+
"arg": {
702+
"type": "color",
703+
"default": "light_red"
704+
}
670705
}
671706
],
672707
"Library path": [

src/modules/battery/battery.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
5858
{
5959
FF_STRBUF_AUTO_DESTROY capacityStr = ffStrbufCreate();
6060
ffPercentAppendNum(&capacityStr, result->capacity, options->percent, false);
61+
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
62+
ffParseTemperature(result->temperature, &tempStr);
6163
ffPrintFormat(FF_BATTERY_MODULE_NAME, index, &options->moduleArgs, FF_BATTERY_NUM_FORMAT_ARGS, (FFformatarg[]){
6264
{FF_FORMAT_ARG_TYPE_STRBUF, &result->manufacturer},
6365
{FF_FORMAT_ARG_TYPE_STRBUF, &result->modelName},
6466
{FF_FORMAT_ARG_TYPE_STRBUF, &result->technology},
6567
{FF_FORMAT_ARG_TYPE_STRBUF, &capacityStr},
6668
{FF_FORMAT_ARG_TYPE_STRBUF, &result->status},
67-
{FF_FORMAT_ARG_TYPE_DOUBLE, &result->temperature},
69+
{FF_FORMAT_ARG_TYPE_STRBUF, &tempStr},
6870
{FF_FORMAT_ARG_TYPE_UINT, &result->cycleCount},
6971
{FF_FORMAT_ARG_TYPE_STRBUF, &result->serial},
7072
{FF_FORMAT_ARG_TYPE_STRBUF, &result->manufactureDate},
@@ -226,7 +228,7 @@ void ffPrintBatteryHelpFormat(void)
226228
"Battery technology",
227229
"Battery capacity (percentage)",
228230
"Battery status",
229-
"Battery temperature",
231+
"Battery temperature (formatted)",
230232
"Battery cycle count",
231233
"Battery serial number",
232234
"Battery manufactor date",

src/modules/cpu/cpu.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void ffPrintCPU(FFCPUOptions* options)
6060
}
6161
else
6262
{
63+
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
64+
ffParseTemperature(cpu.temperature, &tempStr);
6365
ffPrintFormat(FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_CPU_NUM_FORMAT_ARGS, (FFformatarg[]){
6466
{FF_FORMAT_ARG_TYPE_STRBUF, &cpu.name},
6567
{FF_FORMAT_ARG_TYPE_STRBUF, &cpu.vendor},
@@ -68,7 +70,7 @@ void ffPrintCPU(FFCPUOptions* options)
6870
{FF_FORMAT_ARG_TYPE_UINT16, &cpu.coresOnline},
6971
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.frequencyMin},
7072
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.frequencyMax},
71-
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu.temperature}
73+
{FF_FORMAT_ARG_TYPE_STRBUF, &tempStr}
7274
});
7375
}
7476
}
@@ -193,7 +195,7 @@ void ffPrintCPUHelpFormat(void)
193195
"Online core count",
194196
"Min frequency",
195197
"Max frequency",
196-
"Temperature"
198+
"Temperature (formatted)"
197199
});
198200
}
199201

src/modules/gpu/gpu.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ static void printGPUResult(FFGPUOptions* options, uint8_t index, const FFGPUResu
7272
}
7373
else
7474
{
75+
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
76+
ffParseTemperature(gpu->temperature, &tempStr);
7577
ffPrintFormat(FF_GPU_MODULE_NAME, index, &options->moduleArgs, FF_GPU_NUM_FORMAT_ARGS, (FFformatarg[]){
7678
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->vendor},
7779
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->name},
7880
{FF_FORMAT_ARG_TYPE_STRBUF, &gpu->driver},
79-
{FF_FORMAT_ARG_TYPE_DOUBLE, &gpu->temperature},
81+
{FF_FORMAT_ARG_TYPE_STRBUF, &tempStr},
8082
{FF_FORMAT_ARG_TYPE_INT, &gpu->coreCount},
8183
{FF_FORMAT_ARG_TYPE_STRING, type},
8284
{FF_FORMAT_ARG_TYPE_UINT64, &gpu->dedicated.total},

src/modules/physicaldisk/physicaldisk.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options)
102102
}
103103
else
104104
{
105+
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
106+
ffParseTemperature(dev->temperature, &tempStr);
105107
if (dev->type & FF_PHYSICALDISK_TYPE_READWRITE)
106108
readOnlyType = "Read-write";
107109
ffParseSize(dev->size, &buffer);
@@ -115,7 +117,7 @@ void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options)
115117
{FF_FORMAT_ARG_TYPE_STRING, removableType},
116118
{FF_FORMAT_ARG_TYPE_STRING, readOnlyType},
117119
{FF_FORMAT_ARG_TYPE_STRBUF, &dev->revision},
118-
{FF_FORMAT_ARG_TYPE_DOUBLE, &dev->temperature},
120+
{FF_FORMAT_ARG_TYPE_DOUBLE, &tempStr},
119121
});
120122
}
121123
++index;
@@ -263,7 +265,7 @@ void ffPrintPhysicalDiskHelpFormat(void)
263265
"Device kind (Removable or Fixed)",
264266
"Device kind (Read-only or Read-write)",
265267
"Product revision",
266-
"Device temperature",
268+
"Device temperature (formatted)",
267269
});
268270
}
269271

0 commit comments

Comments
 (0)