mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
This commit is contained in:
parent
fcfb7a0915
commit
3dada45ea9
2 changed files with 56 additions and 75 deletions
|
|
@ -109,83 +109,64 @@ impl Config {
|
|||
return Err(format!("{path}: not found").into())
|
||||
})
|
||||
}
|
||||
pub fn load_defs <D: Dsl> (&mut self, dsl: D) -> Usually<()> {
|
||||
pub fn load_defs (&mut self, dsl: impl Dsl) -> Usually<()> {
|
||||
dsl.each(|item|{
|
||||
println!("{item:?}");
|
||||
Ok(match item.exp().head() {
|
||||
Ok(Some("keys")) if let Some(id) = item.exp().tail().head()? => {
|
||||
self.binds.write().unwrap().insert(id.into(), {
|
||||
let mut map = EventMap::new();
|
||||
item.exp().tail().tail()?.each(|item|Ok({
|
||||
if let Ok(Some(sym)) = item.exp().head().sym() {
|
||||
map.add(TuiEvent::from_dsl(item.exp()?.head()?)?, Binding {
|
||||
command: item.exp()?.tail()?.unwrap_or_default().into(),
|
||||
condition: None,
|
||||
description: None,
|
||||
source: None
|
||||
});
|
||||
} else if item.exp().head() == Ok(Some("see")) {
|
||||
// TODO
|
||||
} else {
|
||||
return Err(format!("load_defs: unexpected: {item:?}").into())
|
||||
}
|
||||
}))?;
|
||||
map
|
||||
});
|
||||
},
|
||||
Ok(Some("mode")) if let Some(id) = item.exp().tail().head()? => {
|
||||
self.modes.write().unwrap().insert(id.into(), {
|
||||
let mut mode = Mode::default();
|
||||
item.exp().tail().tail()?.each(|item|Ok(if let Ok(Some(exp)) = item.exp() {
|
||||
match exp.head()? {
|
||||
Some("name") => mode.name.push(
|
||||
exp.tail()?.map(|x|x.trim()).unwrap_or("").into()
|
||||
),
|
||||
Some("info") => mode.info.push(
|
||||
exp.tail()?.map(|x|x.trim()).unwrap_or("").into()
|
||||
),
|
||||
Some("keys") => if let Some(tail) = exp.tail()? {
|
||||
tail.each(|keys|Ok(mode.keys.push(keys.trim().into())))?;
|
||||
} else {
|
||||
return Err(format!("load_view: empty keys: {exp}").into())
|
||||
},
|
||||
Some("mode") => if let (Some(name), Some(tail)) = (
|
||||
exp.tail()?.head()?, exp.tail()?.tail()?,
|
||||
) {
|
||||
let mut submode: Mode<Arc<str>> = Default::default();
|
||||
tail.each(|item|Ok(if let Ok(Some(exp)) = item.exp() {
|
||||
match exp.head()? {
|
||||
Some("keys") => if let Some(tail) = exp.tail()? {
|
||||
tail.each(|keys|Ok(mode.keys.push(keys.trim().into())))?;
|
||||
} else {
|
||||
return Err(format!("load_view: empty keys: {exp}").into())
|
||||
},
|
||||
_ => {
|
||||
return Err(format!("load_view: unexpected in mode {name}: {item:?}").into())
|
||||
}
|
||||
}
|
||||
} else if let Ok(Some(sym)) = item.sym() {
|
||||
// TODO
|
||||
} else {
|
||||
return Err(format!("load_view: unexpected in mode {name}: {item:?}").into())
|
||||
}))?;
|
||||
mode.modes.insert(name.trim().into(), submode);
|
||||
} else {
|
||||
return Err(format!("load_view: empty mode: {exp}").into())
|
||||
},
|
||||
Some(_) => mode.view.push(exp.into()),
|
||||
None => return Err(format!("load_view: empty: {exp}").into())
|
||||
}
|
||||
} else if let Ok(Some(sym)) = item.sym() {
|
||||
mode.view.push(sym.into());
|
||||
} else {
|
||||
return Err(format!("load_view: unexpected: {dsl:?}").into())
|
||||
}))?;
|
||||
mode.into()
|
||||
});
|
||||
},
|
||||
match item.exp().head() {
|
||||
Ok(Some("keys")) if let Some(id) = item.exp().tail().head()? =>
|
||||
self.load_bind(id.into(), item),
|
||||
Ok(Some("mode")) if let Some(id) = item.exp().tail().head()? =>
|
||||
self.load_mode(id.into(), item),
|
||||
_ => return Err(format!("load_defs: unexpected: {item:?}").into())
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
pub fn load_bind (&mut self, id: Arc<str>, item: impl Dsl) -> Usually<()> {
|
||||
let mut map = EventMap::new();
|
||||
item.exp().tail().tail()?.each(|item|Self::load_bind_one(&mut map, item))?;
|
||||
self.binds.write().unwrap().insert(id, map);
|
||||
Ok(())
|
||||
}
|
||||
fn load_bind_one (map: &mut EventMap<Option<TuiEvent>, Arc<str>>, item: impl Dsl) -> Usually<()> {
|
||||
if let Ok(Some(sym)) = item.exp().head().sym() {
|
||||
map.add(TuiEvent::from_dsl(item.exp()?.head()?)?, Binding {
|
||||
command: item.exp()?.tail()?.unwrap_or_default().into(),
|
||||
condition: None,
|
||||
description: None,
|
||||
source: None
|
||||
});
|
||||
} else if item.exp().head() == Ok(Some("see")) {
|
||||
// TODO
|
||||
} else {
|
||||
return Err(format!("load_defs: unexpected: {item:?}").into())
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn load_mode (&mut self, id: Arc<str>, item: impl Dsl) -> Usually<()> {
|
||||
let mut mode = Mode::default();
|
||||
item.exp().tail().tail()?.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<()> {
|
||||
Ok(if let Ok(Some(key)) = item.exp().head() {
|
||||
match key {
|
||||
"name" => mode.name.push(item.exp()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
|
||||
"info" => mode.info.push(item.exp()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
|
||||
"keys" => mode.keys.push(item.exp()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
|
||||
"mode" => if let Some(id) = item.exp()?.tail()?.head()? {
|
||||
let mut submode = Mode::default();
|
||||
Self::load_mode_one(&mut submode, item.exp()?.tail()?.tail()?)?;
|
||||
mode.modes.insert(id.into(), submode);
|
||||
} else {
|
||||
return Err(format!("load_mode_one: incomplete: {item:?}").into());
|
||||
},
|
||||
_ => mode.view.push(item.exp()?.unwrap().into()),
|
||||
}
|
||||
} else if let Ok(Some(sym)) = item.sym() {
|
||||
mode.view.push(sym.into());
|
||||
} else {
|
||||
return Err(format!("load_mode_one: unexpected: {item:?}").into());
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue