From 54bd2e2e7e626a323b8a4d8392ec372035191214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Mon, 10 Feb 2025 17:45:18 +0100 Subject: [PATCH] Auto-generate Requires/Rqd by/Disabled by help --- src/main.rs | 4 +-- src/tui.rs | 62 +++++++++++++++++++++++++++++++++++++++++- template/template.yaml | 4 +-- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0b5f173..52b5405 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,8 +105,8 @@ impl GeneratorOptionItem { fn help(&self) -> &str { match self { - GeneratorOptionItem::Category(category) => category.help.as_str(), - GeneratorOptionItem::Option(option) => option.help.as_str(), + GeneratorOptionItem::Category(category) => &category.help, + GeneratorOptionItem::Option(option) => &option.help, } } } diff --git a/src/tui.rs b/src/tui.rs index 9e5c557..22d4de2 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -416,8 +416,37 @@ impl App { .selected() .min(self.repository.current_level().len() - 1); let option = &self.repository.current_level()[selected]; - // Create a space for header, list, help text and the footer. + + let mut requires = Vec::new(); + let mut required_by = Vec::new(); + let mut disabled_by = Vec::new(); + + self.repository.selected.iter().for_each(|opt| { + let opt = find_option(opt.as_str(), self.repository.options).unwrap(); + for o in opt.requires.iter() { + if let Some(disables) = o.strip_prefix("!") { + if disables == option.name() { + disabled_by.push(opt.name.as_str()); + } + } else if o == option.name() { + required_by.push(o.as_str()); + } + } + }); + for req in option.requires() { + if let Some(disables) = req.strip_prefix("!") { + if self.repository.is_selected(disables) { + disabled_by.push(disables); + } + } else { + requires.push(req); + } + } + let help_text = option.help(); + let help_text = generate_list(help_text, "Requires", &requires); + let help_text = generate_list(&help_text, "Required by", &required_by); + let help_text = generate_list(&help_text, "Disabled by", &disabled_by); if help_text.is_empty() { return None; @@ -468,3 +497,34 @@ impl App { self.footer_paragraph().render(area, buf); } } + +fn generate_list>(base: &str, word: &str, els: &[S]) -> String { + if !els.is_empty() { + let mut requires = String::new(); + + if !base.is_empty() { + requires.push_str(base); + requires.push(' '); + } + + for (i, r) in els.iter().enumerate() { + if i == 0 { + requires.push_str(word); + requires.push(' '); + } else if i == els.len() - 1 { + requires.push_str(" and "); + } else { + requires.push_str(", "); + }; + + requires.push('`'); + requires.push_str(r.as_ref()); + requires.push('`'); + } + requires.push('.'); + + requires + } else { + base.to_string() + } +} diff --git a/template/template.yaml b/template/template.yaml index ec63350..6d1ec47 100644 --- a/template/template.yaml +++ b/template/template.yaml @@ -6,7 +6,7 @@ options: - !Option name: wifi display_name: Enable Wi-Fi via the `esp-wifi` crate. - help: Requires `alloc`. Not available on ESP32-H2. + help: Not available on ESP32-H2. requires: - alloc chips: @@ -20,7 +20,7 @@ options: - !Option name: ble display_name: Enable BLE via the `esp-wifi` crate. - help: Requires `alloc`. Not available on ESP32-S2. + help: Not available on ESP32-S2. requires: - alloc chips: