mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
use def_command
This commit is contained in:
parent
5ccbb9719f
commit
cfd19062fd
37 changed files with 1578 additions and 1594 deletions
|
|
@ -20,11 +20,15 @@ use ::{
|
|||
#[derive(Default, Debug)]
|
||||
pub struct Config {
|
||||
pub dirs: BaseDirectories,
|
||||
pub modes: Arc<RwLock<BTreeMap<Arc<str>, Arc<Mode<Arc<str>>>>>>,
|
||||
pub views: Arc<RwLock<BTreeMap<Arc<str>, Arc<str>>>>,
|
||||
pub binds: Arc<RwLock<BTreeMap<Arc<str>, EventMap<TuiEvent, Arc<str>>>>>,
|
||||
pub modes: Modes,
|
||||
pub views: Views,
|
||||
pub binds: Binds,
|
||||
}
|
||||
|
||||
type Modes = Arc<RwLock<BTreeMap<Arc<str>, Arc<Mode<Arc<str>>>>>>;
|
||||
type Binds = Arc<RwLock<BTreeMap<Arc<str>, EventMap<TuiEvent, Arc<str>>>>>;
|
||||
type Views = Arc<RwLock<BTreeMap<Arc<str>, Arc<str>>>>;
|
||||
|
||||
/// A set of currently active view and keys definitions,
|
||||
/// with optional name and description.
|
||||
#[derive(Default, Debug)]
|
||||
|
|
@ -34,7 +38,7 @@ pub struct Mode<D: Dsl + Ord> {
|
|||
pub info: Vec<D>,
|
||||
pub view: Vec<D>,
|
||||
pub keys: Vec<D>,
|
||||
pub modes: BTreeMap<D, Mode<D>>,
|
||||
pub modes: Modes,
|
||||
}
|
||||
|
||||
/// A collection of input bindings.
|
||||
|
|
@ -62,11 +66,16 @@ pub struct Condition(Arc<Box<dyn Fn()->bool + Send + Sync>>);
|
|||
impl Config {
|
||||
const CONFIG: &'static str = "tek.edn";
|
||||
const DEFAULTS: &'static str = include_str!("../../tek.edn");
|
||||
pub fn init () -> Usually<Self> {
|
||||
let mut cfgs: Self = Default::default();
|
||||
cfgs.dirs = BaseDirectories::with_profile("tek", "v0");
|
||||
cfgs.init_file(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|cfgs.load(dsl))?;
|
||||
Ok(cfgs)
|
||||
|
||||
pub fn new (dirs: Option<BaseDirectories>) -> Self {
|
||||
Self {
|
||||
dirs: dirs.unwrap_or_else(||BaseDirectories::with_profile("tek", "v0")),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
pub fn init (&mut self) -> Usually<()> {
|
||||
self.init_file(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|cfgs.load(dsl))?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn init_file (
|
||||
&mut self, path: &str, defaults: &str, mut each: impl FnMut(&mut Self, &str)->Usually<()>
|
||||
|
|
@ -89,74 +98,59 @@ impl Config {
|
|||
let tail = expr.tail()?;
|
||||
let name = tail.head()?;
|
||||
let body = tail.tail()?;
|
||||
println!("{} {} {}", head.unwrap_or_default(), name.unwrap_or_default(), body.unwrap_or_default());
|
||||
println!("Config::load: {} {} {}", head.unwrap_or_default(), name.unwrap_or_default(), body.unwrap_or_default());
|
||||
match head {
|
||||
Some("view") if let Some(name) = name => self.load_view(name.into(), body),
|
||||
Some("keys") if let Some(name) = name => self.load_bind(name.into(), body),
|
||||
Some("mode") if let Some(name) = name => self.load_mode(name.into(), body),
|
||||
Some("mode") if let Some(name) = name =>
|
||||
Mode::<Arc<str>>::load_into(&self.modes, &name, &body)?,
|
||||
Some("keys") if let Some(name) = name =>
|
||||
EventMap::<TuiEvent, Arc<str>>::load_into(&self.binds, &name, &body)?,
|
||||
Some("view") if let Some(name) = name => {
|
||||
self.views.write().unwrap().insert(name.into(), body.src()?.unwrap_or_default().into());
|
||||
},
|
||||
_ => return Err(format!("Config::load: expected view/keys/mode, got: {item:?}").into())
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
return Err(format!("Config::load: expected expr, got: {item:?}").into())
|
||||
})
|
||||
}
|
||||
pub fn load_view (&mut self, id: Arc<str>, dsl: impl Dsl) -> Usually<()> {
|
||||
self.views.write().unwrap().insert(id, dsl.src()?.unwrap_or_default().into());
|
||||
}
|
||||
|
||||
impl Mode<Arc<str>> {
|
||||
pub fn load_into (modes: &Modes, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
|
||||
let mut mode = Self::default();
|
||||
println!("Mode::load_into: {}: {body:?}", name.as_ref());
|
||||
body.each(|item|mode.load_one(item))?;
|
||||
modes.write().unwrap().insert(name.as_ref().into(), Arc::new(mode));
|
||||
Ok(())
|
||||
}
|
||||
pub fn load_bind (&mut self, id: Arc<str>, dsl: impl Dsl) -> Usually<()> {
|
||||
let mut map = EventMap::new();
|
||||
dsl.each(|item|if item.expr().head() == Ok(Some("see")) {
|
||||
// TODO
|
||||
Ok(())
|
||||
} else if let Ok(Some(word)) = item.expr().head().word() {
|
||||
if let Some(key) = TuiEvent::from_dsl(item.expr()?.head()?)? {
|
||||
map.add(key, Binding {
|
||||
commands: [item.expr()?.tail()?.unwrap_or_default().into()].into(),
|
||||
condition: None,
|
||||
description: None,
|
||||
source: None
|
||||
});
|
||||
Ok(())
|
||||
} else if Some(":char") == item.expr()?.head()? {
|
||||
// TODO
|
||||
return Ok(())
|
||||
} else {
|
||||
return Err(format!("Config::load_bind: invalid key: {:?}", item.expr()?.head()?).into())
|
||||
}
|
||||
} else {
|
||||
return Err(format!("Config::load_bind: unexpected: {item:?}").into())
|
||||
})?;
|
||||
self.binds.write().unwrap().insert(id, map);
|
||||
Ok(())
|
||||
}
|
||||
pub fn load_mode (&mut self, id: Arc<str>, dsl: impl Dsl) -> Usually<()> {
|
||||
let mut mode = Mode::default();
|
||||
dsl.each(|item|Self::load_mode_one(&mut mode, item))?;
|
||||
self.modes.write().unwrap().insert(id.into(), Arc::new(mode));
|
||||
Ok(())
|
||||
}
|
||||
pub fn load_mode_one (mode: &mut Mode<Arc<str>>, item: impl Dsl) -> Usually<()> {
|
||||
pub fn load_one (&mut self, item: impl Dsl) -> Usually<()> {
|
||||
Ok(if let Ok(Some(key)) = item.expr().head() {
|
||||
match key {
|
||||
"name" => mode.name.push(item.expr()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
|
||||
"info" => mode.info.push(item.expr()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
|
||||
"name" => {
|
||||
self.name.push(item.tail()?.map(|x|x.trim()).unwrap_or("").into())
|
||||
},
|
||||
"info" => {
|
||||
self.info.push(item.tail()?.map(|x|x.trim()).unwrap_or("").into())
|
||||
},
|
||||
"keys" => {
|
||||
item.expr()?.tail()?.each(|item|{mode.keys.push(item.trim().into()); Ok(())})?;
|
||||
item.tail()?.each(|item|{
|
||||
self.keys.push(item.trim().into());
|
||||
Ok(())
|
||||
})?;
|
||||
},
|
||||
"mode" => if let Some(id) = item.expr()?.tail()?.head()? {
|
||||
let mut submode = Mode::default();
|
||||
Self::load_mode_one(&mut submode, item.expr()?.tail()?.tail()?)?;
|
||||
mode.modes.insert(id.into(), submode);
|
||||
"self" => if let Some(id) = item.tail()?.head()? {
|
||||
Self::load_into(&self.modes, &id, &item.tail().tail())?;
|
||||
} else {
|
||||
return Err(format!("load_mode_one: incomplete: {item:?}").into());
|
||||
return Err(format!("Mode::load_one: self: incomplete: {item:?}").into());
|
||||
},
|
||||
_ => mode.view.push(item.expr()?.unwrap().into()),
|
||||
}
|
||||
_ if let Some(src) = item.src()? => self.view.push(src.into()),
|
||||
_ => {},
|
||||
};
|
||||
} else if let Ok(Some(word)) = item.word() {
|
||||
mode.view.push(word.into());
|
||||
self.view.push(word.into());
|
||||
} else {
|
||||
return Err(format!("load_mode_one: unexpected: {item:?}").into());
|
||||
return Err(format!("Mode::load_one: unexpected: {item:?}").into());
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -196,6 +190,36 @@ impl<E: Clone + Ord, C> EventMap<E, C> {
|
|||
}
|
||||
}
|
||||
|
||||
impl EventMap<TuiEvent, Arc<str>> {
|
||||
pub fn load_into (binds: &Binds, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
|
||||
println!("EventMap::load_into: {}: {body:?}", name.as_ref());
|
||||
let mut map = Self::new();
|
||||
body.each(|item|if item.expr().head() == Ok(Some("see")) {
|
||||
// TODO
|
||||
Ok(())
|
||||
} else if let Ok(Some(word)) = item.expr().head().word() {
|
||||
if let Some(key) = TuiEvent::from_dsl(item.expr()?.head()?)? {
|
||||
map.add(key, Binding {
|
||||
commands: [item.expr()?.tail()?.unwrap_or_default().into()].into(),
|
||||
condition: None,
|
||||
description: None,
|
||||
source: None
|
||||
});
|
||||
Ok(())
|
||||
} else if Some(":char") == item.expr()?.head()? {
|
||||
// TODO
|
||||
return Ok(())
|
||||
} else {
|
||||
return Err(format!("Config::load_bind: invalid key: {:?}", item.expr()?.head()?).into())
|
||||
}
|
||||
} else {
|
||||
return Err(format!("Config::load_bind: unexpected: {item:?}").into())
|
||||
})?;
|
||||
binds.write().unwrap().insert(name.as_ref().into(), map);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> Binding<C> {
|
||||
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
|
||||
let command: Option<C> = None;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue