mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-19 19:25:41 +02:00
tag: add '/N' formatter
This commit is contained in:
parent
9498d7e445
commit
311c481bfe
2 changed files with 16 additions and 4 deletions
|
@ -86,6 +86,10 @@ be used.
|
||||||
: format
|
: format
|
||||||
: Range tags
|
: Range tags
|
||||||
: Renders a range tag's value as a percentage value
|
: Renders a range tag's value as a percentage value
|
||||||
|
| /N
|
||||||
|
: format
|
||||||
|
: All tag types
|
||||||
|
: Renders a tag's value (in decimal) divided by N
|
||||||
| b
|
| b
|
||||||
: format
|
: format
|
||||||
: All tag types
|
: All tag types
|
||||||
|
|
16
tag.c
16
tag.c
|
@ -430,12 +430,12 @@ sbuf_append(struct sbuf *s1, const char *s2)
|
||||||
|
|
||||||
// stores the number in "*value" on success
|
// stores the number in "*value" on success
|
||||||
static bool
|
static bool
|
||||||
is_number(const char *str, int *value)
|
is_number(const char *str, long *value)
|
||||||
{
|
{
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
char *end;
|
char *end;
|
||||||
int v = strtol(str, &end, 10);
|
long v = strtol(str, &end, 10);
|
||||||
if (errno != 0 || *end != '\0')
|
if (errno != 0 || *end != '\0')
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -522,8 +522,8 @@ tags_expand_template(const char *template, const struct tag_set *tags)
|
||||||
} kind
|
} kind
|
||||||
= VALUE_VALUE;
|
= VALUE_VALUE;
|
||||||
|
|
||||||
int digits = 0;
|
long digits = 0;
|
||||||
int decimals = 2;
|
long decimals = 2;
|
||||||
long divider = 1;
|
long divider = 1;
|
||||||
bool zero_pad = false;
|
bool zero_pad = false;
|
||||||
char *point = NULL;
|
char *point = NULL;
|
||||||
|
@ -537,6 +537,14 @@ tags_expand_template(const char *template, const struct tag_set *tags)
|
||||||
format = FMT_OCT;
|
format = FMT_OCT;
|
||||||
else if (strcmp(tag_args[i], "%") == 0)
|
else if (strcmp(tag_args[i], "%") == 0)
|
||||||
format = FMT_PERCENT;
|
format = FMT_PERCENT;
|
||||||
|
else if (*tag_args[i] == '/') {
|
||||||
|
format = FMT_DIVIDE;
|
||||||
|
const char *divider_str = tag_args[i] + 1;
|
||||||
|
if (!is_number(divider_str, ÷r) || divider == 0) {
|
||||||
|
divider = 1;
|
||||||
|
LOG_WARN("tag `%s`: invalid divider %s, reset to 1", tag_name, divider_str);
|
||||||
|
}
|
||||||
|
}
|
||||||
else if (strcmp(tag_args[i], "b") == 0) {
|
else if (strcmp(tag_args[i], "b") == 0) {
|
||||||
format = FMT_DIVIDE;
|
format = FMT_DIVIDE;
|
||||||
divider = 8;
|
divider = 8;
|
||||||
|
|
Loading…
Add table
Reference in a new issue