Merge branch 'ramp-overwrite-bounds'

This commit is contained in:
Daniel Eklöf 2021-09-20 19:29:23 +02:00
commit 87f38d19a0
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 35 additions and 2 deletions

View file

@ -10,6 +10,8 @@
## Unreleased ## Unreleased
### Added ### Added
* ramp: can now have custom min and max values
(https://codeberg.org/dnkl/yambar/issues/103).
### Changed ### Changed
* Made `libmpdclient` an optional dependency * Made `libmpdclient` an optional dependency

View file

@ -226,6 +226,16 @@ indicator.
: List of particles. Note that the tag value is *not* used as-is; its : List of particles. Note that the tag value is *not* used as-is; its
minimum and maximum values are used to map the tag's range to the minimum and maximum values are used to map the tag's range to the
particle list's range. particle list's range.
| min
: int
: no
: If present this will be used as a lower bound instead of the tags minimum value.
Tag values falling outside the defined range will get clamped to min/max.
| max
: int
: no
: If present this will be used as an upper bound instead of the tags maximum value.
Tag values falling outside the defined range will get clamped to min/max.
## EXAMPLES ## EXAMPLES

View file

@ -14,6 +14,10 @@
struct private { struct private {
char *tag; char *tag;
bool use_custom_min;
long min;
bool use_custom_max;
long max;
struct particle **particles; struct particle **particles;
size_t count; size_t count;
}; };
@ -110,6 +114,9 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
long min = tag != NULL ? tag->min(tag) : 0; long min = tag != NULL ? tag->min(tag) : 0;
long max = tag != NULL ? tag->max(tag) : 0; long max = tag != NULL ? tag->max(tag) : 0;
min = p->use_custom_min ? p->min : min;
max = p->use_custom_max ? p->max : max;
if (min > max) { if (min > max) {
LOG_WARN( LOG_WARN(
"tag's minimum value is greater than its maximum: " "tag's minimum value is greater than its maximum: "
@ -162,13 +169,19 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
static struct particle * static struct particle *
ramp_new(struct particle *common, const char *tag, ramp_new(struct particle *common, const char *tag,
struct particle *particles[], size_t count) struct particle *particles[], size_t count,
bool use_custom_min, long min,
bool use_custom_max, long max)
{ {
struct private *priv = calloc(1, sizeof(*priv)); struct private *priv = calloc(1, sizeof(*priv));
priv->tag = strdup(tag); priv->tag = strdup(tag);
priv->particles = malloc(count * sizeof(priv->particles[0])); priv->particles = malloc(count * sizeof(priv->particles[0]));
priv->count = count; priv->count = count;
priv->use_custom_max = use_custom_max;
priv->max = max;
priv->use_custom_min = use_custom_min;
priv->min = min;
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
priv->particles[i] = particles[i]; priv->particles[i] = particles[i];
@ -184,6 +197,8 @@ from_conf(const struct yml_node *node, struct particle *common)
{ {
const struct yml_node *tag = yml_get_value(node, "tag"); const struct yml_node *tag = yml_get_value(node, "tag");
const struct yml_node *items = yml_get_value(node, "items"); const struct yml_node *items = yml_get_value(node, "items");
const struct yml_node *min = yml_get_value(node, "min");
const struct yml_node *max = yml_get_value(node, "max");
size_t count = yml_list_length(items); size_t count = yml_list_length(items);
struct particle *parts[count]; struct particle *parts[count];
@ -197,7 +212,11 @@ from_conf(const struct yml_node *node, struct particle *common)
it.node, (struct conf_inherit){common->font, common->foreground}); it.node, (struct conf_inherit){common->font, common->foreground});
} }
return ramp_new(common, yml_value_as_string(tag), parts, count); long min_v = min != NULL ? yml_value_as_int(min) : 0;
long max_v = max != NULL ? yml_value_as_int(max) : 0;
return ramp_new(common, yml_value_as_string(tag), parts, count, min != NULL,
min_v, max != NULL, max_v);
} }
static bool static bool
@ -206,6 +225,8 @@ verify_conf(keychain_t *chain, const struct yml_node *node)
static const struct attr_info attrs[] = { static const struct attr_info attrs[] = {
{"tag", true, &conf_verify_string}, {"tag", true, &conf_verify_string},
{"items", true, &conf_verify_particle_list_items}, {"items", true, &conf_verify_particle_list_items},
{"min", false, &conf_verify_int},
{"max", false, &conf_verify_int},
PARTICLE_COMMON_ATTRS, PARTICLE_COMMON_ATTRS,
}; };