edn -> dsl

This commit is contained in:
🪞👃🪞 2025-03-14 23:44:13 +02:00
parent d30eda33d1
commit 877b344765
35 changed files with 197 additions and 225 deletions

View file

@ -1,4 +1,3 @@
//#![feature(lazy_type_alias)]
#![feature(step_trait)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
@ -18,7 +17,9 @@ mod view; pub use self::view::*;
pub(crate) use std::marker::PhantomData;
pub(crate) use std::error::Error;
pub(crate) use ::tek_edn::*;
#[cfg(feature = "dsl")] pub(crate) use ::tengri_dsl::*;
/// Standard result type.
pub type Usually<T> = Result<T, Box<dyn Error>>;
/// Standard optional result type.

View file

@ -1,10 +1,10 @@
use crate::*;
use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
//use ratatui::prelude::{Style, Color};
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
pub trait HasSize<E: Output> {
fn size (&self) -> &Measure<E>;
}
#[macro_export] macro_rules! has_size {
(<$E:ty>|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => {
impl $(<$($L),*$($T $(: $U)?),*>)? HasSize<$E> for $Struct $(<$($L),*$($T),*>)? {
@ -12,6 +12,7 @@ pub trait HasSize<E: Output> {
}
}
}
/// A widget that tracks its render width and height
#[derive(Default)]
pub struct Measure<E: Output> {
@ -19,12 +20,15 @@ pub struct Measure<E: Output> {
pub x: Arc<AtomicUsize>,
pub y: Arc<AtomicUsize>,
}
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
impl<E: Output> Content<E> for Measure<E> {
fn render (&self, to: &mut E) {
self.x.store(to.area().w().into(), Relaxed);
self.y.store(to.area().h().into(), Relaxed);
}
}
impl<E: Output> Clone for Measure<E> {
fn clone (&self) -> Self {
Self {
@ -34,6 +38,7 @@ impl<E: Output> Clone for Measure<E> {
}
}
}
impl<E: Output> std::fmt::Debug for Measure<E> {
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
f.debug_struct("Measure")
@ -42,6 +47,7 @@ impl<E: Output> std::fmt::Debug for Measure<E> {
.finish()
}
}
impl<E: Output> Measure<E> {
pub fn new () -> Self {
Self {
@ -79,66 +85,3 @@ impl<E: Output> Measure<E> {
Bsp::b(Fill::xy(self), item)
}
}
//#[cfg(test)] #[test] fn test_measure () {
//use tek_tui::*;
//let size: Measure<TuiOut> = Measure::default().set_w(1usize).set_h(1usize).clone();
//let size: Measure<TuiOut> = (&Measure::new().set_wh(2usize, 1usize)).clone();
//let _ = format!("{:?}", &size);
//let _ = size.wh();
//let _ = size.format();
//let _ = size.of(());
//}
///// A scrollable area.
//pub struct Scroll<E, F>(pub F, pub Direction, pub u64, PhantomData<E>)
//where
//E: Output,
//F: Send + Sync + Fn(&mut dyn FnMut(&dyn Content<E>)->Usually<()>)->Usually<()>;
//pub trait ContentDebug<E: Output> {
//fn debug <W: Content<E>> (other: W) -> DebugOverlay<E, W> {
//DebugOverlay(Default::default(), other)
//}
//}
//impl<E: Output> ContentDebug<E> for E {}
//impl Render<TuiOut> for Measure<TuiOut> {
//fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> {
//Ok(Some([0u16.into(), 0u16.into()].into()))
//}
//fn render (&self, to: &mut TuiOut) -> Usually<()> {
//self.set_w(to.area().w());
//self.set_h(to.area().h());
//Ok(())
//}
//}
//impl Measure<TuiOut> {
//pub fn debug (&self) -> ShowMeasure {
//ShowMeasure(&self)
//}
//}
//render!(Tui: |self: ShowMeasure<'a>|render(|to: &mut TuiOut|Ok({
//let w = self.0.w();
//let h = self.0.h();
//to.blit(&format!(" {w} x {h} "), to.area.x(), to.area.y(), Some(
//Style::default().bold().italic().bg(Color::Rgb(255, 0, 255)).fg(Color::Rgb(0,0,0))
//))
//})));
//pub struct ShowMeasure<'a>(&'a Measure<TuiOut>);
//pub struct DebugOverlay<E: Output, W: Render<E>>(PhantomData<E>, pub W);
//impl<T: Render<TuiOut>> Render<TuiOut> for DebugOverlay<Tui, T> {
//fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
//self.1.min_size(to)
//}
//fn render (&self, to: &mut TuiOut) -> Usually<()> {
//let [x, y, w, h] = to.area();
//self.1.render(to)?;
//Ok(to.blit(&format!("{w}x{h}+{x}+{y}"), x, y, Some(Style::default().green())))
//}
//}

View file

@ -1,6 +1,6 @@
//! Aligns things to the container. Comes with caveats.
//! ```
//! use ::tek_tui::{*, tek_output::*};
//! use ::tengri::{output::*, tui::*};
//! let area: [u16;4] = [10, 10, 20, 20];
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
//! assert_eq!(Content::layout(item, area), expected);
@ -28,9 +28,12 @@
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
//! ```
use crate::*;
#[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A);
try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter|
#[cfg(feature = "dsl")]
try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter|{
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
match key {
"align/c"|"align/x"|"align/y"|
@ -56,7 +59,9 @@ try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter|
},
_ => return None
}
});
}
});
impl<A> Align<A> {
#[inline] pub fn c (a: A) -> Self { Self(Alignment::Center, a) }
#[inline] pub fn x (a: A) -> Self { Self(Alignment::X, a) }

View file

@ -16,6 +16,7 @@ impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
}
}
}
#[cfg(feature = "dsl")]
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
match key {

View file

@ -1,10 +1,14 @@
use crate::*;
/// Show an item only when a condition is true.
pub struct When<A>(pub bool, pub A);
impl<A> When<A> { #[inline] pub fn new (c: bool, a: A) -> Self { Self(c, a) } }
/// Show one item if a condition is true and another if the condition is false
pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> { #[inline] pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
#[cfg(feature = "dsl")]
try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
if let Some(Token { value: Value::Key("when"), .. }) = iter.peek() {
let _ = iter.next().unwrap();
@ -15,6 +19,8 @@ try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
return Some(Self(condition, content))
}
});
#[cfg(feature = "dsl")]
try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
if let Some(Token { value: Value::Key("either"), .. }) = iter.peek() {
let _ = iter.next().unwrap();
@ -27,6 +33,7 @@ try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter
return Some(Self(condition, content, alternate))
}
});
impl<E: Output, A: Render<E>> Content<E> for When<A> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, item) = self;
@ -45,6 +52,7 @@ impl<E: Output, A: Render<E>> Content<E> for When<A> {
if *cond { item.render(to) }
}
}
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, a, b) = self;

View file

@ -3,7 +3,7 @@
//!
//! Transform may also react to the [Area] provided.
//! ```
//! use ::tek_tui::{*, tek_output::*};
//! use ::tengri::{output::*, tui::*};
//! let area: [u16;4] = [10, 10, 20, 20];
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
//! assert_eq!(Content::layout(item, area), expected);
@ -30,6 +30,7 @@ macro_rules! transform_xy {
#[inline] pub fn y (item: T) -> Self { Self::Y(item) }
#[inline] pub fn xy (item: T) -> Self { Self::XY(item) }
}
#[cfg(feature = "dsl")]
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
for $Enum<RenderBox<'a, E>> {
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {
@ -76,6 +77,7 @@ macro_rules! transform_xy_unit {
#[inline] pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
#[inline] pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
}
#[cfg(feature = "dsl")]
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
for $Enum<E::Unit, RenderBox<'a, E>> {
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {

View file

@ -20,6 +20,7 @@ pub trait Output: Send + Sync + Sized {
#[inline] fn h (&self) -> Self::Unit { self.area().h() }
#[inline] fn wh (&self) -> Self::Size { self.area().wh().into() }
}
/// Renderable with dynamic dispatch.
pub trait Render<E: Output> {
/// Compute layout.
@ -31,6 +32,7 @@ pub trait Render<E: Output> {
Box::new(self) as RenderBox<'a, E>
}
}
/// Most importantly, every [Content] is also a [Render].
///
/// However, the converse does not hold true.
@ -40,17 +42,21 @@ impl<E: Output, C: Content<E>> Render<E> for C {
fn layout (&self, area: E::Area) -> E::Area { Content::layout(self, area) }
fn render (&self, output: &mut E) { Content::render(self, output) }
}
/// Opaque pointer to a renderable living on the heap.
///
/// Return this from [Content::content] to use dynamic dispatch.
pub type RenderBox<'a, E> = Box<RenderDyn<'a, E>>;
/// You can render from a box.
impl<'a, E: Output> Content<E> for RenderBox<'a, E> {
fn content (&self) -> impl Render<E> { self.deref() }
//fn boxed <'b> (self) -> RenderBox<'b, E> where Self: Sized + 'b { self }
}
/// Opaque pointer to a renderable.
pub type RenderDyn<'a, E> = dyn Render<E> + Send + Sync + 'a;
/// You can render from an opaque pointer.
impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
fn content (&self) -> impl Render<E> {
@ -66,6 +72,7 @@ impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
Render::render(self.deref(), output)
}
}
/// Composable renderable with static dispatch.
pub trait Content<E: Output> {
/// Return a [Render]able of a specific type.
@ -75,17 +82,20 @@ pub trait Content<E: Output> {
/// Draw to output. By default, delegates to [Self::content].
fn render (&self, output: &mut E) { self.content().render(output) }
}
/// Every pointer to [Content] is a [Content].
impl<E: Output, C: Content<E>> Content<E> for &C {
fn content (&self) -> impl Render<E> { (*self).content() }
fn layout (&self, area: E::Area) -> E::Area { (*self).layout(area) }
fn render (&self, output: &mut E) { (*self).render(output) }
}
/// The platonic ideal unit of [Content]: total emptiness at dead center (e=1vg^sqrt(-1))
impl<E: Output> Content<E> for () {
fn layout (&self, area: E::Area) -> E::Area { area.center().to_area_pos().into() }
fn render (&self, _: &mut E) {}
}
impl<E: Output, T: Content<E>> Content<E> for Option<T> {
fn content (&self) -> impl Render<E> {
self.as_ref()
@ -100,6 +110,7 @@ impl<E: Output, T: Content<E>> Content<E> for Option<T> {
.map(|content|content.render(output));
}
}
/// Implement [Content] with composable content for a struct.
#[macro_export] macro_rules! content {
// Implement for all [Output]s.
@ -119,6 +130,7 @@ impl<E: Output, T: Content<E>> Content<E> for Option<T> {
}
};
}
/// Implement [Content] with custom rendering for a struct.
#[macro_export] macro_rules! render {
(|$self:ident:$Struct:ident $(<

View file

@ -1,4 +1,5 @@
use crate::*;
#[macro_export] macro_rules! view {
($Output:ty: |$self:ident: $State:ty| $expr:expr; {
$($sym:literal => $body:expr),* $(,)?
@ -23,7 +24,10 @@ use crate::*;
// An ephemeral wrapper around view state and view description,
// that is meant to be constructed and returned from [Content::content].
#[cfg(feature = "dsl")]
pub struct View<'a, T>(pub &'a T, pub SourceIter<'a>);
#[cfg(feature = "dsl")]
impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
fn content (&self) -> impl Render<O> {
let iter = self.1.clone();
@ -35,7 +39,9 @@ impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
return None
}
}
// Provides components to the view.
#[cfg(feature = "dsl")]
pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
+ Context<bool>
+ Context<usize>
@ -67,6 +73,8 @@ pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
None
}
}
#[cfg(feature = "dsl")]
#[macro_export] macro_rules! try_delegate {
($s:ident, $atom:expr, $T:ty) => {
if let Some(value) = <$T>::try_from_atom($s, $atom) {
@ -74,6 +82,8 @@ pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
}
}
}
#[cfg(feature = "dsl")]
#[macro_export] macro_rules! try_from_expr {
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $iter:ident|$body:expr) => {
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct {