From 7fc9749c2893de21523ca272d4c8964d0040b572 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 9 Jan 2019 18:41:16 +0100 Subject: [PATCH] module/clock: user can now specify the date/time format strings --- config-verify.c | 9 ++++++++- config.c | 11 ++++++++++- modules/clock.c | 18 +++++++++++++----- modules/clock.h | 3 ++- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/config-verify.c b/config-verify.c index 32365a2..db60cba 100644 --- a/config-verify.c +++ b/config-verify.c @@ -313,7 +313,14 @@ verify_module_clock(keychain_t *chain, const struct yml_node *node) return false; } - if (strcmp(key, "content") == 0) { + if (strcmp(key, "date-format") == 0 || + strcmp(key, "time-format") == 0) + { + if (!verify_string(chain_push(chain, key), it.value)) + return false; + } + + else if (strcmp(key, "content") == 0) { if (!verify_particle(chain_push(chain, key), it.value)) return false; } diff --git a/config.c b/config.c index 6f490dd..5a9450d 100644 --- a/config.c +++ b/config.c @@ -423,8 +423,17 @@ static struct module * module_clock_from_config(const struct yml_node *node, const struct font *parent_font) { const struct yml_node *c = yml_get_value(node, "content"); + const struct yml_node *date_format = yml_get_value(node, "date-format"); + const struct yml_node *time_format = yml_get_value(node, "time-format"); + assert(c != NULL); - return module_clock(particle_from_config(c, parent_font)); + assert(date_format == NULL || yml_is_scalar(date_format)); + assert(time_format == NULL || yml_is_scalar(time_format)); + + return module_clock( + particle_from_config(c, parent_font), + date_format != NULL ? yml_value_as_string(date_format) : "%x", + time_format != NULL ? yml_value_as_string(time_format) : "%H:%M"); } static struct module * diff --git a/modules/clock.c b/modules/clock.c index 79e5b3a..a9f57e6 100644 --- a/modules/clock.c +++ b/modules/clock.c @@ -1,5 +1,6 @@ #include "clock.h" #include +#include #include #include @@ -9,6 +10,8 @@ struct private { struct particle *label; + char *date_format; + char *time_format; }; static void @@ -16,6 +19,8 @@ destroy(struct module *mod) { struct private *m = mod->private; m->label->destroy(m->label); + free(m->time_format); + free(m->date_format); free(m); module_default_destroy(mod); } @@ -27,11 +32,11 @@ content(struct module *mod) time_t t = time(NULL); struct tm *tm = localtime(&t); - char time_str[1024]; - strftime(time_str, sizeof(time_str), "%H:%M", tm); - char date_str[1024]; - strftime(date_str, sizeof(date_str), "%e %b", tm); + strftime(date_str, sizeof(date_str), m->date_format, tm); + + char time_str[1024]; + strftime(time_str, sizeof(time_str), m->time_format, tm); struct tag_set tags = { .tags = (struct tag *[]){tag_new_string(mod, "time", time_str), @@ -74,10 +79,13 @@ run(struct module_run_context *ctx) } struct module * -module_clock(struct particle *label) +module_clock(struct particle *label, + const char *date_format, const char *time_format) { struct private *m = malloc(sizeof(*m)); m->label = label; + m->date_format = strdup(date_format); + m->time_format = strdup(time_format); struct module *mod = module_common_new(); mod->private = m; diff --git a/modules/clock.h b/modules/clock.h index 694ede4..2af48db 100644 --- a/modules/clock.h +++ b/modules/clock.h @@ -3,4 +3,5 @@ #include "../module.h" #include "../particle.h" -struct module *module_clock(struct particle *label); +struct module *module_clock( + struct particle *label, const char *date_format, const char *time_format);