updated structure

This commit is contained in:
2024-10-16 20:43:34 +02:00
parent 590de19df8
commit 12417848f3
4 changed files with 128 additions and 62 deletions
+11 -21
View File
@@ -1,25 +1,15 @@
use crate::{Component, ComponentParams}; use crate::{utils::render_to_frame, Component, Params};
#[derive(Debug)] pub fn div(params: Params) -> Component {
pub struct Div { Component::new(params, |s| {
pub children: Vec<Box<dyn Component>>, let mut frame: Vec<String> = vec![" ".repeat(s.width); s.height];
for c in &mut s.children {
render_to_frame(s.width, &mut frame, c);
}
frame.join("\n")
})
} }
impl Component for Div {} pub fn text(params: Params) -> Component {
Component::new(params, |c| c.expr.clone())
pub fn div(params: ComponentParams) -> Div {
Div {
children: params.children,
}
}
#[derive(Debug)]
pub struct Text {
pub text: String,
}
impl Component for Text {}
pub fn text(params: ComponentParams) -> Text {
Text { text: params.expr }
} }
+80 -7
View File
@@ -1,15 +1,88 @@
use std::fmt::Debug; use std::{thread::sleep, time::Duration};
use crossterm::terminal;
use utils::{clear, hide_cursor, render_to_frame};
pub mod components; pub mod components;
pub mod macros; pub mod macros;
pub mod utils;
pub trait Component: Debug { #[derive(Debug)]
fn render(&mut self) -> String { pub struct Params {
String::new() pub expr: String,
pub children: Vec<Component>,
}
#[derive(Debug, Clone)]
pub struct Component {
pub render: fn(&mut Component) -> String,
pub update: fn(&mut Component),
pub x: usize,
pub y: usize,
pub width: usize,
pub height: usize,
pub expr: String,
pub children: Vec<Component>,
}
impl Component {
fn new(params: Params, render: fn(&mut Component) -> String) -> Component {
Component {
render,
update: |_| {},
x: 0,
y: 0,
width: 0,
height: 0,
expr: params.expr,
children: params.children,
}
} }
} }
pub struct ComponentParams { #[derive(Debug, Clone)]
pub children: Vec<Box<dyn Component>>, pub struct Screen {
pub expr: String, component: Component,
}
impl Screen {
pub fn new() -> Screen {
Screen {
component: Component::new(
Params {
expr: "".to_string(),
children: vec![],
},
|_| String::new(),
),
}
}
pub fn set_component(&mut self, component: Component) {
let (width, height) = terminal::size().unwrap();
self.component = component;
if self.component.width == 0 {
self.component.width = width as usize;
}
if self.component.width == 0 {
self.component.height = height as usize;
}
self.render();
}
pub fn render(&mut self) {
let (width, height) = terminal::size().unwrap();
let mut frame: Vec<String> = vec![" ".repeat(width as usize); height as usize];
render_to_frame(width as usize, &mut frame, &mut self.component);
clear();
print!("{}", frame.join("\n"));
}
pub fn run(&mut self) {
hide_cursor();
// enable_raw_mode().unwrap();
loop {
self.render();
sleep(Duration::from_millis(500));
}
}
} }
+15 -15
View File
@@ -8,7 +8,7 @@ macro_rules! __oml {
}}; }};
($tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*} $($rest:tt)+) => {{ ($tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*} $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+); let mut comps: Vec<osui::Component> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($($k = $v),*) {$($inner)*})); comps.insert(0, oml!($tag ($($k = $v),*) {$($inner)*}));
comps comps
}}; }};
@@ -19,7 +19,7 @@ macro_rules! __oml {
}}; }};
($tag:ident {$($inner:tt)*} $($rest:tt)+) => {{ ($tag:ident {$($inner:tt)*} $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+); let mut comps: Vec<osui::Component> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag {$($inner)*})); comps.insert(0, oml!($tag {$($inner)*}));
comps comps
}}; }};
@@ -30,7 +30,7 @@ macro_rules! __oml {
}}; }};
($tag:ident ($expr:expr; $($k:ident = $v:expr),*) {$($inner:tt)*} $($rest:tt)+) => {{ ($tag:ident ($expr:expr; $($k:ident = $v:expr),*) {$($inner:tt)*} $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+); let mut comps: Vec<osui::Component> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($expr; $($k = $v),*) {$($inner)*})); comps.insert(0, oml!($tag ($expr; $($k = $v),*) {$($inner)*}));
comps comps
}}; }};
@@ -41,7 +41,7 @@ macro_rules! __oml {
}}; }};
($tag:ident ($expr:expr; $($k:ident = $v:expr),*) $($rest:tt)+) => {{ ($tag:ident ($expr:expr; $($k:ident = $v:expr),*) $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+); let mut comps: Vec<osui::Component> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($expr; $($k = $v),*))); comps.insert(0, oml!($tag ($expr; $($k = $v),*)));
comps comps
}}; }};
@@ -52,7 +52,7 @@ macro_rules! __oml {
}}; }};
($tag:ident ($($k:ident = $v:expr),*) $($rest:tt)+) => {{ ($tag:ident ($($k:ident = $v:expr),*) $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+); let mut comps: Vec<osui::Component> = osui::__oml!($($rest)+);
comps.insert(0, oml!($tag ($($k = $v),*))); comps.insert(0, oml!($tag ($($k = $v),*)));
comps comps
}}; }};
@@ -63,7 +63,7 @@ macro_rules! __oml {
}}; }};
($expr:expr; $($rest:tt)+) => {{ ($expr:expr; $($rest:tt)+) => {{
let mut comps: Vec<Box<dyn osui::Component>> = osui::__oml!($($rest)+); let mut comps: Vec<osui::Component> = osui::__oml!($($rest)+);
comps.insert(0, oml!($expr)); comps.insert(0, oml!($expr));
comps comps
}}; }};
@@ -73,43 +73,43 @@ macro_rules! __oml {
macro_rules! oml { macro_rules! oml {
// Props (P) // Props (P)
($tag:ident ($($k:ident = $v:expr),*)) => {{ ($tag:ident ($($k:ident = $v:expr),*)) => {{
let mut c = $tag(osui::ComponentParams { children: Vec::new(), expr: String::new() }); let mut c = $tag(osui::Params { children: Vec::new(), expr: String::new() });
$( $(
c.$k = $v; c.$k = $v;
)* )*
Box::new(c) c
}}; }};
// Expression, With components (EC) // Expression, With components (EC)
( $tag:ident ($expr:expr; $($k:ident = $v:expr),* ) {$($inner:tt)*} ) => {{ ( $tag:ident ($expr:expr; $($k:ident = $v:expr),* ) {$($inner:tt)*} ) => {{
let mut c = $tag(osui::ComponentParams { children: osui::__oml!($($inner)*), expr: String::from($expr) }); let mut c = $tag(osui::Params { children: osui::__oml!($($inner)*), expr: String::from($expr) });
$( $(
c.$k = $v; c.$k = $v;
)* )*
Box::new(c) c
}}; }};
// Expression (E) // Expression (E)
( $tag:ident ($expr:expr; $($k:ident = $v:expr),*) ) => {{ ( $tag:ident ($expr:expr; $($k:ident = $v:expr),*) ) => {{
let mut c = $tag(osui::ComponentParams { children: Vec::new(), expr: String::from($expr) }); let mut c = $tag(osui::Params { children: Vec::new(), expr: String::from($expr) });
$( $(
c.$k = $v; c.$k = $v;
)* )*
Box::new(c) c
}}; }};
// Components (C) // Components (C)
( $tag:ident {$($inner:tt)*} ) => { ( $tag:ident {$($inner:tt)*} ) => {
Box::new($tag(osui::ComponentParams { children: osui::__oml!($($inner)*), expr: String::new() })) $tag(osui::Params { children: osui::__oml!($($inner)*), expr: String::new() })
}; };
// Props, With components (PC) // Props, With components (PC)
( $tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*} ) => {{ ( $tag:ident ($($k:ident = $v:expr),*) {$($inner:tt)*} ) => {{
let mut c = $tag(osui::ComponentParams { children: osui::__oml!($($inner)*), expr: String::new() }); let mut c = $tag(osui::Params { children: osui::__oml!($($inner)*), expr: String::new() });
$( $(
c.$k = $v; c.$k = $v;
)* )*
Box::new(c) c
}}; }};
// Pre-Defined (D) // Pre-Defined (D)
+22 -19
View File
@@ -1,31 +1,34 @@
use osui::{ use osui::{
components::{div, text, Div}, components::{div, text},
oml, ComponentParams, oml, Component, Params, Screen,
}; };
fn main() { fn main() {
let doc = oml!(div {app()}); let mut screen = Screen::new();
screen.set_component(oml!(app()));
println!("{:?}", doc); screen.run();
} }
fn app(_: ComponentParams) -> Div { fn app(_: Params) -> Component {
*oml!( oml!(
// Components (C) // Components (C)
div { div (width = 5, height = 3) {
// Props, With components (PC)
div () {
// Props (P)
text(/* Properties go here, Example: x = 20 y = 5 */)
}
// Expression (E) text("Hello, World! daddy"; width = 30)
text("Hello";)
// Expression, With components (EC) // // Props, With components (PC)
text("World!";) { // div () {
// Components in here // // Props (P)
} // text(/* Properties go here, Example: x = 20 y = 5 */)
// }
// // Expression (E)
// text("Hello";)
// // Expression, With components (EC)
// text("World!";) {
// // Components in here
// }
} }
) )
} }