diff --git a/src/tek.rs b/src/tek.rs index 9337a78e..270b49cd 100644 --- a/src/tek.rs +++ b/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) -> Usually<()> { + pub fn run_with_config <'a> (config: Arc>) -> UsuallyRef<'a, ()> { Cli::parse().run(Some(config)) } /// Command-line configuration. impl Cli { - pub fn run (&self, mut config: Option>) -> Usually<()> { + pub fn run <'a> (&self, mut config: Option>>) -> 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 (callback: impl FnOnce(Arc)->T) -> Usually { + pub fn watched (callback: impl FnOnce(Arc)->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) -> Usually> { + pub fn init_new (_dirs: Option) -> UsuallyRef<'a, Arc> { let config = Arc::new(Self::new(None)); config.init()?; Ok(config) } /// Watch a config's file for changes. - pub fn watch (config: Arc, poll: Option) -> Usually<()> { + pub fn watch (config: Arc, poll: Option) -> 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 Usually<()>> ( + pub fn load 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::, Box>(None) } Ok(Some(())) @@ -447,9 +447,7 @@ mod config { impl<'a> Modes<'a> { /// Register a mode. pub fn add ( - &self, - name: &'a (impl AsRef + ?Sized), - body: &(impl Language + ?Sized) + &self, name: &'a (impl AsRef + ?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 where Self: Namespace, { - if let Some(command) = Namespace::::namespace(state, src)? { + if let Some(command) = Namespace::::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, body: &impl Language + binds: &Binds, name: impl AsRef, 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, body: &'a impl Language, + views: &Views, name: impl AsRef, 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("")).unwrap_or(""); - 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("")).unwrap_or(""); + 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 { east( format!("{}x{} ", self.size.0.load(Relaxed), self.size.1.load(Relaxed)), diff --git a/tengri b/tengri index 291ab632..e69d4287 160000 --- a/tengri +++ b/tengri @@ -1 +1 @@ -Subproject commit 291ab632245c8beb83973da4f5157eb7b40cc7eb +Subproject commit e69d4287e0ee1f751d2f096f7f85b43628cef259