wip: more edn rendering setup

This commit is contained in:
🪞👃🪞 2025-01-05 04:48:01 +01:00
parent 174a7ee614
commit f1b3fc0040
10 changed files with 85 additions and 47 deletions

View file

@ -1 +1 @@
:test :hello

View file

@ -1,8 +1,20 @@
use tek_edn::*; use tek_edn::{*, tek_layout::{*, tek_engine::{*, tui::{*, TuiRun}}}};
use std::sync::{Arc, RwLock};
const SOURCE: &'static str = include_str!("edn01.edn"); const EDN: &'static str = include_str!("edn01.edn");
fn main () { fn main () -> Usually<()> {
Tui::run(Arc::new(RwLock::new(Demo::new())))?; let state = Arc::new(RwLock::new(Example));
Tui::new().unwrap().run(&state)?;
Ok(()) Ok(())
} }
pub struct Example;
impl EdnLayout<Tui> for Example {
fn get_content <'a> (&'a self, sym: &'a str) -> Box<EdnRender<'a, Tui>> {
Box::new(Thunk::new(move||if sym == ":hello" { "Hello world!" } else { "" }))
}
}
impl Handle<Tui> for Example {}

View file

@ -1,12 +1 @@
(sized (bsp/s :hello :world)
(bsp/s (fill/x (fixed/y 2 (lay
(align/w :input-meter-l)
(align/e :input-meter-r)
(align/x :transport))))
(bsp/n (row :clip-play :clip-next :clip-edit :edit-stat)
(bsp/n (max/y :sample-h (fill/xy :sample-view))
(bsp/n (align/w (fixed/y 1 :sample-stat))
(bsp/n (fixed/x :pool-w :pool-view)
(fill/xy (bsp/e
(fixed/x :samples-w (push/y :samples-y :samples-view))
:midi-view))))))))

View file

@ -1,8 +1,20 @@
use tek_edn::*; use tek_edn::{*, tek_layout::{*, tek_engine::{*, tui::{*, TuiRun}}}};
use std::sync::{Arc, RwLock};
const SOURCE: &'static str = include_str!("edn02.edn"); const EDN: &'static str = include_str!("edn02.edn");
fn main () { fn main () -> Usually<()> {
Tui::run(Arc::new(RwLock::new(Demo::new())))?; let state = Arc::new(RwLock::new(Example));
Tui::new().unwrap().run(&state)?;
Ok(()) Ok(())
} }
pub struct Example;
impl EdnLayout<Tui> for Example {
fn get_content <'a> (&'a self, sym: &'a str) -> Box<EdnRender<'a, Tui>> {
Box::new(Thunk::new(move||if sym == ":hello" { "Hello world!" } else { "" }))
}
}
impl Handle<Tui> for Example {}

View file

@ -14,14 +14,27 @@ impl<T> Default for EdnItem<T> {
} }
impl<T: Debug> Debug for EdnItem<T> { impl<T: Debug> Debug for EdnItem<T> {
fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> { fn fmt (&self, f: &mut Formatter<'_>) -> Result<(), FormatError> {
use EdnItem::*;
match self { match self {
Self::Nil => write!(f, "Nil"), Nil => write!(f, "Nil"),
Self::Num(u) => write!(f, "Num({u})"), Num(u) => write!(f, "Num({u})"),
Self::Sym(u) => write!(f, "Sym({u:?})"), Sym(u) => write!(f, "Sym({u:?})"),
Self::Key(u) => write!(f, "Key({u:?})"), Key(u) => write!(f, "Key({u:?})"),
Self::Exp(e) => write!(f, "Exp({})", itertools::join( Exp(e) => write!(f, "Exp({})",
e.iter().map(|i|format!("{:?}", i)), "," itertools::join(e.iter().map(|i|format!("{:?}", i)), ","))
)) }
}
}
impl<T: PartialEq> PartialEq for EdnItem<T> {
fn eq (&self, other: &Self) -> bool {
use EdnItem::*;
match (self, other) {
(Nil, Nil) => true,
(Num(a), Num(b)) => a == b,
(Sym(a), Sym(b)) => a == b,
(Key(a), Key(b)) => a == b,
(Exp(a), Exp(b)) => a == b,
_ => false
} }
} }
} }

8
edn/src/edn_main.rs Normal file
View file

@ -0,0 +1,8 @@
use crate::*;
#[macro_export] macro_rules! run_tek_edn {
($source:expr) => {
struct EdnRunner;
impl<E: Engine> EdnLayout<E> for EdnRunner;
}
}

View file

@ -16,7 +16,7 @@ pub trait EdnLayout<E: Engine> {
fn get_bool (&self, _sym: &str) -> bool { false } fn get_bool (&self, _sym: &str) -> bool { false }
fn get_unit (&self, _sym: &str) -> E::Unit { 0.into() } fn get_unit (&self, _sym: &str) -> E::Unit { 0.into() }
fn get_usize (&self, _sym: &str) -> usize { 0 } fn get_usize (&self, _sym: &str) -> usize { 0 }
fn get_content <'a> (&'a self, _sym: &str) -> Box<EdnRender<'a, E>> { Box::new(()) } fn get_content <'a> (&'a self, _sym: &'a str) -> Box<EdnRender<'a, E>> { Box::new(()) }
} }
/// Renders from EDN source and context. /// Renders from EDN source and context.

View file

@ -4,40 +4,42 @@
pub(crate) use std::{ pub(crate) use std::{
fmt::{Debug, Formatter, Error as FormatError} fmt::{Debug, Formatter, Error as FormatError}
}; };
pub use ::tek_layout;
pub(crate) use ::tek_layout::{ pub(crate) use ::tek_layout::{
*, *,
tek_engine::{Usually, Content, Render, Engine, Thunk} tek_engine::{Usually, Content, Render, Engine, Thunk}
}; };
mod edn_error; pub use self::edn_error::*; mod edn_error; pub use self::edn_error::*;
mod edn_item; pub use self::edn_item::*; mod edn_item; pub use self::edn_item::*;
mod edn_token; pub use self::edn_token::*; mod edn_token; pub use self::edn_token::*;
mod edn_view; pub use self::edn_view::*; mod edn_view; pub use self::edn_view::*;
mod edn_main; pub use self::edn_main::*;
#[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> { #[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> {
use EdnItem::*; use EdnItem::*;
assert_eq!(EdnItem::read_all("")?, assert_eq!(EdnItem::<String>::read_all("")?,
vec![]); vec![]);
assert_eq!(EdnItem::read_all(" ")?, assert_eq!(EdnItem::<String>::read_all(" ")?,
vec![]); vec![]);
assert_eq!(EdnItem::read_all("1234")?, assert_eq!(EdnItem::<String>::read_all("1234")?,
vec![Num(1234)]); vec![Num(1234)]);
assert_eq!(EdnItem::read_all("1234 5 67")?, assert_eq!(EdnItem::<String>::read_all("1234 5 67")?,
vec![Num(1234), Num(5), Num(67)]); vec![Num(1234), Num(5), Num(67)]);
assert_eq!(EdnItem::read_all("foo/bar")?, assert_eq!(EdnItem::<String>::read_all("foo/bar")?,
vec![Key("foo/bar".into())]); vec![Key("foo/bar".into())]);
assert_eq!(EdnItem::read_all(":symbol")?, assert_eq!(EdnItem::<String>::read_all(":symbol")?,
vec![Sym(":symbol".into())]); vec![Sym(":symbol".into())]);
assert_eq!(EdnItem::read_all(" foo/bar :baz 456")?, assert_eq!(EdnItem::<String>::read_all(" foo/bar :baz 456")?,
vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)]); vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)]);
assert_eq!(EdnItem::read_all(" (foo/bar :baz 456) ")?, assert_eq!(EdnItem::<String>::read_all(" (foo/bar :baz 456) ")?,
vec![Exp(vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)])]); vec![Exp(vec![Key("foo/bar".into()), Sym(":baz".into()), Num(456)])]);
Ok(()) Ok(())
} }
#[cfg(test)] #[test] fn test_edn_layout () -> Result<(), ParseError> { #[cfg(test)] #[test] fn test_edn_layout () -> Result<(), ParseError> {
let source = include_str!("example.edn"); EdnItem::<String>::read_all(include_str!("../examples/edn01.edn"))?;
let layout = EdnItem::read_all(source)?; EdnItem::<String>::read_all(include_str!("../examples/edn02.edn"))?;
//panic!("{layout:?}"); //panic!("{layout:?}");
//let content = <dyn EdnLayout<::tek_engine::tui::Tui>>::from(&layout); //let content = <dyn EdnLayout<::tek_engine::tui::Tui>>::from(&layout);
Ok(()) Ok(())

View file

@ -3,7 +3,9 @@ use std::sync::{Arc, Mutex, RwLock};
/// Handle input /// Handle input
pub trait Handle<E: Engine>: Send + Sync { pub trait Handle<E: Engine>: Send + Sync {
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled>; fn handle (&mut self, _input: &E::Input) -> Perhaps<E::Handled> {
Ok(None)
}
} }
/// Current input state /// Current input state

View file

@ -36,11 +36,11 @@ impl<'a, E: Engine> Content<E> for Box<dyn Render<E> + Send + Sync + 'a> {
impl<E: Engine> Content<E> for &(dyn Render<E> + '_) { impl<E: Engine> Content<E> for &(dyn Render<E> + '_) {
fn content (&self) -> impl Render<E> { self } fn content (&self) -> impl Render<E> { self }
} }
pub struct Thunk<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync>(F, PhantomData<E>); pub struct Thunk<E: Engine, T: Render<E>, F: Fn()->T + Send + Sync>(F, PhantomData<E>);
impl<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync> Thunk<E, T, F> { impl<E: Engine, T: Render<E>, F: Fn()->T + Send + Sync> Thunk<E, T, F> {
pub fn new (thunk: F) -> Self { Self(thunk, Default::default()) } pub fn new (thunk: F) -> Self { Self(thunk, Default::default()) }
} }
impl<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E, T, F> { impl<E: Engine, T: Render<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E, T, F> {
fn content (&self) -> impl Render<E> { (self.0)() } fn content (&self) -> impl Render<E> { (self.0)() }
} }
impl<E: Engine, T: Content<E>> Content<E> for &T { impl<E: Engine, T: Content<E>> Content<E> for &T {