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