From 87854fa1015d8ac8aed5b167cc6d26a478910500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Gibrowski=20Fa=C3=A9?= Date: Sun, 24 Jul 2022 15:10:09 -0300 Subject: [PATCH] float tag: let user specify number of decimals Closes #200 --- CHANGELOG.md | 2 ++ doc/yambar-tags.5.scd | 6 +++++- tag.c | 32 ++++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ade384d..e7e922a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Added +* Support for specifying number of decimals when printing a float tag ([#200][200]). * Support for custom font fallbacks ([#153][153]). * overline: new decoration ([#153][153]). * i3/sway: boolean option `strip-workspace-numbers`. @@ -31,6 +32,7 @@ [153]: https://codeberg.org/dnkl/yambar/issues/153 [159]: https://codeberg.org/dnkl/yambar/issues/159 +[200]: https://codeberg.org/dnkl/yambar/issues/200 [202]: https://codeberg.org/dnkl/yambar/issues/202 [219]: https://codeberg.org/dnkl/yambar/pulls/219 [228]: https://codeberg.org/dnkl/yambar/pulls/228 diff --git a/doc/yambar-tags.5.scd b/doc/yambar-tags.5.scd index c4d5408..a8bb2ce 100644 --- a/doc/yambar-tags.5.scd +++ b/doc/yambar-tags.5.scd @@ -59,7 +59,11 @@ be used. [[ *Formatter* :[ *Kind* :[ *Description* -:[ *Applies to*] +:[ *Applies to* +| . +: format +: How many decimals to print +: Float tags | hex : format : Renders a tag's value in hex diff --git a/tag.c b/tag.c index fc09bdc..4e72c2e 100644 --- a/tag.c +++ b/tag.c @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include #include #define LOG_MODULE "tag" @@ -425,6 +427,16 @@ sbuf_append(struct sbuf *s1, const char *s2) sbuf_append_at_most(s1, s2, strlen(s2)); } +bool +is_number(const char *str) { + while (*str != '\0') { + if (!isdigit(*str)) + return false; + ++str; + } + return true; +} + char * tags_expand_template(const char *template, const struct tag_set *tags) { @@ -456,7 +468,7 @@ tags_expand_template(const char *template, const struct tag_set *tags) strncpy(tag_name_and_arg, begin + 1, end - begin - 1); tag_name_and_arg[end - begin - 1] = '\0'; - static const size_t MAX_TAG_ARGS = 3; + static const size_t MAX_TAG_ARGS = 4; const char *tag_name = NULL; const char *tag_args[MAX_TAG_ARGS]; memset(tag_args, 0, sizeof(tag_args)); @@ -507,6 +519,9 @@ tags_expand_template(const char *template, const struct tag_set *tags) VALUE_UNIT, } kind = VALUE_VALUE; + int decimals = 2; + char *float_fmt_end; + for (size_t i = 0; i < MAX_TAG_ARGS; i++) { if (tag_args[i] == NULL) break; @@ -534,6 +549,8 @@ tags_expand_template(const char *template, const struct tag_set *tags) kind = VALUE_MAX; else if (strcmp(tag_args[i], "unit") == 0) kind = VALUE_UNIT; + else if (tag_args[i][0] == '.' && is_number(tag_args[i] + 1)) + decimals = strtol(tag_args[i] + 1, &float_fmt_end, 10); else LOG_WARN("invalid tag formatter: %s", tag_args[i]); } @@ -542,9 +559,16 @@ tags_expand_template(const char *template, const struct tag_set *tags) switch (kind) { case VALUE_VALUE: switch (format) { - case FMT_DEFAULT: - sbuf_append(&formatted, tag->as_string(tag)); + case FMT_DEFAULT: { + if (tag->type(tag) == TAG_TYPE_FLOAT){ + char str[24]; + snprintf(str, sizeof(str), "%.*f", decimals, tag->as_float(tag)); + sbuf_append(&formatted, str); + } else { + sbuf_append(&formatted, tag->as_string(tag)); + } break; + } case FMT_HEX: case FMT_OCT: { @@ -583,7 +607,7 @@ tags_expand_template(const char *template, const struct tag_set *tags) char str[24]; if (tag->type(tag) == TAG_TYPE_FLOAT) - snprintf(str, sizeof(str), "%.2f", tag->as_float(tag) / (double)divider); + snprintf(str, sizeof(str), "%.*f", decimals, tag->as_float(tag) / (double)divider); else snprintf(str, sizeof(str), "%lu", tag->as_int(tag) / divider); sbuf_append(&formatted, str);