EdnViewData has to go?

This commit is contained in:
🪞👃🪞 2025-01-14 00:24:48 +01:00
parent ddcb967a2c
commit 585bba6666
6 changed files with 25 additions and 25 deletions

View file

@ -1,29 +1,21 @@
use crate::*; use crate::*;
/// Implement `EdnProvide` for a type and context /// Implement `EdnProvide` for a type and context
#[macro_export] macro_rules! edn_provide { #[macro_export] macro_rules! edn_provide {
($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { ($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$lt> EdnProvide<$lt, $type> for $State { impl<$lt> EdnProvide<$lt, $type> for $State {
fn get <S: AsRef<str>> (&$lt $self, edn: &$lt EdnItem<S>) -> Option<$type> { fn get <S: AsRef<str>> (&$lt $self, edn: &$lt EdnItem<S>) -> Option<$type> {
Some(match edn.to_ref() { Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr),*, _ => return None })
$(EdnItem::Sym($pat) => $expr),*,
_ => return None
})
} }
} }
}; };
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { ($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> EdnProvide<'a, $type> for $State { impl<'a> EdnProvide<'a, $type> for $State {
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> { fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<$type> {
Some(match edn.to_ref() { Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr),*, _ => return None })
$(EdnItem::Sym($pat) => $expr),*,
_ => return None
})
} }
} }
}; };
} }
/// Map EDN tokens to parameters of a given type for a given context /// Map EDN tokens to parameters of a given type for a given context
pub trait EdnProvide<'a, U>: Sized { pub trait EdnProvide<'a, U>: Sized {
fn get <S: AsRef<str>> (&'a self, _edn: &'a EdnItem<S>) -> Option<U> { fn get <S: AsRef<str>> (&'a self, _edn: &'a EdnItem<S>) -> Option<U> {
@ -33,7 +25,6 @@ pub trait EdnProvide<'a, U>: Sized {
self.get(edn).expect("no value") self.get(edn).expect("no value")
} }
} }
impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for &T { impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for &T {
fn get <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> Option<U> { fn get <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> Option<U> {
(*self).get(edn) (*self).get(edn)
@ -42,3 +33,11 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for &T {
(*self).get_or_fail(edn) (*self).get_or_fail(edn)
} }
} }
impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option<T> {
fn get <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> Option<U> {
self.as_ref().map(|s|s.get(edn)).flatten()
}
fn get_or_fail <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> U {
self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider")
}
}

View file

@ -1,6 +1,5 @@
use crate::*; use crate::*;
use EdnItem::*; /** Implement `EdnCommand` for given `State` and `Command` */
#[macro_export] macro_rules! edn_command { #[macro_export] macro_rules! edn_command {
($Command:ty : |$state:ident:$State:ty| { $(( ($Command:ty : |$state:ident:$State:ty| { $((
// identifier // identifier
@ -48,7 +47,6 @@ use EdnItem::*;
let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg); let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg);
}; };
} }
/// Turns an EDN symbol sequence into a command enum variant. /// Turns an EDN symbol sequence into a command enum variant.
pub trait EdnCommand<C>: Command<C> { pub trait EdnCommand<C>: Command<C> {
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self; fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self;

View file

@ -1,4 +1,3 @@
use crate::*;
/// Event source /// Event source
pub trait Input: Send + Sync + Sized { pub trait Input: Send + Sync + Sized {
/// Type of input event /// Type of input event

View file

@ -1,6 +1,19 @@
use crate::*; use crate::*;
use std::marker::PhantomData; use std::marker::PhantomData;
use EdnItem::*; use EdnItem::*;
/// Implements `EdnProvide` for content components and expressions
#[macro_export] macro_rules! edn_provide_content {
(|$self:ident:$Stat:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> EdnProvide<'a, Box<dyn Render<TuiOut> + 'a>> for $State {
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<TuiOut> + 'a>> {
Some(match edn.to_ref() {
$(EdnItem::Sym($pat) => $expr),*,
_ => return None
})
}
}
}
}
/// Renders from EDN source and context. /// Renders from EDN source and context.
#[derive(Default)] #[derive(Default)]
pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> { pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> {

View file

@ -1,13 +1,10 @@
use crate::*; use crate::*;
use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}}; use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
//use ratatui::prelude::{Style, Color}; //use ratatui::prelude::{Style, Color};
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small // TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
pub trait HasSize<E: Output> { pub trait HasSize<E: Output> {
fn size (&self) -> &Measure<E>; fn size (&self) -> &Measure<E>;
} }
#[macro_export] macro_rules! has_size { #[macro_export] macro_rules! has_size {
(<$E:ty>|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => { (<$E:ty>|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => {
impl $(<$($L),*$($T $(: $U)?),*>)? HasSize<$E> for $Struct $(<$($L),*$($T),*>)? { impl $(<$($L),*$($T $(: $U)?),*>)? HasSize<$E> for $Struct $(<$($L),*$($T),*>)? {
@ -15,7 +12,6 @@ pub trait HasSize<E: Output> {
} }
} }
} }
/// A widget that tracks its render width and height /// A widget that tracks its render width and height
#[derive(Default)] #[derive(Default)]
pub struct Measure<E: Output> { pub struct Measure<E: Output> {
@ -23,14 +19,12 @@ pub struct Measure<E: Output> {
pub x: Arc<AtomicUsize>, pub x: Arc<AtomicUsize>,
pub y: Arc<AtomicUsize>, pub y: Arc<AtomicUsize>,
} }
impl<E: Output> Content<E> for Measure<E> { impl<E: Output> Content<E> for Measure<E> {
fn render (&self, to: &mut E) { fn render (&self, to: &mut E) {
self.x.store(to.area().w().into(), Relaxed); self.x.store(to.area().w().into(), Relaxed);
self.y.store(to.area().h().into(), Relaxed); self.y.store(to.area().h().into(), Relaxed);
} }
} }
impl<E: Output> Clone for Measure<E> { impl<E: Output> Clone for Measure<E> {
fn clone (&self) -> Self { fn clone (&self) -> Self {
Self { Self {
@ -40,7 +34,6 @@ impl<E: Output> Clone for Measure<E> {
} }
} }
} }
impl<E: Output> std::fmt::Debug 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> { fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
f.debug_struct("Measure") f.debug_struct("Measure")
@ -49,7 +42,6 @@ impl<E: Output> std::fmt::Debug for Measure<E> {
.finish() .finish()
} }
} }
impl<E: Output> Measure<E> { impl<E: Output> Measure<E> {
pub fn w (&self) -> usize { self.x.load(Relaxed) } pub fn w (&self) -> usize { self.x.load(Relaxed) }
pub fn h (&self) -> usize { self.y.load(Relaxed) } pub fn h (&self) -> usize { self.y.load(Relaxed) }
@ -69,7 +61,6 @@ impl<E: Output> Measure<E> {
Bsp::b(Fill::xy(self), item) Bsp::b(Fill::xy(self), item)
} }
} }
///// A scrollable area. ///// A scrollable area.
//pub struct Scroll<E, F>(pub F, pub Direction, pub u64, PhantomData<E>) //pub struct Scroll<E, F>(pub F, pub Direction, pub u64, PhantomData<E>)
//where //where

View file

@ -1 +1 @@
(fill/x :toolbar) (fill/x (fixed/y 2 :toolbar))