Lines
100 %
Functions
50 %
Branches
//! Scripts tab: the user's scripts, from `list-scripts`.
use ratatui::layout::Constraint;
use scripting::nomiscript::{Value, format_value};
use crate::tabs::plist::{plist_flag, plist_text};
use crate::tabs::select_list::{SelectList, SelectRow};
/// One script as the server lists it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScriptRow {
pub id: String,
pub name: String,
pub enabled: bool,
pub has_source: bool,
}
/// The scripts view: exactly the scripts the user holds.
pub type ScriptsTab = SelectList<ScriptRow>;
impl ScriptRow {
fn state(&self) -> String {
let state = if self.enabled { "enabled" } else { "disabled" };
match self.has_source {
true => state.to_string(),
false => format!("{state}, no source"),
impl SelectRow for ScriptRow {
const HEADERS: &'static [&'static str] = &["Name", "State"];
const WIDTHS: &'static [Constraint] = &[Constraint::Fill(1), Constraint::Length(24)];
fn parse(plist: &Value) -> Result<Self, String> {
let malformed = || format!("malformed script: {}", format_value(plist));
let name = plist_text(plist, ":name").ok_or_else(malformed)?;
Ok(Self {
id: plist_text(plist, ":id").ok_or_else(malformed)?,
name: if name.is_empty() {
"unnamed".to_string()
} else {
name
},
enabled: plist_flag(plist, ":enabled").ok_or_else(malformed)?,
has_source: plist_flag(plist, ":has-source").ok_or_else(malformed)?,
})
fn key(&self) -> &str {
&self.id
fn cells(&self) -> Vec<String> {
vec![self.name.clone(), self.state()]
#[cfg(test)]
mod tests;