Skip to content

Commit

Permalink
Auto-generate Requires/Rqd by/Disabled by help
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Feb 10, 2025
1 parent ea27137 commit 54bd2e2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
62 changes: 61 additions & 1 deletion src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -468,3 +497,34 @@ impl App {
self.footer_paragraph().render(area, buf);
}
}

fn generate_list<S: AsRef<str>>(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()
}
}
4 changes: 2 additions & 2 deletions template/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 54bd2e2

Please sign in to comment.