Compare commits

...

2 commits

Author SHA1 Message Date
a55e84c29f output, tui: Stack implementation
Some checks are pending
/ build (push) Waiting to run
2025-05-17 10:15:07 +03:00
b25977d878 add has!, MaybeHas, maybe_has! 2025-05-17 05:47:51 +03:00
5 changed files with 109 additions and 3 deletions

View file

@ -16,6 +16,29 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
} }
pub trait Has<T>: Send + Sync { pub trait Has<T>: Send + Sync {
fn get (&self) -> &T; fn get (&self) -> &T;
fn get_mut (&mut self) -> &mut T; fn get_mut (&mut self) -> &mut T;
} }
#[macro_export] macro_rules! has {
($T:ty: |$self:ident : $S:ty| $x:expr) => {
impl Has<$T> for $S {
fn get (&$self) -> &$T { &$x }
fn get_mut (&mut $self) -> &mut $T { &mut $x }
}
};
}
pub trait MaybeHas<T>: Send + Sync {
fn get (&self) -> Option<&T>;
fn get_mut (&mut self) -> Option<&mut T>;
}
#[macro_export] macro_rules! maybe_has {
($T:ty: |$self:ident : $S:ty| $x:block; $y:block $(;)?) => {
impl MaybeHas<$T> for $S {
fn get (&$self) -> Option<&$T> $x
fn get_mut (&mut $self) -> Option<&mut $T> $y
}
};
}

View file

@ -2,6 +2,7 @@ mod align; pub use self::align::*;
mod bsp; pub use self::bsp::*; mod bsp; pub use self::bsp::*;
mod cond; pub use self::cond::*; mod cond; pub use self::cond::*;
mod map; pub use self::map::*; mod map; pub use self::map::*;
mod stack; pub use self::stack::*;
//mod reduce; pub use self::reduce::*; //mod reduce; pub use self::reduce::*;
mod thunk; pub use self::thunk::*; mod thunk; pub use self::thunk::*;
mod transform; pub use self::transform::*; mod transform; pub use self::transform::*;

View file

@ -1,3 +1,85 @@
use crate::*;
pub struct Stack<E, F> {
__: PhantomData<E>,
direction: Direction,
callback: F
}
impl<E, F> Stack<E, F> {
pub fn new (direction: Direction, callback: F) -> Self {
Self { direction, callback, __: Default::default(), }
}
pub fn north (callback: F) -> Self {
Self::new(Direction::North, callback)
}
pub fn south (callback: F) -> Self {
Self::new(Direction::South, callback)
}
pub fn east (callback: F) -> Self {
Self::new(Direction::East, callback)
}
pub fn west (callback: F) -> Self {
Self::new(Direction::West, callback)
}
}
impl<E: Output, F: Fn(&mut dyn FnMut(&dyn Render<E>)->())->()> Content<E> for Stack<E, F> {
fn layout (&self, mut to: E::Area) -> E::Area {
let mut x = to.x();
let mut y = to.y();
let mut w = to.w();
let mut h = to.h();
(self.callback)(&mut move |component: &dyn Render<E>|{
let layout = component.layout([x, y, w, h].into());
match self.direction {
Direction::North => {
todo!()
},
Direction::South => {
y = y + layout.h();
h = h.minus(layout.h());
},
Direction::East => {
x = x + layout.w();
w = w.minus(layout.w());
},
Direction::West => {
todo!()
},
_ => unreachable!(),
}
});
to
}
fn render (&self, to: &mut E) {
let mut x = to.x();
let mut y = to.y();
let mut w = to.w();
let mut h = to.h();
(self.callback)(&mut move |component: &dyn Render<E>|{
let layout = component.layout([x, y, w, h].into());
match self.direction {
Direction::North => {
todo!()
},
Direction::South => {
y = y + layout.h();
h = h.minus(layout.h());
to.place(layout, component);
},
Direction::East => {
x = x + layout.w();
w = w.minus(layout.w());
to.place(layout, component);
},
Direction::West => {
todo!()
},
_ => unreachable!()
}
});
}
}
/*Stack::down(|add|{ /*Stack::down(|add|{
let mut i = 0; let mut i = 0;
for (_, name) in self.dirs.iter() { for (_, name) in self.dirs.iter() {

View file

@ -14,7 +14,7 @@ pub trait Output: Send + Sync + Sized {
/// Mutable pointer to area /// Mutable pointer to area
fn area_mut (&mut self) -> &mut Self::Area; fn area_mut (&mut self) -> &mut Self::Area;
/// Render widget in area /// Render widget in area
fn place (&mut self, area: Self::Area, content: &impl Render<Self>); fn place <T: Render<Self> + ?Sized> (&mut self, area: Self::Area, content: &T);
#[inline] fn x (&self) -> Self::Unit { self.area().x() } #[inline] fn x (&self) -> Self::Unit { self.area().x() }
#[inline] fn y (&self) -> Self::Unit { self.area().y() } #[inline] fn y (&self) -> Self::Unit { self.area().y() }
#[inline] fn w (&self) -> Self::Unit { self.area().w() } #[inline] fn w (&self) -> Self::Unit { self.area().w() }

View file

@ -12,7 +12,7 @@ impl Output for TuiOut {
type Area = [Self::Unit;4]; type Area = [Self::Unit;4];
#[inline] fn area (&self) -> [u16;4] { self.area } #[inline] fn area (&self) -> [u16;4] { self.area }
#[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area } #[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area }
#[inline] fn place (&mut self, area: [u16;4], content: &impl Render<TuiOut>) { #[inline] fn place <T: Render<Self> + ?Sized> (&mut self, area: [u16;4], content: &T) {
let last = self.area(); let last = self.area();
*self.area_mut() = area; *self.area_mut() = area;
content.render(self); content.render(self);