tag: add kib/mib/gib formatters

This commit is contained in:
Daniel Eklöf 2021-08-27 21:57:34 +02:00
parent 8c2e5d8bde
commit eff890ab9d
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 36 additions and 15 deletions

View file

@ -35,7 +35,7 @@
`*-bitrate` tags).
* tags: percentage formatter, for range tags: `{tag_name:%}`.
* tags: percentage tag formatter, for range tags: `{tag_name:%}`.
* tags: kb/mb/gb tag formatters.
* tags: kb/mb/gb, and kib/mib/gib tag formatters.
### Changed

View file

@ -72,20 +72,14 @@ be used.
: format
: Renders a range tag's value as a percentage value
: Range tags
| kb
| kb, mb, gb
: format
: Renders a tag's value (in decimal) divided by 1024 (note that no
unit suffix is appended)
: Renders a tag's value (in decimal) divided by 1024, 1024^2 or
1024^3. Note: no unit suffix is appended)
: All tag types
| mb
| kib, mib, gib
: format
: Renders a tag's value (in decimal) divided by 1024^2 (note that no
unit suffix is appended)
: All tag types
| gb
: format
: Renders a tag's value (in decimal) divided by 1024^3 (note that no
unit suffix is appended)
: Same as *kb*, *mb* and *gb*, but divide by 1000^n instead of 1024^n.
: All tag types
| min
: selector

33
tag.c
View file

@ -467,8 +467,17 @@ tags_expand_template(const char *template, const struct tag_set *tags)
FMT_KBYTE,
FMT_MBYTE,
FMT_GBYTE,
FMT_KIBYTE,
FMT_MIBYTE,
FMT_GIBYTE,
} format = FMT_DEFAULT;
enum { VALUE_VALUE, VALUE_MIN, VALUE_MAX, VALUE_UNIT } kind = VALUE_VALUE;
enum {
VALUE_VALUE,
VALUE_MIN,
VALUE_MAX,
VALUE_UNIT,
} kind = VALUE_VALUE;
for (size_t i = 0; i < MAX_TAG_ARGS; i++) {
if (tag_args[i] == NULL)
@ -485,6 +494,12 @@ tags_expand_template(const char *template, const struct tag_set *tags)
format = FMT_MBYTE;
else if (strcmp(tag_args[i], "gb") == 0)
format = FMT_GBYTE;
else if (strcmp(tag_args[i], "kib") == 0)
format = FMT_KIBYTE;
else if (strcmp(tag_args[i], "mib") == 0)
format = FMT_MIBYTE;
else if (strcmp(tag_args[i], "gib") == 0)
format = FMT_GIBYTE;
else if (strcmp(tag_args[i], "min") == 0)
kind = VALUE_MIN;
else if (strcmp(tag_args[i], "max") == 0)
@ -525,11 +540,17 @@ tags_expand_template(const char *template, const struct tag_set *tags)
case FMT_KBYTE:
case FMT_MBYTE:
case FMT_GBYTE: {
case FMT_GBYTE:
case FMT_KIBYTE:
case FMT_MIBYTE:
case FMT_GIBYTE: {
const long divider =
format == FMT_KBYTE ? 1024 :
format == FMT_MBYTE ? 1024 * 1024 :
format == FMT_GBYTE ? 1024 * 1024 * 1024 :
format == FMT_KIBYTE ? 1000 :
format == FMT_MIBYTE ? 1000 * 1000 :
format == FMT_GIBYTE ? 1000 * 1000 * 1000 :
1;
char str[24];
@ -559,11 +580,17 @@ tags_expand_template(const char *template, const struct tag_set *tags)
case FMT_KBYTE:
case FMT_MBYTE:
case FMT_GBYTE: {
case FMT_GBYTE:
case FMT_KIBYTE:
case FMT_MIBYTE:
case FMT_GIBYTE: {
const long divider =
format == FMT_KBYTE ? 1024 :
format == FMT_MBYTE ? 1024 * 1024 :
format == FMT_GBYTE ? 1024 * 1024 * 1024 :
format == FMT_KIBYTE ? 1000 :
format == FMT_MIBYTE ? 1000 * 1000 :
format == FMT_GIBYTE ? 1000 * 1000 * 1000 :
1;
value /= divider;
fmt = "%lu";