Weather: honor display.temp.unit

Fix #1560
This commit is contained in:
李通洲 2025-02-14 10:31:13 +08:00
parent a8afd05d19
commit ba7885913d
No known key found for this signature in database
GPG Key ID: 269AD4F5325A22A3
6 changed files with 25 additions and 4 deletions

View File

@ -817,8 +817,8 @@
"unit": {
"type": "string",
"description": "Set the unit of the temperature",
"enum": ["CELSIUS", "C", "FAHRENHEIT", "F", "KELVIN", "K"],
"default": "C"
"enum": ["D", "Default", "Celsius", "C", "Fahrenheit", "F", "Kelvin", "K"],
"default": "D"
},
"ndigits": {
"type": "integer",

View File

@ -39,6 +39,7 @@ void ffTempsAppendNum(double celsius, FFstrbuf* buffer, FFColorRangeConfig confi
switch (options->tempUnit)
{
case FF_TEMPERATURE_UNIT_DEFAULT:
case FF_TEMPERATURE_UNIT_CELSIUS:
ffStrbufAppendF(buffer, "%.*f°C", options->tempNdigits, celsius);
break;

View File

@ -722,11 +722,12 @@
"arg": {
"type": "enum",
"enum": {
"D": "Default",
"C": "Celsius",
"F": "Fahrenheit",
"K": "Kelvin"
},
"default": "C"
"default": "D"
}
},
{

View File

@ -19,6 +19,17 @@ void ffPrepareWeather(FFWeatherOptions* options)
ffStrbufAppend(&path, &options->location);
ffStrbufAppendS(&path, "?format=");
ffStrbufAppend(&path, &options->outputFormat);
switch (instance.config.display.tempUnit)
{
case FF_TEMPERATURE_UNIT_CELSIUS:
ffStrbufAppendS(&path, "&m");
break;
case FF_TEMPERATURE_UNIT_FAHRENHEIT:
ffStrbufAppendS(&path, "&u");
break;
default:
break;
}
status = ffNetworkingSendHttpRequest(&state, "wttr.in", path.chars, "User-Agent: curl/0.0.0\r\n");
}

View File

@ -130,6 +130,8 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
{
int value;
const char* error = ffJsonConfigParseEnum(unit, &value, (FFKeyValuePair[]) {
{ "DEFAULT", FF_TEMPERATURE_UNIT_DEFAULT },
{ "D", FF_TEMPERATURE_UNIT_DEFAULT },
{ "CELSIUS", FF_TEMPERATURE_UNIT_CELSIUS },
{ "C", FF_TEMPERATURE_UNIT_CELSIUS },
{ "FAHRENHEIT", FF_TEMPERATURE_UNIT_FAHRENHEIT },
@ -418,6 +420,8 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key
if(ffStrEqualsIgnCase(subkey, "unit"))
{
options->tempUnit = (FFTemperatureUnit) ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
{ "DEFAULT", FF_TEMPERATURE_UNIT_DEFAULT },
{ "D", FF_TEMPERATURE_UNIT_DEFAULT },
{ "CELSIUS", FF_TEMPERATURE_UNIT_CELSIUS },
{ "C", FF_TEMPERATURE_UNIT_CELSIUS },
{ "FAHRENHEIT", FF_TEMPERATURE_UNIT_FAHRENHEIT },
@ -513,7 +517,7 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
options->keyPaddingLeft = 0;
options->keyType = FF_MODULE_KEY_TYPE_STRING;
options->tempUnit = FF_TEMPERATURE_UNIT_CELSIUS;
options->tempUnit = FF_TEMPERATURE_UNIT_DEFAULT;
options->tempNdigits = 1;
ffStrbufInitStatic(&options->tempColorGreen, FF_COLOR_FG_GREEN);
ffStrbufInitStatic(&options->tempColorYellow, instance.state.terminalLightTheme ? FF_COLOR_FG_YELLOW : FF_COLOR_FG_LIGHT_YELLOW);
@ -643,6 +647,9 @@ void ffOptionsGenerateDisplayJsonConfig(FFOptionsDisplay* options, yyjson_mut_do
{
switch (options->tempUnit)
{
case FF_TEMPERATURE_UNIT_DEFAULT:
yyjson_mut_obj_add_str(doc, temperature, "unit", "DEFAULT");
break;
case FF_TEMPERATURE_UNIT_CELSIUS:
yyjson_mut_obj_add_str(doc, obj, "unit", "C");
break;

View File

@ -11,6 +11,7 @@ typedef enum __attribute__((__packed__)) FFSizeBinaryPrefixType
typedef enum __attribute__((__packed__)) FFTemperatureUnit
{
FF_TEMPERATURE_UNIT_DEFAULT,
FF_TEMPERATURE_UNIT_CELSIUS,
FF_TEMPERATURE_UNIT_FAHRENHEIT,
FF_TEMPERATURE_UNIT_KELVIN,