summaryrefslogtreecommitdiff
path: root/src/til.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2022-04-22 12:19:49 -0700
committerVito Caputo <vcaputo@pengaru.com>2022-04-22 12:19:49 -0700
commit7f872664738b45c982f3e95749f2136a0de4c19a (patch)
tree6e29ea43fba6350e5e413b56d065811455bf720a /src/til.c
parent10fca58ba81c3c9bc14212d4ae4c7bc83deafcec (diff)
til: add til_module_randomize_setup() from rtv
This commit pulls the setup randomizer out of rtv into libtil proper, so other modules may make use of it. Other than adding an assert no functional changes occurred. It may make sense to split this into two functions; one which takes a til_module_t as-is, and a lower-level bare setup function callback based function that doesn't know about til_module_t the former would call into. That way generic setup randomization can occur (the same setup machinery is used in video contexts for example) without necessarily having a til_module_t on hand.
Diffstat (limited to 'src/til.c')
-rw-r--r--src/til.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/til.c b/src/til.c
index 50df146..531c967 100644
--- a/src/til.c
+++ b/src/til.c
@@ -234,3 +234,47 @@ int til_module_setup(til_settings_t *settings, til_setting_t **res_setting, cons
return 0;
}
+
+
+/* originally taken from rtv, this randomizes a module's setup @res_setup, returning args form as well */
+char * til_module_randomize_setup(const til_module_t *module, void **res_setup)
+{
+ til_settings_t *settings;
+ til_setting_t *setting;
+ const til_setting_desc_t *desc;
+ char *arg;
+
+ assert(module);
+
+ if (!module->setup)
+ return NULL;
+
+ settings = til_settings_new(NULL);
+ if (!settings)
+ return NULL;
+
+ while (module->setup(settings, &setting, &desc, res_setup) > 0) {
+ if (desc->random) {
+ char *value;
+
+ value = desc->random();
+ til_settings_add_value(settings, desc->key, value, desc);
+ free(value);
+ } else if (desc->values) {
+ int n;
+
+ for (n = 0; desc->values[n]; n++);
+
+ n = rand() % n;
+
+ til_settings_add_value(settings, desc->key, desc->values[n], desc);
+ } else {
+ til_settings_add_value(settings, desc->key, desc->preferred, desc);
+ }
+ }
+
+ arg = til_settings_as_arg(settings);
+ til_settings_free(settings);
+
+ return arg;
+}
© All Rights Reserved