1use ratatui::layout::Constraint;
4use scripting::nomiscript::{Value, format_value};
5
6use crate::tabs::plist::plist_text;
7use crate::tabs::select_list::{SelectList, SelectRow};
8
9pub type ConfigEntry = (String, String);
11
12pub type ConfigTab = SelectList<ConfigEntry>;
14
15impl SelectList<ConfigEntry> {
16 #[must_use]
18 pub fn selected_edit(&self) -> Option<ConfigEntry> {
19 self.selected_row().cloned()
20 }
21}
22
23impl SelectRow for ConfigEntry {
24 const HEADERS: &'static [&'static str] = &["Key", "Value"];
25 const WIDTHS: &'static [Constraint] = &[Constraint::Percentage(40), Constraint::Percentage(60)];
26
27 fn parse(plist: &Value) -> Result<Self, String> {
28 let malformed = || format!("malformed config entry: {}", format_value(plist));
29 let key = plist_text(plist, ":key").ok_or_else(malformed)?;
30 let value = plist_text(plist, ":value").ok_or_else(malformed)?;
31 Ok((key, value))
32 }
33
34 fn key(&self) -> &str {
35 &self.0
36 }
37
38 fn cells(&self) -> Vec<String> {
39 vec![self.0.clone(), self.1.clone()]
40 }
41
42 fn has_key(&self, key: &str) -> bool {
43 self.0.eq_ignore_ascii_case(key)
44 }
45}
46
47#[cfg(test)]
48mod tests;