From e201cc3d30693eb3a65c22b3bd5f69489d32100a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 27 Aug 2021 21:02:01 +0200 Subject: [PATCH 1/5] =?UTF-8?q?tag:=20add=20a=20=E2=80=98%=E2=80=99=20form?= =?UTF-8?q?atter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Range tags can now be rendered as a percentage value, by using a ‘%’ formatter: {tag_name:%} --- CHANGELOG.md | 1 + doc/yambar-tags.5.scd | 51 ++++++++++++++++++++++++++++++++++++++----- tag.c | 28 ++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f587e1..57fe2e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ * network: `ssid`, `signal`, `rx-bitrate` and `rx-bitrate` tags. * network: `poll-interval` option (for the new `signal` and `*-bitrate` tags). +* tags: percentage formatter, for range tags: `{tag_name:%}`. ### Changed diff --git a/doc/yambar-tags.5.scd b/doc/yambar-tags.5.scd index 1e3c138..8d357cb 100644 --- a/doc/yambar-tags.5.scd +++ b/doc/yambar-tags.5.scd @@ -38,9 +38,50 @@ The available tag *types* are: # FORMATTING -As mentioned above, each tag type has a default representation that is -used when the tag is rendered by a string particle. +A tag may be followed by one or more formatters that alter the tags +rendition. + +Formatters are added by appending a ':' separated list of formatter +names: + + "{tag_name:max:hex}" + +In the table below, "kind" describes the type of action performed by +the formatter: + +- *format*: changes the representation of the tag's value +- *selector*: changes what to render + +In general, formatters of the same kind cannot be combined; if +multiple formatters of the same kind are specified, the last one will +be used. + +[[ *Formatter* +:[ *Kind* +:[ *Description* +:[ *Applies to*] +| hex +: format +: Renders a tag's value in hex +: All tag types +| oct +: format +: Renders a tag's value in octal +: All tag types +| % +: format +: Renders a range tag's value as a percentage value +: Range tags +| min +: selector +: Renders a range tag's mininum value +: Range tags +| max +: selector +: Renders a range tag's maximum value +: Range tags +| unit +: selector +: Renders a realtime tag's unit (e.g. "s", or "ms") +: Realtime tags -All integer, floating point and boolean tag types can be modified to -instead be rendered in hexadecimal or octal form, by appending either -the *:hex* or *:oct* suffixes. For example, _\"{tag_name:hex}\"_. \ No newline at end of file diff --git a/tag.c b/tag.c index 8ca7e52..0548b2e 100644 --- a/tag.c +++ b/tag.c @@ -459,7 +459,7 @@ tags_expand_template(const char *template, const struct tag_set *tags) sbuf_append_at_most(&formatted, template, begin - template); /* Parse arguments */ - enum { FMT_DEFAULT, FMT_HEX, FMT_OCT } format = FMT_DEFAULT; + enum { FMT_DEFAULT, FMT_HEX, FMT_OCT, FMT_PERCENT } format = FMT_DEFAULT; enum { VALUE_VALUE, VALUE_MIN, VALUE_MAX, VALUE_UNIT } kind = VALUE_VALUE; for (size_t i = 0; i < MAX_TAG_ARGS; i++) { @@ -469,12 +469,16 @@ tags_expand_template(const char *template, const struct tag_set *tags) format = FMT_HEX; else if (strcmp(tag_args[i], "oct") == 0) format = FMT_OCT; + else if (strcmp(tag_args[i], "%") == 0) + format = FMT_PERCENT; else if (strcmp(tag_args[i], "min") == 0) kind = VALUE_MIN; else if (strcmp(tag_args[i], "max") == 0) kind = VALUE_MAX; else if (strcmp(tag_args[i], "unit") == 0) kind = VALUE_UNIT; + else + LOG_WARN("invalid tag formatter: %s", tag_args[i]); } /* Copy tag value */ @@ -493,20 +497,40 @@ tags_expand_template(const char *template, const struct tag_set *tags) sbuf_append(&formatted, str); break; } + + case FMT_PERCENT: { + const long min = tag->min(tag); + const long max = tag->max(tag); + const long cur = tag->as_int(tag); + + char str[4]; + snprintf(str, sizeof(str), "%lu", (cur - min) * 100 / (max - min)); + sbuf_append(&formatted, str); + break; + } } break; case VALUE_MIN: case VALUE_MAX: { - const long value = kind == VALUE_MIN ? tag->min(tag) : tag->max(tag); + const long min = tag->min(tag); + const long max = tag->max(tag); + long value = kind == VALUE_MIN ? min : max; const char *fmt; switch (format) { case FMT_DEFAULT: fmt = "%ld"; break; case FMT_HEX: fmt = "%lx"; break; case FMT_OCT: fmt = "%lo"; break; + + case FMT_PERCENT: + value = (value - min) * 100 / (max - min); + fmt = "%lu"; + break; } + + char str[24]; snprintf(str, sizeof(str), fmt, value); sbuf_append(&formatted, str); From a6194c63e6d1dfe1c0346783f06016f16dfe7a28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 27 Aug 2021 21:15:19 +0200 Subject: [PATCH 2/5] tag: add kb/mb/gb formatters --- CHANGELOG.md | 2 ++ doc/yambar-tags.5.scd | 13 ++++++++++++ tag.c | 46 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57fe2e7..365b4fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ * network: `poll-interval` option (for the new `signal` and `*-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. ### Changed diff --git a/doc/yambar-tags.5.scd b/doc/yambar-tags.5.scd index 8d357cb..a237e30 100644 --- a/doc/yambar-tags.5.scd +++ b/doc/yambar-tags.5.scd @@ -72,6 +72,19 @@ be used. : format : Renders a range tag's value as a percentage value : Range tags +| kb +: format +: Renders a tag's value (in decimal) divided by 1024 (note that no + unit suffix is appended) +| mb +: format +: Renders a tag's value (in decimal) divided by 1024^2 (note that no + unit suffix is appended) +| gb +: format +: Renders a tag's value (in decimal) divided by 1024^3 (note that no + unit suffix is appended) +: All tag types | min : selector : Renders a range tag's mininum value diff --git a/tag.c b/tag.c index 0548b2e..fce5d38 100644 --- a/tag.c +++ b/tag.c @@ -459,7 +459,15 @@ tags_expand_template(const char *template, const struct tag_set *tags) sbuf_append_at_most(&formatted, template, begin - template); /* Parse arguments */ - enum { FMT_DEFAULT, FMT_HEX, FMT_OCT, FMT_PERCENT } format = FMT_DEFAULT; + enum { + FMT_DEFAULT, + FMT_HEX, + FMT_OCT, + FMT_PERCENT, + FMT_KBYTE, + FMT_MBYTE, + FMT_GBYTE, + } format = FMT_DEFAULT; enum { VALUE_VALUE, VALUE_MIN, VALUE_MAX, VALUE_UNIT } kind = VALUE_VALUE; for (size_t i = 0; i < MAX_TAG_ARGS; i++) { @@ -471,6 +479,12 @@ tags_expand_template(const char *template, const struct tag_set *tags) format = FMT_OCT; else if (strcmp(tag_args[i], "%") == 0) format = FMT_PERCENT; + else if (strcmp(tag_args[i], "kb") == 0) + format = FMT_KBYTE; + else if (strcmp(tag_args[i], "mb") == 0) + format = FMT_MBYTE; + else if (strcmp(tag_args[i], "gb") == 0) + format = FMT_GBYTE; else if (strcmp(tag_args[i], "min") == 0) kind = VALUE_MIN; else if (strcmp(tag_args[i], "max") == 0) @@ -508,6 +522,21 @@ tags_expand_template(const char *template, const struct tag_set *tags) sbuf_append(&formatted, str); break; } + + case FMT_KBYTE: + case FMT_MBYTE: + case FMT_GBYTE: { + const long divider = + format == FMT_KBYTE ? 1024 : + format == FMT_MBYTE ? 1024 * 1024 : + format == FMT_GBYTE ? 1024 * 1024 * 1024 : + 1; + + char str[24]; + snprintf(str, sizeof(str), "%lu", tag->as_int(tag) / divider); + sbuf_append(&formatted, str); + break; + } } break; @@ -527,9 +556,20 @@ tags_expand_template(const char *template, const struct tag_set *tags) value = (value - min) * 100 / (max - min); fmt = "%lu"; break; - } - + case FMT_KBYTE: + case FMT_MBYTE: + case FMT_GBYTE: { + const long divider = + format == FMT_KBYTE ? 1024 : + format == FMT_MBYTE ? 1024 * 1024 : + format == FMT_GBYTE ? 1024 * 1024 * 1024 : + 1; + value /= divider; + fmt = "%lu"; + break; + } + } char str[24]; snprintf(str, sizeof(str), fmt, value); From f0b16033fe28986dadbb6609280c3cad29ff5c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 27 Aug 2021 21:18:01 +0200 Subject: [PATCH 3/5] doc: yambar-tags: codespell: mininum -> minimum --- doc/yambar-tags.5.scd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/yambar-tags.5.scd b/doc/yambar-tags.5.scd index a237e30..cdb0b4b 100644 --- a/doc/yambar-tags.5.scd +++ b/doc/yambar-tags.5.scd @@ -87,7 +87,7 @@ be used. : All tag types | min : selector -: Renders a range tag's mininum value +: Renders a range tag's minimum value : Range tags | max : selector From 8c2e5d8bdea8bcccbc56c8a60774ff75f5f2b4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 27 Aug 2021 21:19:20 +0200 Subject: [PATCH 4/5] doc: yambar-tags: add missing last column to kb/mb formatters --- doc/yambar-tags.5.scd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/yambar-tags.5.scd b/doc/yambar-tags.5.scd index cdb0b4b..38925a9 100644 --- a/doc/yambar-tags.5.scd +++ b/doc/yambar-tags.5.scd @@ -76,10 +76,12 @@ be used. : format : Renders a tag's value (in decimal) divided by 1024 (note that no unit suffix is appended) +: All tag types | mb : 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 From eff890ab9d5ad74aa9da4d5b8f7ee5f45adbe7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Fri, 27 Aug 2021 21:57:34 +0200 Subject: [PATCH 5/5] tag: add kib/mib/gib formatters --- CHANGELOG.md | 2 +- doc/yambar-tags.5.scd | 16 +++++----------- tag.c | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 365b4fd..c48f9d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/yambar-tags.5.scd b/doc/yambar-tags.5.scd index 38925a9..49a08ab 100644 --- a/doc/yambar-tags.5.scd +++ b/doc/yambar-tags.5.scd @@ -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 diff --git a/tag.c b/tag.c index fce5d38..2943423 100644 --- a/tag.c +++ b/tag.c @@ -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";