From 59aa8988f0bcda8310e81cae31b762626f24d4ae Mon Sep 17 00:00:00 2001 From: TerraMaster85 Date: Thu, 16 Jul 2026 21:36:50 -0400 Subject: [PATCH] feat: macrofication --- .gitignore | 2 ++ examples/empty_container.rs | 10 ++---- src/elements/.container.rs.swp | Bin 12288 -> 0 bytes src/elements/.element.rs.swp | Bin 12288 -> 0 bytes src/elements/container.rs | 61 +++++---------------------------- src/elements/macros.rs | 59 +++++++++++++++++++++++++++++++ src/elements/mod.rs | 3 ++ 7 files changed, 75 insertions(+), 60 deletions(-) delete mode 100644 src/elements/.container.rs.swp delete mode 100644 src/elements/.element.rs.swp create mode 100644 src/elements/macros.rs diff --git a/.gitignore b/.gitignore index ea8c4bf7f35f6f77f75d92ad8ce8349f6e81ddba..3fa5a865055e68b453c5792e93ec736f19143dce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +*.swp +*~ diff --git a/examples/empty_container.rs b/examples/empty_container.rs index f7f12062c3405cb74c6a547a928a99b849f33ec8..2e9b42ff48fbc74961defd899d385bb9b9d9dfe5 100644 --- a/examples/empty_container.rs +++ b/examples/empty_container.rs @@ -2,18 +2,12 @@ extern crate lintuini; use lintuini::elements::Element; use lintuini::elements::Container; -use ratatui::style::{Color, Style}; -use ratatui::widgets::BorderType; - use crossterm::event; fn main() -> std::io::Result<()> { - let fill = Style::default().bg(Color::Blue); - let bord_style = Style::default().bg(Color::Green).fg(Color::Black); - let bord_type = BorderType::Double; - let cont = lintuini::elements::Container::new(); + let cont = Container::new(); - ratatui::run(|mut terminal| { + ratatui::run(|terminal| { loop { terminal.draw(|mut frame| { let area = frame.area(); diff --git a/src/elements/.container.rs.swp b/src/elements/.container.rs.swp deleted file mode 100644 index d732bb03a6ed4d59dc5d92664a497581b191e665..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2UuYaf9LFcET4RkZ76cVxY9!sO+e_LY)VUIF#AJhk}PlAHq`Llm=fAYtIP@IKN_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86-3PBe|#PDh?w<~l`$SGro%SyB$sO8O?BrViJi8>uvX zsz)9>-PJ+icB=}x!MhBc6Jy|C57wHPMED}E)e;1aXwH%|E+U3?0axYNd=d&ct zQa#H(ZKK^_^dv90PuV8o&Cw(gDDrbrqQr)9BOFeX<}NtQ&7fMYS~qw9<<=>)Tr zOv@{>QX;YudU0^D!1yaN;h)UvQ?Us_Syjw;QJRmv%R{=xujdaC;y`*WWuleG<<8 zekO!VR^AlD)0)%Ps*d|?r%XI8Htdb;VK0u1%Oex@G8^=CtP^cq)_m#OY|2J>4FL$; zOdxl|&dQv4z5HsaSoreAb9($}=Vp6gHUuC50SG_<0uX=z1R!v$1>9&xeB}ye>ni5! z=gh>j?&1Lf2tWV=5P$##AOHafKmY;|fWR#jP@xdt=Y*KQ&&l)u|IP3JU;O1K#}AHk zjx&yL9G^Juaopv&!*RjujyX0t9&!}>Jmo-x00bZa0SG_<0uX=z1Rwx`|5@PVL=d;3 zq2M5jL#xdo$z5xKKPc1jaFIGbmAwz|(l7n+$k1AKd)xo$O)hOMTrRn0x(dTVdD`VM z?do_*Q8J2Y!??6Rd7NLSvaPc`cKz03oA?fTYNgz)Zlmon zZgu?fFR2$<=9#9zDyO9^EB2}(9#MRweh{RV-|&U}Br+ { @@ -16,24 +15,14 @@ pub struct Container<'a> { widgets: ContainerWidgets<'a>, } -impl ElementWithChildren for Container<'_> { - #[inline(always)] - fn add_child(&mut self, child: impl Into>) -> Result<(), error::LintuiniError>{ - self.children.push(child.into()); - - - Ok(()) - } - #[inline(always)] - fn children(&self) -> &[Box] { - self.children.as_slice() - } - #[inline(always)] - fn children_mut(&mut self) -> &mut [Box] { - self.children.as_mut_slice() - } +#[derive(Clone, Default)] +struct ContainerWidgets<'a> { + block0: block::Block<'a>, } +basic_config_methods!(Container<'_>); +element_with_children!(Container<'_>); + impl Element for Container<'_> { #[inline(always)] fn constraint(&self) -> layout::Constraint { @@ -44,42 +33,10 @@ impl Element for Container<'_> { frm.render_widget(&self.widgets.block0, rect); for (el, r) in self.children.iter().zip(&*self.layout.split(rect)) { - el.render(frm, *r); + el.render(frm, *r)?; } Ok(()) } } -impl Container<'_> { - #[inline(always)] - pub fn new() -> Self { - Container::default() - } - - #[inline(always)] - pub fn constraint(mut self, constraint: layout::Constraint) -> Self { - self.constraint = constraint; - self - } - - #[inline(always)] - pub fn direction(mut self, direction: layout::Direction) -> Self { - self.layout = self.layout.direction(direction); - self.regenerate_layout() - } - - fn regenerate_layout(mut self) -> Self { - if self.children.len() == 0 { return self; } - self.layout = self.layout.constraints( - self.children - .iter() - .map(|el| el.constraint()) - ); - self - } -} -#[derive(Clone, Default)] -struct ContainerWidgets<'a> { - block0: block::Block<'a>, -} diff --git a/src/elements/macros.rs b/src/elements/macros.rs new file mode 100644 index 0000000000000000000000000000000000000000..42cea00df8e26f59aa5b492bf886fa84db51efe0 --- /dev/null +++ b/src/elements/macros.rs @@ -0,0 +1,59 @@ +macro_rules! element_with_children { + ($el:ty) => { + impl ElementWithChildren for $el { + #[inline(always)] + fn add_child(&mut self, child: impl Into>) -> Result<(), crate::error::LintuiniError>{ + self.children.push(child.into()); + self.regenerate_layout(); + Ok(()) + } + + #[inline(always)] + fn children(&self) -> &[Box] { + self.children.as_slice() + } + + #[inline(always)] + fn children_mut(&mut self) -> &mut [Box] { + self.children.as_mut_slice() + } + } + } +} +pub(crate) use element_with_children; + +macro_rules! basic_config_methods { + ($el:ty) => { + impl $el { + #[inline(always)] + pub fn new() -> Self { + <$el>::default() + } + + #[inline(always)] + pub fn constraint(&mut self, constraint: ratatui_core::layout::Constraint) -> &mut Self { + self.constraint = constraint; + self + } + + // beware the owned self! + #[inline(always)] + pub fn direction(mut self, direction: ratatui_core::layout::Direction) -> Self { + self.layout = self.layout.direction(direction); + self.regenerate_layout(); + self + } + + pub fn regenerate_layout(&mut self) -> &mut Self { + if self.children.len() == 0 { return self; } + self.layout = self.layout.clone().constraints( + self.children + .iter() + .map(|el| el.constraint()) + ); + self + } + } + } +} +pub(crate) use basic_config_methods; diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 0f7316f28f7a5afc89d995948cb8113b64ce4c71..0a6e194da33cdb3da3bb1d086a9db05029d7fd89 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -3,3 +3,6 @@ pub use container::*; pub mod element; pub use element::*; + +pub mod macros; +