Skip to content

Commit 468d1e0

Browse files
committed
Display: add option --fraction-ndigits
to control number of digits to print when formatting ordinary fraction numbers
1 parent 6851e7b commit 468d1e0

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

doc/json_schema.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,28 @@
934934
}
935935
}
936936
},
937+
"fraction": {
938+
"type": "object",
939+
"additionalProperties": false,
940+
"description": "Set how ordinary fraction numbers should be displayed",
941+
"properties": {
942+
"ndigits": {
943+
"oneOf": [
944+
{
945+
"type": "number",
946+
"description": "Set the number of digits to keep after the decimal point when formatting ordinary fraction numbers",
947+
"minimum": 0,
948+
"maximum": 9
949+
},
950+
{
951+
"type": "null",
952+
"description": "The number of digits will be automatically determined based on the value"
953+
}
954+
],
955+
"default": null
956+
}
957+
}
958+
},
937959
"noBuffer": {
938960
"type": "boolean",
939961
"description": "Whether to disable the stdout application buffer",

src/common/format.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg)
3232
ffStrbufAppend(buffer, (FFstrbuf*)formatarg->value);
3333
break;
3434
case FF_FORMAT_ARG_TYPE_FLOAT:
35-
ffStrbufAppendF(buffer, "%f", *(float*)formatarg->value);
35+
if (instance.config.display.fractionNdigits >= 0)
36+
ffStrbufAppendF(buffer, "%.*f", instance.config.display.fractionNdigits, *(float*)formatarg->value);
37+
else
38+
ffStrbufAppendF(buffer, "%g", *(float*)formatarg->value);
3639
break;
3740
case FF_FORMAT_ARG_TYPE_DOUBLE:
38-
ffStrbufAppendF(buffer, "%g", *(double*)formatarg->value);
41+
if (instance.config.display.fractionNdigits >= 0)
42+
ffStrbufAppendF(buffer, "%.*f", instance.config.display.fractionNdigits, *(double*)formatarg->value);
43+
else
44+
ffStrbufAppendF(buffer, "%g", *(double*)formatarg->value);
3945
break;
4046
case FF_FORMAT_ARG_TYPE_BOOL:
4147
ffStrbufAppendS(buffer, *(bool*)formatarg->value ? "true" : "false");
@@ -108,6 +114,7 @@ static inline bool formatArgSet(const FFformatarg* arg)
108114
{
109115
return arg->value != NULL && (
110116
(arg->type == FF_FORMAT_ARG_TYPE_DOUBLE && *(double*)arg->value > 0.0) || //Also is false for NaN
117+
(arg->type == FF_FORMAT_ARG_TYPE_FLOAT && *(float*)arg->value > 0.0) || //Also is false for NaN
111118
(arg->type == FF_FORMAT_ARG_TYPE_INT && *(int*)arg->value > 0) ||
112119
(arg->type == FF_FORMAT_ARG_TYPE_STRBUF && ((FFstrbuf*)arg->value)->length > 0) ||
113120
(arg->type == FF_FORMAT_ARG_TYPE_STRING && ffStrSet((char*)arg->value)) ||

src/data/help.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,15 @@
719719
"default": 2
720720
}
721721
},
722+
{
723+
"long": "fraction-ndigits",
724+
"desc": "Set the number of digits to keep after the decimal point when printing ordinary fraction numbers",
725+
"remark": "If negative, the number of digits will be automatically determined based on the value",
726+
"arg": {
727+
"type": "num",
728+
"default": -1
729+
}
730+
},
722731
{
723732
"long": "temp-unit",
724733
"desc": "Set the temperature unit",

src/options/display.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,23 @@ const char* ffOptionsParseDisplayJsonConfig(FFOptionsDisplay* options, yyjson_va
221221
else
222222
return "display.bar must be an object";
223223
}
224+
else if (ffStrEqualsIgnCase(key, "fraction"))
225+
{
226+
if (yyjson_is_obj(val))
227+
{
228+
yyjson_val* ndigits = yyjson_obj_get(val, "ndigits");
229+
if (ndigits)
230+
{
231+
if (yyjson_is_null(ndigits))
232+
options->fractionNdigits = -1;
233+
else if (!yyjson_is_int(ndigits))
234+
return "display.fraction.ndigits must be an integer";
235+
options->fractionNdigits = (int8_t) yyjson_get_int(ndigits);
236+
}
237+
}
238+
else
239+
return "display.fraction must be an object";
240+
}
224241
else if (ffStrEqualsIgnCase(key, "noBuffer"))
225242
options->noBuffer = yyjson_get_bool(val);
226243
else if (ffStrEqualsIgnCase(key, "keyWidth"))
@@ -462,6 +479,8 @@ bool ffOptionsParseDisplayCommandLine(FFOptionsDisplay* options, const char* key
462479
else
463480
return false;
464481
}
482+
else if(ffStrEqualsIgnCase(key, "--fraction-ndigits"))
483+
options->fractionNdigits = (int8_t) ffOptionParseInt32(key, value);
465484
else if(ffStrEqualsIgnCase(key, "--no-buffer"))
466485
options->noBuffer = ffOptionParseBoolean(value);
467486
else if(ffStrStartsWithIgnCase(key, "--bar-"))
@@ -539,6 +558,7 @@ void ffOptionsInitDisplay(FFOptionsDisplay* options)
539558
ffStrbufInitStatic(&options->percentColorYellow, instance.state.terminalLightTheme ? FF_COLOR_FG_YELLOW : FF_COLOR_FG_LIGHT_YELLOW);
540559
ffStrbufInitStatic(&options->percentColorRed, instance.state.terminalLightTheme ? FF_COLOR_FG_RED : FF_COLOR_FG_LIGHT_RED);
541560
options->freqNdigits = 2;
561+
options->fractionNdigits = -1;
542562

543563
ffListInit(&options->constants, sizeof(FFstrbuf));
544564
}

src/options/display.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ typedef struct FFOptionsDisplay
6060
uint16_t keyWidth;
6161
uint16_t keyPaddingLeft;
6262
int8_t freqNdigits;
63+
int8_t fractionNdigits;
6364
FFlist constants; // list of FFstrbuf
6465
} FFOptionsDisplay;
6566

0 commit comments

Comments
 (0)