config: load view (and maybe name/info?)

This commit is contained in:
🪞👃🪞 2025-05-02 21:05:06 +03:00
parent 0efcb7f0fe
commit aefc147347
3 changed files with 72 additions and 49 deletions

71
crates/app/src/config.rs Normal file
View file

@ -0,0 +1,71 @@
use crate::*;
/// Configuration
#[derive(Default, Debug)]
pub struct Configuration {
/// Name of configuration
pub name: Option<Arc<str>>,
/// Description of configuration
pub info: Option<Arc<str>>,
/// View definition
pub view: TokenIter<'static>,
// Input keymap
pub keys: InputMap<'static, Tek, TekCommand, TuiIn, TokenIter<'static>>
}
impl Configuration {
pub fn from_file (path: &impl AsRef<Path>, _watch: bool) -> Usually<Self> {
Self::from_source(String::from_utf8(std::fs::read(path.as_ref())?)?)
}
pub fn from_source (source: impl AsRef<str> + 'static) -> Usually<Self> {
let source: Box<str> = source.as_ref().into();
let source: &'static str = Box::leak(source);
let iter = TokenIter::from(source.as_ref());
let mut name: Option<TokenIter> = None;
let mut info: Option<TokenIter> = None;
let mut view: Option<TokenIter> = None;
let mut keys: Option<TokenIter> = None;
for token in iter {
match token.value {
Value::Exp(_, mut exp) => {
let next = exp.next();
match next {
Some(Token { value: Value::Key(sym), .. }) => match sym {
"name" => name = Some(exp),
"info" => info = Some(exp),
"keys" => keys = Some(exp),
"view" => view = Some(exp),
_ => return Err(format!("(e3) unexpected symbol {sym:?}").into())
},
_ => return Err(format!("(e2) unexpected exp {:?}", next.map(|x|x.value)).into())
}
},
t => return Err(format!("(e1) unexpected token {token:?}").into())
};
}
Ok(Self {
info: info.and_then(|mut x|x.next()).and_then(|x|if let Value::Str(x) = x.value {
Some(x.into())
} else {
None
}),
name: name.and_then(|mut x|x.next()).and_then(|x|if let Value::Str(x) = x.value {
Some(x.into())
} else {
None
}),
view: if let Some(view) = view {
view
} else {
return Err(format!("missing view definition").into())
},
keys: if let Some(keys) = keys {
let mut map = InputMap::default();
// TODO add layers
map
} else {
return Err(format!("missing keys definition").into())
},
})
}
}

View file

@ -37,6 +37,7 @@ pub(crate) use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed};
mod api; pub use self::api::*;
mod audio; pub use self::audio::*;
mod config; pub use self::config::*;
mod model; pub use self::model::*;
mod view; pub use self::view::*;

View file

@ -419,55 +419,6 @@ pub trait HasSelection {
fn selected_mut (&mut self) -> &mut Selection;
}
/// Configuration
#[derive(Default, Debug)]
pub struct Configuration {
/// View definition
pub view: TokenIter<'static>,
// Input keymap
pub keys: InputMap<'static, Tek, TekCommand, TuiIn, TokenIter<'static>>
}
impl Configuration {
pub fn from_file (path: &impl AsRef<Path>, _watch: bool) -> Usually<Self> {
Self::from_source(String::from_utf8(std::fs::read(path.as_ref())?)?)
}
pub fn from_source (source: impl AsRef<str> + 'static) -> Usually<Self> {
let source: Box<str> = source.as_ref().into();
let source: &'static str = Box::leak(source);
let iter = TokenIter::from(source.as_ref());
let mut view: Option<TokenIter> = None;
let mut keys: Option<TokenIter> = None;
for token in iter {
match token.value {
Value::Exp(_, mut exp) => match exp.next() {
Some(Token { value: Value::Sym(sym), .. }) => match sym {
"keys" => keys = Some(exp),
"view" => view = Some(exp),
_ => return Err(format!("unexpected token {token:?}").into())
},
_ => return Err(format!("unexpected token {token:?}").into())
},
t => return Err(format!("unexpected token {token:?}").into())
};
}
Ok(Self {
view: if let Some(view) = view {
view
} else {
return Err(format!("missing view definition").into())
},
keys: if let Some(keys) = keys {
let mut map = InputMap::default();
// TODO add layers
map
} else {
return Err(format!("missing keys definition").into())
},
})
}
}
/// Various possible modal overlays
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum Modal {