mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-08-28 04:46:58 +02:00
parent
e6984915e4
commit
816150125d
2 changed files with 47 additions and 41 deletions
86
src/tek.rs
86
src/tek.rs
|
|
@ -76,13 +76,13 @@ fn run_new_plain (config: Config) -> Usually<()> {
|
|||
#[cfg(feature = "cli")] pub mod cli {
|
||||
use crate::*;
|
||||
|
||||
pub fn run_with_config (config: Arc<Config>) -> Usually<()> {
|
||||
pub fn run_with_config <'a> (config: Arc<Config<'a>>) -> UsuallyRef<'a, ()> {
|
||||
Cli::parse().run(Some(config))
|
||||
}
|
||||
|
||||
/// Command-line configuration.
|
||||
impl Cli {
|
||||
pub fn run (&self, mut config: Option<Arc<Config>>) -> Usually<()> {
|
||||
pub fn run <'a> (&self, mut config: Option<Arc<Config<'a>>>) -> UsuallyRef<'a, ()> {
|
||||
if config.is_none() {
|
||||
config = Some(Config::init_new(None)?);
|
||||
}
|
||||
|
|
@ -323,20 +323,20 @@ mod config {
|
|||
}
|
||||
}
|
||||
/// Create, initialize, and watch a new configuration.
|
||||
pub fn watched <T> (callback: impl FnOnce(Arc<Self>)->T) -> Usually<T> {
|
||||
pub fn watched <T> (callback: impl FnOnce(Arc<Self>)->T) -> UsuallyRef<'a, T> {
|
||||
let config = Self::init_new(None)?;
|
||||
Self::watch(config.clone(), None)?;
|
||||
let result = callback(config);
|
||||
Ok(result)
|
||||
}
|
||||
/// Create and initialize a new configuration.
|
||||
pub fn init_new (_dirs: Option<BaseDirectories>) -> Usually<Arc<Self>> {
|
||||
pub fn init_new (_dirs: Option<BaseDirectories>) -> UsuallyRef<'a, Arc<Self>> {
|
||||
let config = Arc::new(Self::new(None));
|
||||
config.init()?;
|
||||
Ok(config)
|
||||
}
|
||||
/// Watch a config's file for changes.
|
||||
pub fn watch (config: Arc<Self>, poll: Option<Duration>) -> Usually<()> {
|
||||
pub fn watch (config: Arc<Self>, poll: Option<Duration>) -> UsuallyRef<'a, ()> {
|
||||
let handler = {
|
||||
let config = config.clone();
|
||||
move |result|match result {
|
||||
|
|
@ -378,18 +378,18 @@ mod config {
|
|||
self.dirs.get_config_file(Self::CONFIG)
|
||||
}
|
||||
/// Write initial contents of configuration.
|
||||
pub fn init (&self) -> Usually<()> {
|
||||
pub fn init (&self) -> UsuallyRef<'a, ()> {
|
||||
//println!("\r\ninit {}", quanta::Clock::new().raw());
|
||||
self.clear();
|
||||
self.load(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|{
|
||||
self.load(Self::CONFIG, Self::DEFAULTS, move|cfgs, dsl|{
|
||||
cfgs.add(&dsl)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
/// Write initial contents of a configuration file.
|
||||
pub fn load <F: FnMut(&Self, &str)->Usually<()>> (
|
||||
pub fn load <F: FnMut(&Self, &str)->UsuallyRef<'a, ()>> (
|
||||
&self, path: &str, defaults: &str, mut each: F
|
||||
) -> UsuallyRef<()> {
|
||||
) -> UsuallyRef<'a, ()> {
|
||||
self.stamp.store(quanta::Clock::new().raw(), Relaxed);
|
||||
if self.find_file().is_none() {
|
||||
//println!("Creating {path:?}");
|
||||
|
|
@ -404,7 +404,7 @@ mod config {
|
|||
})
|
||||
}
|
||||
/// Add statements to configuration from [Dsl] source.
|
||||
pub fn add (&self, dsl: impl Language) -> Usually<&Self> {
|
||||
pub fn add (&'a self, dsl: impl Language) -> UsuallyRef<'a, &Self> {
|
||||
dsl.each(|item|self.add_one(item))?;
|
||||
Ok(self)
|
||||
}
|
||||
|
|
@ -423,9 +423,9 @@ mod config {
|
|||
let name = tail.head()?;
|
||||
let body = tail.tail()?;
|
||||
match head {
|
||||
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)?,
|
||||
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 Ok::<Option<()>, Box<dyn Error>>(None)
|
||||
}
|
||||
Ok(Some(()))
|
||||
|
|
@ -447,9 +447,7 @@ mod config {
|
|||
impl<'a> Modes<'a> {
|
||||
/// Register a mode.
|
||||
pub fn add (
|
||||
&self,
|
||||
name: &'a (impl AsRef<str> + ?Sized),
|
||||
body: &(impl Language + ?Sized)
|
||||
&self, name: &'a (impl AsRef<str> + ?Sized), body: impl Language
|
||||
)
|
||||
-> UsuallyRef<'a, ()>
|
||||
{
|
||||
|
|
@ -526,9 +524,9 @@ mod config {
|
|||
})?))
|
||||
}
|
||||
/// Add a submode to the mode.
|
||||
fn add_mode (&mut self, dsl: &'a (impl Language + ?Sized)) -> PerhapsRef<'a, ()> {
|
||||
fn add_mode (&mut self, dsl: &'a (impl Language + ?Sized + 'a)) -> PerhapsRef<'a, ()> {
|
||||
Ok(Some(if let Some(id) = dsl.head()? {
|
||||
self.modes.add(&id, &dsl.tail())?;
|
||||
self.modes.add(id, &dsl.tail())?;
|
||||
} else {
|
||||
return Err(format!("Mode::add: self: incomplete: {dsl:?}").into());
|
||||
}))
|
||||
|
|
@ -860,7 +858,7 @@ mod bind {
|
|||
> (state: &mut Self, src: &str) -> Usually<bool> where
|
||||
Self: Namespace<T>,
|
||||
{
|
||||
if let Some(command) = Namespace::<T>::namespace(state, src)? {
|
||||
if let Some(command) = Namespace::<T>::namespace(state, &src)? {
|
||||
match command.clone().dispatch(state) {
|
||||
Err(err) => {
|
||||
state.history.push((command.into(), None));
|
||||
|
|
@ -986,9 +984,9 @@ mod bind {
|
|||
}
|
||||
|
||||
pub(crate) fn load_bind <'a> (
|
||||
binds: &Binds, name: &impl AsRef<str>, body: &impl Language
|
||||
binds: &Binds, name: impl AsRef<str>, body: impl Language
|
||||
) -> Usually<()> {
|
||||
binds.write().unwrap().insert(name.as_ref().into(), Bind::load(body)?);
|
||||
binds.write().unwrap().insert(name.as_ref().into(), Bind::load(&body)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -1313,7 +1311,7 @@ mod draw {
|
|||
|
||||
/// Load custom view definition.
|
||||
pub(crate) fn load_view <'a> (
|
||||
views: &Views, name: &impl AsRef<str>, body: &'a impl Language,
|
||||
views: &Views, name: impl AsRef<str>, body: impl Language,
|
||||
) -> UsuallyRef<'a, ()> {
|
||||
views.write().unwrap().insert(
|
||||
name.as_ref().into(),
|
||||
|
|
@ -1332,7 +1330,7 @@ mod draw {
|
|||
//self.perf.cycle(&mut |_|{
|
||||
draw(|to: &mut Tui|{
|
||||
self.draw_error(to)?;
|
||||
self.draw_mode(to)?;
|
||||
self.draw_modes(to)?;
|
||||
//self.draw_debug(to)?;
|
||||
Ok(Some(to.area().into()))
|
||||
})
|
||||
|
|
@ -1348,31 +1346,39 @@ mod draw {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_mode <'a> (&'a self, to: &mut Tui) -> UsuallyRef<'a, ()> {
|
||||
fn draw_modes <'a> (&'a self, to: &mut Tui) -> UsuallyRef<'a, ()> {
|
||||
if let Some(mode) = self.mode.as_ref().and_then(|m|self.config.get_mode(m)) {
|
||||
let mut error = false;
|
||||
for (index, dsl) in mode.view.iter().enumerate() {
|
||||
match self.interpret(to, dsl) {
|
||||
Ok(Some(XYWH(.., w, h))) => {
|
||||
self.size.0.store(w as usize, Relaxed);
|
||||
self.size.1.store(h as usize, Relaxed);
|
||||
},
|
||||
Err(e) => {
|
||||
let src = &dsl.src().unwrap_or(Some("<source error>")).unwrap_or("<no source>");
|
||||
let message = format!(
|
||||
"Mode: {:?}\n\nLayer: #{index}\n\nError: {e}\n\nSource:\n{src}",
|
||||
&mode.name
|
||||
);
|
||||
*self.error.write().unwrap() = Some(message.into());
|
||||
return Err(e);
|
||||
},
|
||||
_ => {}
|
||||
if let Err(e) = self.draw_mode(to, dsl) {
|
||||
let src = &dsl.src().unwrap_or(Some("<source error>")).unwrap_or("<no source>");
|
||||
let message = format!(
|
||||
"Mode: {:?}\n\nLayer: #{index}\n\nError: {e}\n\nSource:\n{src}",
|
||||
&mode.name
|
||||
);
|
||||
*self.error.write().unwrap() = Some(message.into());
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
*self.error.write().unwrap() = None;
|
||||
if !error {
|
||||
*self.error.write().unwrap() = None;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_mode <'a> (&'a self, to: &mut Tui, dsl: &'a impl Language) -> UsuallyRef<'a, ()> {
|
||||
Ok(match self.interpret(to, dsl) {
|
||||
Err(e) => return Err(e),
|
||||
Ok(None) => {},
|
||||
Ok(Some(XYWH(.., w, h))) => {
|
||||
self.size.0.store(w as usize, Relaxed);
|
||||
self.size.1.store(h as usize, Relaxed);
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
fn draw_debug (&self, to: &mut Tui) -> Drawn<u16> {
|
||||
east(
|
||||
format!("{}x{} ", self.size.0.load(Relaxed), self.size.1.load(Relaxed)),
|
||||
|
|
|
|||
2
tengri
2
tengri
|
|
@ -1 +1 @@
|
|||
Subproject commit 291ab632245c8beb83973da4f5157eb7b40cc7eb
|
||||
Subproject commit e69d4287e0ee1f751d2f096f7f85b43628cef259
|
||||
Loading…
Add table
Add a link
Reference in a new issue