tag: add 'kbit' 'mbit' 'gbit' 'kibit' 'mibit' 'gibit' formatters

This commit is contained in:
Zhong Jianxin 2024-08-21 19:11:57 +08:00
parent 700bf5b28c
commit 981afaab86
2 changed files with 39 additions and 1 deletions

View file

@ -100,6 +100,15 @@ be used.
: format
: All tag types
: Same as *kb*, *mb* and *gb*, but divide by 1024^n instead of 1000^n.
| kbit, mbit, gbit
: format
: All tag types
: Renders a tag's value (in decimal) divided by 8\*1000, 8\*1000^2 or
8\*1000^3. Note: no unit suffix is appended
| kibit, mibit, gibit
: format
: All tag types
: Same as *kbit*, *mbit* and *gbit*, but divide by 8\*1024^n instead of 8\*1000^n.
| min
: selector
: Range tags

31
tag.c
View file

@ -530,6 +530,7 @@ tags_expand_template(const char *template, const struct tag_set *tags)
int digits = 0;
int decimals = 2;
bool use_byte = false;
bool zero_pad = false;
char *point = NULL;
@ -546,16 +547,40 @@ tags_expand_template(const char *template, const struct tag_set *tags)
format = FMT_BYTE;
else if (strcmp(tag_args[i], "kb") == 0)
format = FMT_KBYTE;
else if (strcmp(tag_args[i], "kbit") == 0) {
format = FMT_KBYTE;
use_byte = true;
}
else if (strcmp(tag_args[i], "mb") == 0)
format = FMT_MBYTE;
else if (strcmp(tag_args[i], "mbit") == 0) {
format = FMT_MBYTE;
use_byte = true;
}
else if (strcmp(tag_args[i], "gb") == 0)
format = FMT_GBYTE;
else if (strcmp(tag_args[i], "gbit") == 0) {
format = FMT_GBYTE;
use_byte = true;
}
else if (strcmp(tag_args[i], "kib") == 0)
format = FMT_KIBYTE;
else if (strcmp(tag_args[i], "kibit") == 0) {
format = FMT_KIBYTE;
use_byte = true;
}
else if (strcmp(tag_args[i], "mib") == 0)
format = FMT_MIBYTE;
else if (strcmp(tag_args[i], "mibit") == 0) {
format = FMT_MIBYTE;
use_byte = true;
}
else if (strcmp(tag_args[i], "gib") == 0)
format = FMT_GIBYTE;
else if (strcmp(tag_args[i], "gibit") == 0) {
format = FMT_GIBYTE;
use_byte = true;
}
else if (strcmp(tag_args[i], "min") == 0)
kind = VALUE_MIN;
else if (strcmp(tag_args[i], "max") == 0)
@ -644,7 +669,7 @@ tags_expand_template(const char *template, const struct tag_set *tags)
case FMT_KIBYTE:
case FMT_MIBYTE:
case FMT_GIBYTE: {
const long divider =
long divider =
format == FMT_BYTE
? 8
: format == FMT_KBYTE
@ -661,6 +686,10 @@ tags_expand_template(const char *template, const struct tag_set *tags)
? 1024 * 1024 * 1024
: 1;
if (use_byte) {
divider *= 8;
}
char str[24];
if (tag->type(tag) == TAG_TYPE_FLOAT) {
const char *fmt = zero_pad ? "%0*.*f" : "%*.*f";