|
| 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 | +} |
0 commit comments