summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-05-09 17:44:07 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-05-11 15:19:25 -0700
commitce5d4d8bf226ec2fd7177427f490893e5d262419 (patch)
tree6194019d6d6635c0d6d304786e05b1580b8da59b
parentf4934be45ed47c7858c3bedc2d16794e011eb403 (diff)
til: teach til_randomize_setup() about nested settings
This probably needs more work, but it's good enough for now
-rw-r--r--src/til.c47
1 files changed, 34 insertions, 13 deletions
diff --git a/src/til.c b/src/til.c
index f5c2e8f..aae1875 100644
--- a/src/til.c
+++ b/src/til.c
@@ -332,23 +332,44 @@ int til_module_randomize_setup(const til_module_t *module, unsigned seed, til_se
if (!settings)
return -ENOMEM;
- while (module->setup(settings, &setting, &desc, res_setup) > 0) {
- if (desc->spec.random) {
- char *value;
+ for (setting = NULL; module->setup(settings, &setting, &desc, res_setup) > 0; setting = NULL) {
+ if (!setting) {
+ if (desc->spec.random) {
+ char *value;
- value = desc->spec.random(rand_r(&seed));
- til_settings_add_value(desc->container, desc->spec.key, value, desc);
- free(value);
- } else if (desc->spec.values) {
- int n;
+ value = desc->spec.random(rand_r(&seed));
+ setting = til_settings_add_value(desc->container, desc->spec.key, value, desc);
+ free(value);
+ } else if (desc->spec.values) {
+ int n;
- for (n = 0; desc->spec.values[n]; n++);
+ for (n = 0; desc->spec.values[n]; n++);
- n = rand_r(&seed) % n;
+ n = rand_r(&seed) % n;
- til_settings_add_value(desc->container, desc->spec.key, desc->spec.values[n], desc);
- } else {
- til_settings_add_value(desc->container, desc->spec.key, desc->spec.preferred, desc);
+ setting = til_settings_add_value(desc->container, desc->spec.key, desc->spec.values[n], desc);
+ } else {
+ setting = til_settings_add_value(desc->container, desc->spec.key, desc->spec.preferred, desc);
+ }
+ }
+
+ assert(setting);
+
+ if (desc->spec.as_nested_settings && !setting->value_as_nested_settings) {
+ char *label = NULL;
+
+ if (!desc->spec.key) {
+ /* generate a positional label for bare-value specs */
+ r = til_settings_label_setting(desc->container, setting, &label);
+ if (r < 0)
+ return r;
+ }
+
+ setting->value_as_nested_settings = til_settings_new(desc->spec.key ? : label, setting->value);
+ free(label);
+
+ if (!setting->value_as_nested_settings)
+ return -ENOMEM;
}
}
© All Rights Reserved