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;
}
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;
}

View file

@ -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 *

View file

@ -1,5 +1,6 @@
#include "clock.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
@ -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;

View file

@ -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);