Skip to content

Commit ba60d90

Browse files
committed
Temps: support color range customization
1 parent 8f205b8 commit ba60d90

File tree

15 files changed

+268
-102
lines changed

15 files changed

+268
-102
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ set(LIBFASTFETCH_SRC
277277
src/common/printing.c
278278
src/common/properties.c
279279
src/common/settings.c
280+
src/common/temps.c
280281
src/detection/chassis/chassis.c
281282
src/detection/cpu/cpu.c
282283
src/detection/cpuusage/cpuusage.c

doc/json_schema.json

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "http://json-schema.org/schema",
2+
"$schema": "https://json-schema.org/draft-07/schema",
33
"$defs": {
44
"colors": {
55
"type": "string",
@@ -44,6 +44,32 @@
4444
"description": "Value greater than green and less then yellow will be shown in yellow.\nValue greater than yellow will be shown in red"
4545
}
4646
}
47+
},
48+
"temperature": {
49+
"description": "Detect and display temperature if supported",
50+
"oneOf": [
51+
{
52+
"type": "boolean",
53+
"default": false
54+
},
55+
{
56+
"type": "object",
57+
"properties": {
58+
"green": {
59+
"type": "integer",
60+
"minimum": 0,
61+
"maximum": 100,
62+
"description": "Value less then green will be shown in green"
63+
},
64+
"yellow": {
65+
"type": "integer",
66+
"minimum": 0,
67+
"maximum": 100,
68+
"description": "Value greater than green and less then yellow will be shown in yellow.\nValue greater than yellow will be shown in red"
69+
}
70+
}
71+
}
72+
]
4773
}
4874
},
4975
"type": "object",
@@ -272,7 +298,7 @@
272298
"description": "Force display detection to use DRM. Linux only",
273299
"oneOf": [
274300
{
275-
"type": "bool",
301+
"type": "boolean",
276302
"const": false,
277303
"description": "Try `wayland`, then `x11`, then `drm`"
278304
},
@@ -282,7 +308,7 @@
282308
"const": "sysfs-only"
283309
},
284310
{
285-
"type": "bool",
311+
"type": "boolean",
286312
"const": true,
287313
"description": "Try `libdrm` first, then `sysfs` if libdrm failed"
288314
}
@@ -870,9 +896,7 @@
870896
"default": false
871897
},
872898
"temp": {
873-
"description": "Detect and display Battery temperature if supported",
874-
"type": "boolean",
875-
"default": false
899+
"$ref": "#/$defs/temperature"
876900
},
877901
"percent": {
878902
"$ref": "#/$defs/percent"
@@ -970,9 +994,7 @@
970994
"const": "cpu"
971995
},
972996
"temp": {
973-
"description": "Detect and display CPU temperature if supported",
974-
"type": "boolean",
975-
"default": false
997+
"$ref": "#/$defs/temperature"
976998
},
977999
"freqNdigits": {
9781000
"description": "Set the number of digits to keep after the decimal point when printing CPU frequency",
@@ -1317,9 +1339,7 @@
13171339
"const": "gpu"
13181340
},
13191341
"temp": {
1320-
"description": "Detect and display GPU temperature if supported",
1321-
"type": "boolean",
1322-
"default": false
1342+
"$ref": "#/$defs/temperature"
13231343
},
13241344
"driverSpecific": {
13251345
"description": "Use driver specific method to detect more detailed GPU information (memory usage, core count, etc)",
@@ -1542,9 +1562,7 @@
15421562
"type": "string"
15431563
},
15441564
"temp": {
1545-
"description": "Detect and display SSD temperature if supported",
1546-
"type": "boolean",
1547-
"default": false
1565+
"$ref": "#/$defs/temperature"
15481566
},
15491567
"key": {
15501568
"$ref": "#/$defs/key"

src/common/parsing.c

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

54
#include <ctype.h>
65
#include <inttypes.h>
@@ -97,43 +96,6 @@ void ffParseSize(uint64_t bytes, FFstrbuf* result)
9796
}
9897
}
9998

100-
void ffParseTemperature(double celsius, FFstrbuf* buffer)
101-
{
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)
121-
{
122-
case FF_TEMPERATURE_UNIT_CELSIUS:
123-
ffStrbufAppendF(buffer, "%.*f°C", options->temperatureNdigits, celsius);
124-
break;
125-
case FF_TEMPERATURE_UNIT_FAHRENHEIT:
126-
ffStrbufAppendF(buffer, "%.*f°F", options->temperatureNdigits, celsius * 1.8 + 32);
127-
break;
128-
case FF_TEMPERATURE_UNIT_KELVIN:
129-
ffStrbufAppendF(buffer, "%.*f K", options->temperatureNdigits, celsius + 273.15);
130-
break;
131-
}
132-
133-
if (!options->pipe)
134-
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
135-
}
136-
13799
void ffParseGTK(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, const FFstrbuf* gtk4)
138100
{
139101
if(gtk2->length > 0 && gtk3->length > 0 && gtk4->length > 0)

src/common/parsing.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,3 @@ void ffVersionToPretty(const FFVersion* version, FFstrbuf* pretty);
2626
int8_t ffVersionCompare(const FFVersion* version1, const FFVersion* version2);
2727

2828
void ffParseSize(uint64_t bytes, FFstrbuf* result);
29-
void ffParseTemperature(double celsius, FFstrbuf* buffer);

src/common/temps.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#include "fastfetch.h"
2+
#include "common/temps.h"
3+
#include "util/textModifier.h"
4+
#include "util/stringUtils.h"
5+
6+
void ffTempsAppendNum(double celsius, FFstrbuf* buffer, FFColorRangeConfig config)
7+
{
8+
if (celsius != celsius) // ignores NaN
9+
return;
10+
11+
const FFOptionsDisplay* options = &instance.config.display;
12+
const char* colorGreen = options->temperatureColorGreen.chars;
13+
const char* colorYellow = options->temperatureColorYellow.chars;
14+
const char* colorRed = options->temperatureColorRed.chars;
15+
16+
uint8_t green = config.green, yellow = config.yellow;
17+
18+
if (!options->pipe)
19+
{
20+
if(green <= yellow)
21+
{
22+
if (celsius > yellow)
23+
ffStrbufAppendF(buffer, "\e[%sm", colorRed);
24+
else if (celsius > green)
25+
ffStrbufAppendF(buffer, "\e[%sm", colorYellow);
26+
else
27+
ffStrbufAppendF(buffer, "\e[%sm", colorGreen);
28+
}
29+
else
30+
{
31+
if (celsius < yellow)
32+
ffStrbufAppendF(buffer, "\e[%sm", colorRed);
33+
else if (celsius < green)
34+
ffStrbufAppendF(buffer, "\e[%sm", colorYellow);
35+
else
36+
ffStrbufAppendF(buffer, "\e[%sm", colorGreen);
37+
}
38+
}
39+
40+
switch (options->temperatureUnit)
41+
{
42+
case FF_TEMPERATURE_UNIT_CELSIUS:
43+
ffStrbufAppendF(buffer, "%.*f°C", options->temperatureNdigits, celsius);
44+
break;
45+
case FF_TEMPERATURE_UNIT_FAHRENHEIT:
46+
ffStrbufAppendF(buffer, "%.*f°F", options->temperatureNdigits, celsius * 1.8 + 32);
47+
break;
48+
case FF_TEMPERATURE_UNIT_KELVIN:
49+
ffStrbufAppendF(buffer, "%.*f K", options->temperatureNdigits, celsius + 273.15);
50+
break;
51+
}
52+
53+
if (!options->pipe)
54+
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
55+
}
56+
57+
bool ffTempsParseCommandOptions(const char* key, const char* subkey, const char* value, bool* useTemp, FFColorRangeConfig* config)
58+
{
59+
if (!ffStrStartsWithIgnCase(subkey, "temp"))
60+
return false;
61+
62+
if (subkey[strlen("temp")] == '\0')
63+
{
64+
*useTemp = ffOptionParseBoolean(value);
65+
return true;
66+
}
67+
68+
if (subkey[strlen("temp")] != '-')
69+
return false;
70+
71+
subkey += strlen("temp-");
72+
73+
if (ffStrEqualsIgnCase(subkey, "green"))
74+
{
75+
uint32_t num = ffOptionParseUInt32(key, value);
76+
if (num > 100)
77+
{
78+
fprintf(stderr, "Error: usage: %s must be between 0 and 100\n", key);
79+
exit(480);
80+
}
81+
config->green = (uint8_t) num;
82+
return true;
83+
}
84+
85+
if (ffStrEqualsIgnCase(subkey, "yellow"))
86+
{
87+
uint32_t num = ffOptionParseUInt32(key, value);
88+
if (num > 100)
89+
{
90+
fprintf(stderr, "Error: usage: %s must be between 0 and 100\n", key);
91+
exit(480);
92+
}
93+
config->yellow = (uint8_t) num;
94+
return true;
95+
}
96+
97+
return false;
98+
}
99+
100+
bool ffTempsParseJsonObject(const char* key, yyjson_val* value, bool* useTemp, FFColorRangeConfig* config)
101+
{
102+
if (!ffStrEqualsIgnCase(key, "temp"))
103+
return false;
104+
105+
if (yyjson_is_bool(value))
106+
{
107+
*useTemp = yyjson_get_bool(value);
108+
return true;
109+
}
110+
111+
if (yyjson_is_null(value))
112+
{
113+
*useTemp = false;
114+
return true;
115+
}
116+
117+
if (!yyjson_is_obj(value))
118+
{
119+
fprintf(stderr, "Error: usage: %s must be an object or a boolean\n", key);
120+
exit(480);
121+
}
122+
123+
*useTemp = true;
124+
125+
yyjson_val* greenVal = yyjson_obj_get(value, "green");
126+
if (greenVal)
127+
{
128+
int num = yyjson_get_int(greenVal);
129+
if (num < 0 || num > 100)
130+
{
131+
fputs("Error: usage: temp.green must be between 0 and 100\n", stderr);
132+
exit(480);
133+
}
134+
config->green = (uint8_t) num;
135+
}
136+
137+
yyjson_val* yellowVal = yyjson_obj_get(value, "yellow");
138+
if (yellowVal)
139+
{
140+
int num = yyjson_get_int(yellowVal);
141+
if (num < 0 || num > 100)
142+
{
143+
fputs("Error: usage: temp.yellow must be between 0 and 100\n", stderr);
144+
exit(480);
145+
}
146+
config->yellow = (uint8_t) num;
147+
}
148+
149+
return true;
150+
}
151+
152+
void ffTempsGenerateJsonConfig(yyjson_mut_doc* doc, yyjson_mut_val* module, FF_MAYBE_UNUSED bool defaultTemp, FFColorRangeConfig defaultConfig, bool temp, FFColorRangeConfig config)
153+
{
154+
assert(defaultTemp == false); // assume defaultTemp is always false
155+
156+
if (!temp)
157+
return;
158+
159+
if (config.green != defaultConfig.green || config.yellow != defaultConfig.yellow)
160+
{
161+
yyjson_mut_val* temp = yyjson_mut_obj_add_obj(doc, module, "temp");
162+
if (config.green != defaultConfig.green)
163+
yyjson_mut_obj_add_uint(doc, temp, "green", config.green);
164+
if (config.yellow != defaultConfig.yellow)
165+
yyjson_mut_obj_add_uint(doc, temp, "yellow", config.yellow);
166+
}
167+
else
168+
{
169+
yyjson_mut_obj_add_bool(doc, module, "temp", true);
170+
}
171+
}

src/common/temps.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#include "common/parsing.h"
4+
5+
void ffTempsAppendNum(double celsius, FFstrbuf* buffer, FFColorRangeConfig config);
6+
bool ffTempsParseCommandOptions(const char* key, const char* subkey, const char* value, bool* useTemp, FFColorRangeConfig* config);
7+
bool ffTempsParseJsonObject(const char* key, yyjson_val* value, bool* useTemp, FFColorRangeConfig* config);
8+
void ffTempsGenerateJsonConfig(yyjson_mut_doc* doc, yyjson_mut_val* module, FF_MAYBE_UNUSED bool defaultTemp, FFColorRangeConfig defaultConfig, bool temp, FFColorRangeConfig config);

src/data/help.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,29 @@
15321532
"type": "num"
15331533
},
15341534
"pseudo": true
1535+
},
1536+
{
1537+
"long": "<module>-temp-green",
1538+
"desc": [
1539+
"Threshold of temperature colors",
1540+
"Value less then temp-green will be shown in green"
1541+
],
1542+
"arg": {
1543+
"type": "num"
1544+
},
1545+
"pseudo": true
1546+
},
1547+
{
1548+
"long": "<module>-temp-yellow",
1549+
"desc": [
1550+
"Threshold of temperature colors",
1551+
"Value greater than temp-green and less then yellow will be shown in yellow",
1552+
"Value greater than temp-yellow will be shown in red"
1553+
],
1554+
"arg": {
1555+
"type": "num"
1556+
},
1557+
"pseudo": true
15351558
}
15361559
]
15371560
}

0 commit comments

Comments
 (0)