refactor: Mode, View, Bind

This commit is contained in:
i do not exist 2026-07-02 09:21:47 +03:00
parent 51faa82d74
commit 701a651fbd
21 changed files with 901 additions and 880 deletions

View file

@ -1,4 +1,4 @@
use crate::{*, bind::*, view::*};
use crate::*;
/// Configuration: mode, view, and bind definitions.
///
@ -48,12 +48,14 @@ impl Config {
config.init()?;
Ok(config)
}
/// Create a new app configuration from a set of XDG base directories,
pub fn new (dirs: Option<BaseDirectories>) -> Self {
let default = ||BaseDirectories::with_profile(Self::CONFIG_DIR, Self::CONFIG_SUB);
let dirs = dirs.unwrap_or_else(default);
Self { dirs, ..Default::default() }
}
/// Write initial contents of configuration.
pub fn init (&mut self) -> Usually<()> {
self.init_one(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|{
@ -62,6 +64,7 @@ impl Config {
})?;
Ok(())
}
/// Write initial contents of a configuration file.
pub fn init_one (
&mut self, path: &str, defaults: &str, mut each: impl FnMut(&mut Self, &str)->Usually<()>
@ -78,11 +81,13 @@ impl Config {
return Err(format!("{path}: not found").into())
})
}
/// Add statements to configuration from [Dsl] source.
pub fn add (&mut self, dsl: impl Language) -> Usually<&mut Self> {
dsl.each(|item|self.add_one(item))?;
Ok(self)
}
fn add_one (&self, item: impl Language) -> Usually<()> {
if let Some(expr) = item.expr()? {
let head = expr.head()?;
@ -91,7 +96,7 @@ impl Config {
let body = tail.tail()?;
//println!("Config::load: {} {} {}", head.unwrap_or_default(), name.unwrap_or_default(), body.unwrap_or_default());
match head {
Some("mode") if let Some(name) = name => load_mode(&self.modes, &name, &body)?,
Some("mode") if let Some(name) = name => self.modes.add(&name, &body)?,
Some("keys") if let Some(name) = name => load_bind(&self.binds, &name, &body)?,
Some("view") if let Some(name) = name => load_view(&self.views, &name, &body)?,
_ => return Err(format!("Config::load: expected view/keys/mode, got: {item:?}").into())
@ -101,7 +106,12 @@ impl Config {
return Err(format!("Config::load: expected expr, got: {item:?}").into())
}
}
pub fn get_mode (&self, mode: impl AsRef<str>) -> Option<Arc<Mode<Arc<str>>>> {
self.modes.clone().read().unwrap().get(mode.as_ref()).cloned()
self.modes.get(mode)
}
}
mod views; pub use self::views::*;
mod modes; pub use self::modes::*;
mod mode; pub use self::mode::*;