Skip to main content

tui/tabs/config/
mod.rs

1//! Config tab: every key of the user with its value, from `list-config`.
2
3use ratatui::layout::Constraint;
4use scripting::nomiscript::{Value, format_value};
5
6use crate::tabs::plist::plist_text;
7use crate::tabs::select_list::{SelectList, SelectRow};
8
9/// One stored config key with its value.
10pub type ConfigEntry = (String, String);
11
12/// The `list-config` view: exactly the user's stored keys.
13pub type ConfigTab = SelectList<ConfigEntry>;
14
15impl SelectList<ConfigEntry> {
16    /// Key and stored value of the selected row.
17    #[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;