add (data) to config

This commit is contained in:
i do not exist 2026-10-05 16:47:07 +03:00
parent 353a20bab9
commit b6f7d2d361
6 changed files with 142 additions and 96 deletions

View file

@ -1,20 +1,5 @@
use crate::*;
/// Has a stack of currently active interaction modes.
pub trait HasModeStack {
/// Stack of active modes.
fn mode_stack (&self) -> &RwLock<Vec<Arc<str>>>;
fn mode_stack_push (&self, mode: impl AsRef<str>) {
self.mode_stack().write().push(mode.as_ref().into())
}
}
impl HasModeStack for App {
fn mode_stack (&self) -> &RwLock<Vec<Arc<str>>> {
&self.stack
}
}
/// Collection of UI modes.
///
/// ```
@ -37,13 +22,11 @@ pub trait HasModes {
///
/// ```
/// # use ::{std::sync::Arc, tek::*};
/// let config: Config = Default::default();
///
/// let config = Config::default();
/// config.modes_add(stringify!(Menu
/// (name Menu) (info Mode selector.) (axis Y Index) (bind Confirm)
/// (view (bg (g 30) (bar/s 7 Logo (full/xy Dialog))))
/// )).unwrap();
///
/// let _: Arc<Mode> = config.modes_get("Menu").unwrap();
/// ```
fn modes_add <'a> (&self, expr: impl Language) -> UsuallyRef<'a, ()> {
@ -104,6 +87,7 @@ pub struct Mode {
pub view: Vec<Arc<View<Tui, App>>>,
pub keys: Vec<Arc<str>>,
pub axis: BTreeMap<Arc<str>, Arc<str>>,
pub data: BTreeMap<Arc<str>, usize>,
pub modes: Arc<RwLock<BTreeMap<Arc<str>, Arc<Mode>>>>,
}
@ -128,6 +112,7 @@ impl Mode {
"self" => self.add_mode(tail)?,
"bind" => self.add_bind(tail)?,
"axis" => self.add_axis(tail)?,
"data" => self.add_data(tail)?,
"name" => { self.name.push(tail.into()); self },
"info" => { self.info.push(tail.into()); self },
"view" => { self.view.push(View::new(tail)?.into()); self },
@ -167,13 +152,33 @@ impl Mode {
param = Some(src.into());
Ok(2)
} else {
err!("expected (mode (axis [Name] [command] [command])), got: {tail}")
err!("mode: expected (axis Name Param), got: {tail}")
})?;
if let Some(axis) = axis && let Some(param) = param {
self.axis.insert(axis.into(), param.into());
Ok(self)
} else {
err!("mode: expected (axis X Param), got: {tail}")
err!("mode: expected (axis Name Param), got: {tail}")
}
}
fn add_data (&mut self, tail: &str) -> Usually<&mut Self> {
let mut param: Option<Arc<str>> = None;
let mut value: Option<usize> = None;
tail.each(0, |index: usize, src: &str|if index == 0 {
param = Some(src.into());
Ok(1)
} else if index == 1 {
value = Some(src.parse::<usize>()?);
Ok(2)
} else {
err!("mode: expected (data Param Value), got: {tail}")
})?;
if let Some(param) = param && let Some(value) = value {
self.data.insert(param.into(), value.into());
Ok(self)
} else {
err!("mode: expected (data Param Value), got: {tail}")
}
}