module/clock: user can now specify the date/time format strings

This commit is contained in:
Daniel Eklöf 2019-01-09 18:41:16 +01:00
parent 9037aadd04
commit 7fc9749c28
4 changed files with 33 additions and 8 deletions

View file

@ -313,7 +313,14 @@ verify_module_clock(keychain_t *chain, const struct yml_node *node)
return false; 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)) if (!verify_particle(chain_push(chain, key), it.value))
return false; return false;
} }

View file

@ -423,8 +423,17 @@ static struct module *
module_clock_from_config(const struct yml_node *node, const struct font *parent_font) 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 *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); 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 * static struct module *

View file

@ -1,5 +1,6 @@
#include "clock.h" #include "clock.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <assert.h> #include <assert.h>
@ -9,6 +10,8 @@
struct private { struct private {
struct particle *label; struct particle *label;
char *date_format;
char *time_format;
}; };
static void static void
@ -16,6 +19,8 @@ destroy(struct module *mod)
{ {
struct private *m = mod->private; struct private *m = mod->private;
m->label->destroy(m->label); m->label->destroy(m->label);
free(m->time_format);
free(m->date_format);
free(m); free(m);
module_default_destroy(mod); module_default_destroy(mod);
} }
@ -27,11 +32,11 @@ content(struct module *mod)
time_t t = time(NULL); time_t t = time(NULL);
struct tm *tm = localtime(&t); struct tm *tm = localtime(&t);
char time_str[1024];
strftime(time_str, sizeof(time_str), "%H:%M", tm);
char date_str[1024]; 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 = { struct tag_set tags = {
.tags = (struct tag *[]){tag_new_string(mod, "time", time_str), .tags = (struct tag *[]){tag_new_string(mod, "time", time_str),
@ -74,10 +79,13 @@ run(struct module_run_context *ctx)
} }
struct module * 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)); struct private *m = malloc(sizeof(*m));
m->label = label; m->label = label;
m->date_format = strdup(date_format);
m->time_format = strdup(time_format);
struct module *mod = module_common_new(); struct module *mod = module_common_new();
mod->private = m; mod->private = m;

View file

@ -3,4 +3,5 @@
#include "../module.h" #include "../module.h"
#include "../particle.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);