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