From 21e28315e3476229af9e4482857cfdb9f4228804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 13 Jan 2019 10:38:25 +0100 Subject: [PATCH] particle/progress-bar: expose info through the new struct particle_info struct --- config-verify.c | 15 ++--------- config.c | 31 +++------------------- particle.h | 3 ++- particles/progress-bar.c | 56 ++++++++++++++++++++++++++++++++++------ particles/progress-bar.h | 6 +---- 5 files changed, 56 insertions(+), 55 deletions(-) diff --git a/config-verify.c b/config-verify.c index 1a99d9f..4074f35 100644 --- a/config-verify.c +++ b/config-verify.c @@ -12,6 +12,7 @@ #include "particles/empty.h" #include "particles/list.h" #include "particles/map.h" +#include "particles/progress-bar.h" const char * conf_err_prefix(const keychain_t *chain, const struct yml_node *node) @@ -290,18 +291,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"right-margin", false, &conf_verify_int}, \ {"on-click", false, &conf_verify_string}, - static const struct attr_info progress_bar[] = { - {"tag", true, &conf_verify_string}, - {"length", true, &conf_verify_int}, - /* TODO: make these optional? Default to empty */ - {"start", true, &conf_verify_particle}, - {"end", true, &conf_verify_particle}, - {"fill", true, &conf_verify_particle}, - {"empty", true, &conf_verify_particle}, - {"indicator", true, &conf_verify_particle}, - COMMON_ATTRS - }; - static const struct attr_info ramp[] = { {"tag", true, &conf_verify_string}, {"items", true, &conf_verify_particle_list_items}, @@ -326,6 +315,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) {"empty", &particle_empty}, {"list", &particle_list}, {"map", &particle_map}, + {"progress-bar", &particle_progress_bar}, }; static const struct { @@ -333,7 +323,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node) const struct attr_info *attrs; size_t count; } particles[] = { - {"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])}, {"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])}, {"string", string, sizeof(string) / sizeof(string[0])}, }; diff --git a/config.c b/config.c index c2845fc..bee4a2f 100644 --- a/config.c +++ b/config.c @@ -186,31 +186,6 @@ particle_ramp_from_config(const struct yml_node *node, on_click_template); } -static struct particle * -particle_progress_bar_from_config(const struct yml_node *node, - const struct font *parent_font, - int left_margin, int right_margin, - const char *on_click_template) -{ - const struct yml_node *tag = yml_get_value(node, "tag"); - const struct yml_node *length = yml_get_value(node, "length"); - const struct yml_node *start = yml_get_value(node, "start"); - const struct yml_node *end = yml_get_value(node, "end"); - const struct yml_node *fill = yml_get_value(node, "fill"); - const struct yml_node *empty = yml_get_value(node, "empty"); - const struct yml_node *indicator = yml_get_value(node, "indicator"); - - return particle_progress_bar_new( - yml_value_as_string(tag), - yml_value_as_int(length), - conf_to_particle(start, parent_font), - conf_to_particle(end, parent_font), - conf_to_particle(fill, parent_font), - conf_to_particle(empty, parent_font), - conf_to_particle(indicator, parent_font), - left_margin, right_margin, on_click_template); -} - static struct particle * particle_simple_list_from_config(const struct yml_node *node, const struct font *parent_font) @@ -261,6 +236,9 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "map") == 0) ret = particle_map.from_conf( pair.value, parent_font, left, right, on_click_template); + else if (strcmp(type, "progress-bar") == 0) + ret = particle_progress_bar.from_conf( + pair.value, parent_font, left, right, on_click_template); else if (strcmp(type, "string") == 0) ret = particle_string_from_config( @@ -268,9 +246,6 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font) else if (strcmp(type, "ramp") == 0) ret = particle_ramp_from_config( pair.value, parent_font, left, right, on_click_template); - else if (strcmp(type, "progress-bar") == 0) - ret = particle_progress_bar_from_config( - pair.value, parent_font, left, right, on_click_template); else assert(false); diff --git a/particle.h b/particle.h index 6bf1621..5b8644d 100644 --- a/particle.h +++ b/particle.h @@ -27,7 +27,8 @@ struct particle_info { {"margin", false, &conf_verify_int}, \ {"left-margin", false, &conf_verify_int}, \ {"right-margin", false, &conf_verify_int}, \ - {"on-click", false, &conf_verify_string} + {"on-click", false, &conf_verify_string}, \ + {NULL, false, NULL} }; diff --git a/particles/progress-bar.c b/particles/progress-bar.c index 88b4f2a..d90a8a6 100644 --- a/particles/progress-bar.c +++ b/particles/progress-bar.c @@ -7,6 +7,7 @@ #define LOG_MODULE "progress_bar" #define LOG_ENABLE_DBG 0 #include "../log.h" +#include "../config.h" struct private { char *tag; @@ -206,14 +207,14 @@ instantiate(const struct particle *particle, const struct tag_set *tags) return exposable; } -struct particle * -particle_progress_bar_new(const char *tag, int width, - struct particle *start_marker, - struct particle *end_marker, - struct particle *fill, struct particle *empty, - struct particle *indicator, - int left_margin, int right_margin, - const char *on_click_template) +static struct particle * +progress_bar_new(const char *tag, int width, + struct particle *start_marker, + struct particle *end_marker, + struct particle *fill, struct particle *empty, + struct particle *indicator, + int left_margin, int right_margin, + const char *on_click_template) { struct private *priv = malloc(sizeof(*priv)); priv->tag = strdup(tag); @@ -232,3 +233,42 @@ particle_progress_bar_new(const char *tag, int width, return particle; } + +static struct particle * +from_conf(const struct yml_node *node, const struct font *parent_font, + int left_margin, int right_margin, const char *on_click_template) +{ + const struct yml_node *tag = yml_get_value(node, "tag"); + const struct yml_node *length = yml_get_value(node, "length"); + const struct yml_node *start = yml_get_value(node, "start"); + const struct yml_node *end = yml_get_value(node, "end"); + const struct yml_node *fill = yml_get_value(node, "fill"); + const struct yml_node *empty = yml_get_value(node, "empty"); + const struct yml_node *indicator = yml_get_value(node, "indicator"); + + return progress_bar_new( + yml_value_as_string(tag), + yml_value_as_int(length), + conf_to_particle(start, parent_font), + conf_to_particle(end, parent_font), + conf_to_particle(fill, parent_font), + conf_to_particle(empty, parent_font), + conf_to_particle(indicator, parent_font), + left_margin, right_margin, on_click_template); +} + +const struct particle_info particle_progress_bar = { + .from_conf = &from_conf, + .attr_count = PARTICLE_COMMON_ATTRS_COUNT + 7, + .attrs = { + {"tag", true, &conf_verify_string}, + {"length", true, &conf_verify_int}, + /* TODO: make these optional? Default to empty */ + {"start", true, &conf_verify_particle}, + {"end", true, &conf_verify_particle}, + {"fill", true, &conf_verify_particle}, + {"empty", true, &conf_verify_particle}, + {"indicator", true, &conf_verify_particle}, + PARTICLE_COMMON_ATTRS, + }, +}; diff --git a/particles/progress-bar.h b/particles/progress-bar.h index a9f94a5..4095cef 100644 --- a/particles/progress-bar.h +++ b/particles/progress-bar.h @@ -1,8 +1,4 @@ #pragma once #include "../particle.h" -struct particle * particle_progress_bar_new( - const char *tag, int width, - struct particle *start_marker, struct particle *end_marker, - struct particle *fill, struct particle *empty, struct particle *indicator, - int left_margin, int right_margin, const char *on_click_template); +extern const struct particle_info particle_progress_bar;