From ce5d4d8bf226ec2fd7177427f490893e5d262419 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 9 May 2023 17:44:07 -0700 Subject: til: teach til_randomize_setup() about nested settings This probably needs more work, but it's good enough for now --- src/til.c | 47 ++++++++++++++++++++++++++++++++++------------- 1 file 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; } } -- cgit v1.2.3