float tag: let user specify number of decimals

Closes #200
This commit is contained in:
Leonardo Gibrowski Faé 2022-07-24 15:10:09 -03:00 committed by Daniel Eklöf
parent 8deac539ef
commit 87854fa101
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 35 additions and 5 deletions

View file

@ -13,6 +13,7 @@
### Added ### Added
* Support for specifying number of decimals when printing a float tag ([#200][200]).
* Support for custom font fallbacks ([#153][153]). * Support for custom font fallbacks ([#153][153]).
* overline: new decoration ([#153][153]). * overline: new decoration ([#153][153]).
* i3/sway: boolean option `strip-workspace-numbers`. * i3/sway: boolean option `strip-workspace-numbers`.
@ -31,6 +32,7 @@
[153]: https://codeberg.org/dnkl/yambar/issues/153 [153]: https://codeberg.org/dnkl/yambar/issues/153
[159]: https://codeberg.org/dnkl/yambar/issues/159 [159]: https://codeberg.org/dnkl/yambar/issues/159
[200]: https://codeberg.org/dnkl/yambar/issues/200
[202]: https://codeberg.org/dnkl/yambar/issues/202 [202]: https://codeberg.org/dnkl/yambar/issues/202
[219]: https://codeberg.org/dnkl/yambar/pulls/219 [219]: https://codeberg.org/dnkl/yambar/pulls/219
[228]: https://codeberg.org/dnkl/yambar/pulls/228 [228]: https://codeberg.org/dnkl/yambar/pulls/228

View file

@ -59,7 +59,11 @@ be used.
[[ *Formatter* [[ *Formatter*
:[ *Kind* :[ *Kind*
:[ *Description* :[ *Description*
:[ *Applies to*] :[ *Applies to*
| .<number>
: format
: How many decimals to print
: Float tags
| hex | hex
: format : format
: Renders a tag's value in hex : Renders a tag's value in hex

30
tag.c
View file

@ -2,7 +2,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h>
#include <assert.h> #include <assert.h>
#define LOG_MODULE "tag" #define LOG_MODULE "tag"
@ -425,6 +427,16 @@ sbuf_append(struct sbuf *s1, const char *s2)
sbuf_append_at_most(s1, s2, strlen(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 * char *
tags_expand_template(const char *template, const struct tag_set *tags) 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); strncpy(tag_name_and_arg, begin + 1, end - begin - 1);
tag_name_and_arg[end - begin - 1] = '\0'; 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_name = NULL;
const char *tag_args[MAX_TAG_ARGS]; const char *tag_args[MAX_TAG_ARGS];
memset(tag_args, 0, sizeof(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, VALUE_UNIT,
} kind = VALUE_VALUE; } kind = VALUE_VALUE;
int decimals = 2;
char *float_fmt_end;
for (size_t i = 0; i < MAX_TAG_ARGS; i++) { for (size_t i = 0; i < MAX_TAG_ARGS; i++) {
if (tag_args[i] == NULL) if (tag_args[i] == NULL)
break; break;
@ -534,6 +549,8 @@ tags_expand_template(const char *template, const struct tag_set *tags)
kind = VALUE_MAX; kind = VALUE_MAX;
else if (strcmp(tag_args[i], "unit") == 0) else if (strcmp(tag_args[i], "unit") == 0)
kind = VALUE_UNIT; 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 else
LOG_WARN("invalid tag formatter: %s", tag_args[i]); 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) { switch (kind) {
case VALUE_VALUE: case VALUE_VALUE:
switch (format) { switch (format) {
case FMT_DEFAULT: 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)); sbuf_append(&formatted, tag->as_string(tag));
}
break; break;
}
case FMT_HEX: case FMT_HEX:
case FMT_OCT: { case FMT_OCT: {
@ -583,7 +607,7 @@ tags_expand_template(const char *template, const struct tag_set *tags)
char str[24]; char str[24];
if (tag->type(tag) == TAG_TYPE_FLOAT) 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 else
snprintf(str, sizeof(str), "%lu", tag->as_int(tag) / divider); snprintf(str, sizeof(str), "%lu", tag->as_int(tag) / divider);
sbuf_append(&formatted, str); sbuf_append(&formatted, str);