diff --git a/doc/json_schema.json b/doc/json_schema.json index 0c0812568..d74f47a2b 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -934,6 +934,28 @@ } } }, + "fraction": { + "type": "object", + "additionalProperties": false, + "description": "Set how ordinary fraction numbers should be displayed", + "properties": { + "ndigits": { + "oneOf": [ + { + "type": "number", + "description": "Set the number of digits to keep after the decimal point when formatting ordinary fraction numbers", + "minimum": 0, + "maximum": 9 + }, + { + "type": "null", + "description": "The number of digits will be automatically determined based on the value" + } + ], + "default": null + } + } + }, "noBuffer": { "type": "boolean", "description": "Whether to disable the stdout application buffer", diff --git a/src/common/format.c b/src/common/format.c index 204c1d8d7..fa9199204 100644 --- a/src/common/format.c +++ b/src/common/format.c @@ -32,10 +32,16 @@ void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg) ffStrbufAppend(buffer, (FFstrbuf*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_FLOAT: - ffStrbufAppendF(buffer, "%f", *(float*)formatarg->value); + if (instance.config.display.fractionNdigits >= 0) + ffStrbufAppendF(buffer, "%.*f", instance.config.display.fractionNdigits, *(float*)formatarg->value); + else + ffStrbufAppendF(buffer, "%g", *(float*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_DOUBLE: - ffStrbufAppendF(buffer, "%g", *(double*)formatarg->value); + if (instance.config.display.fractionNdigits >= 0) + ffStrbufAppendF(buffer, "%.*f", instance.config.display.fractionNdigits, *(double*)formatarg->value); + else + ffStrbufAppendF(buffer, "%g", *(double*)formatarg->value); break; case FF_FORMAT_ARG_TYPE_BOOL: ffStrbufAppendS(buffer, *(bool*)formatarg->value ? "true" : "false"); @@ -108,6 +114,7 @@ static inline bool formatArgSet(const FFformatarg* arg) { return arg->value != NULL && ( (arg->type == FF_FORMAT_ARG_TYPE_DOUBLE && *(double*)arg->value > 0.0) || //Also is false for NaN + (arg->type == FF_FORMAT_ARG_TYPE_FLOAT && *(float*)arg->value > 0.0) || //Also is false for NaN (arg->type == FF_FORMAT_ARG_TYPE_INT && *(int*)arg->value > 0) || (arg->type == FF_FORMAT_ARG_TYPE_STRBUF && ((FFstrbuf*)arg->value)->length > 0) || (arg->type == FF_FORMAT_ARG_TYPE_STRING && ffStrSet((char*)arg->value)) || diff --git a/src/data/help.json b/src/data/help.json index 4244fad56..0c7302bbf 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -719,6 +719,15 @@ "default": 2 } }, + { + "long": "fraction-ndigits", + "desc": "Set the number of digits to keep after the decimal point when printing ordinary fraction numbers", + "remark": "If negative, the number of digits will be automatically determined based on the value", + "arg": { + "type": "num", + "default": -1 + } + }, { "long": "temp-unit", "desc": "Set the temperature unit", diff --git a/src/options/display.c b/src/options/display.c index 61393a91b..cc4b703e6 100644 --- a/src/options/display.c +++ b/src/options/display.c @@ -221,6 +221,23 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va else return "display.bar must be an object"; } + else if (ffStrEqualsIgnCase(key, "fraction")) + { + if (yyjson_is_obj(val)) + { + yyjson_val* ndigits = yyjson_obj_get(val, "ndigits"); + if (ndigits) + { + if (yyjson_is_null(ndigits)) + options->fractionNdigits = -1; + else if (!yyjson_is_int(ndigits)) + return "display.fraction.ndigits must be an integer"; + options->fractionNdigits = (int8_t) yyjson_get_int(ndigits); + } + } + else + return "display.fraction must be an object"; + } else if (ffStrEqualsIgnCase(key, "noBuffer")) options->noBuffer = yyjson_get_bool(val); else if (ffStrEqualsIgnCase(key, "keyWidth")) @@ -462,6 +479,8 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key else return false; } + else if(ffStrEqualsIgnCase(key, "--fraction-ndigits")) + options->fractionNdigits = (int8_t) ffOptionParseInt32(key, value); else if(ffStrEqualsIgnCase(key, "--no-buffer")) options->noBuffer = ffOptionParseBoolean(value); else if(ffStrStartsWithIgnCase(key, "--bar-")) @@ -539,6 +558,7 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options) ffStrbufInitStatic(&options->percentColorYellow, instance.state.terminalLightTheme ? FF_COLOR_FG_YELLOW : FF_COLOR_FG_LIGHT_YELLOW); ffStrbufInitStatic(&options->percentColorRed, instance.state.terminalLightTheme ? FF_COLOR_FG_RED : FF_COLOR_FG_LIGHT_RED); options->freqNdigits = 2; + options->fractionNdigits = -1; ffListInit(&options->constants, sizeof(FFstrbuf)); } diff --git a/src/options/display.h b/src/options/display.h index 74c4ecb3b..d531dd7bc 100644 --- a/src/options/display.h +++ b/src/options/display.h @@ -60,6 +60,7 @@ typedef struct FFOptionsDisplay uint16_t keyWidth; uint16_t keyPaddingLeft; int8_t freqNdigits; + int8_t fractionNdigits; FFlist constants; // list of FFstrbuf } FFOptionsDisplay;