From 9e013f737fad394cb7363eba26262c6741e5919f Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 15 Oct 2024 13:20:04 +0200 Subject: [PATCH] working structure --- src/components.rs | 16 ++++++++++++ src/key.rs | 8 ------ src/lib.rs | 25 ++++++------------ src/macros.rs | 31 ++++++++++++++++++++++ src/main.rs | 11 ++++---- src/ui.rs | 65 ----------------------------------------------- 6 files changed, 60 insertions(+), 96 deletions(-) create mode 100644 src/components.rs delete mode 100644 src/key.rs create mode 100644 src/macros.rs delete mode 100644 src/ui.rs diff --git a/src/components.rs b/src/components.rs new file mode 100644 index 0000000..58cfa7e --- /dev/null +++ b/src/components.rs @@ -0,0 +1,16 @@ +use crate::{Component, ComponentParams}; + +#[derive(Debug)] +pub struct Div { + pub test: i32, + pub children: Vec>, +} + +impl Component for Div {} + +pub fn div(params: ComponentParams) -> Div { + Div { + test: 0, + children: params.children, + } +} diff --git a/src/key.rs b/src/key.rs deleted file mode 100644 index a33e9ea..0000000 --- a/src/key.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub const ENTER: &str = "\r"; -pub const TAB: &str = "\t"; -pub const BACKSPACE: &str = "\u{7f}"; -pub const ESC: &str = "\u{1b}"; -pub const UP: &str = "\u{1b}[A"; -pub const DOWN: &str = "\u{1b}[B"; -pub const RIGHT: &str = "\u{1b}[C"; -pub const LEFT: &str = "\u{1b}[D"; diff --git a/src/lib.rs b/src/lib.rs index 333773e..f0ddc8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,19 +1,10 @@ -#[macro_export] -macro_rules! __xml { - ($(<$tag:ident> {$($inner:tt)*})+) => {{ - let mut comps = String::new(); - $( - let inner = osui::__xml!($($inner)*); - comps.push_str(&format!("{} {{{}}}", stringify!($tag), inner)); - )* - comps - }}; - () => { String::new() }; -} +use std::fmt::Debug; -#[macro_export] -macro_rules! xml { - (<$tag:ident> {$($inner:tt)*}) => { - osui::__xml!(<$tag> {$($inner)*}) - }; +pub mod components; +pub mod macros; + +pub trait Component: Debug {} + +pub struct ComponentParams { + pub children: Vec>, } diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..e263e7c --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,31 @@ +#[macro_export] +macro_rules! __oml { + ($( ($tag:ident $($k:ident = $v:expr)*) {$($inner:tt)*} )+) => {{ + let mut comps: Vec> = Vec::new(); + $( + // comps.push(Box::new($tag(ComponentParams { children: osui::__oml!($($inner)*) }))); + comps.push(oml!(($tag $($k = $v)*) {$($inner)*})); + )* + comps + }}; + () => { Vec::new() }; +} + +#[macro_export] +macro_rules! oml { + ( ($tag:ident) {} ) => { + Box::new($tag(ComponentParams { children: Vec::new() })) + }; + + ( ($tag:ident) {$($inner:tt)*} ) => { + Box::new($tag(ComponentParams { children: osui::__oml!($($inner)*) })) + }; + + ( ($tag:ident $($k:ident = $v:expr)+ ) {$($inner:tt)*} ) => {{ + let mut c = $tag(ComponentParams { children: osui::__oml!($($inner)*) }); + $( + c.$k = $v; + )+ + Box::new(c) + }}; +} diff --git a/src/main.rs b/src/main.rs index 504e29c..0192c4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ -use osui::xml; +use osui::{components::div, oml, Component, ComponentParams}; fn main() { - let doc = xml!( -
{ -
{ - } + let doc = oml!( + (div) { + (div) {} } ); - println!("{doc}"); + println!("{:?}", doc); } diff --git a/src/ui.rs b/src/ui.rs deleted file mode 100644 index a9cc5fd..0000000 --- a/src/ui.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::{flush, key, read_key, Component, ComponentWrapper}; -impl Component for String { - fn render(&self) -> String { - self.clone() - } -} - -pub fn text(text: &str) -> ComponentWrapper { - ComponentWrapper::new(text.to_string()) -} - -#[derive(Clone)] -pub struct Input { - pub max_length: usize, - pub key: String, - pub data: String, -} - -impl Component for Input { - fn render(&self) -> String { - format!( - " {}\n|{}|\n {}", - "_".repeat(self.max_length), - " ".repeat(self.max_length), - "‾".repeat(self.max_length), - ) - } - fn run(&mut self, x: usize, y: usize, update_fn: fn(&mut Self) -> bool) -> String { - print!("\x1B[{};{}H", y + 2, x + 2); - flush(); - loop { - self.key = read_key(); - if update_fn(self) { - break; - } - match self.key.as_str() { - key::ENTER => break, - key::BACKSPACE => { - if self.data.len() > 0 { - self.data.pop(); - print!("\x1b[1D \x1b[1D"); - flush(); - } - } - c => { - if self.data.len() < self.max_length { - self.data += c.to_string().as_str(); - print!("{c}"); - flush(); - } - } - } - } - print!("\n\n"); - return self.data.clone(); - } -} - -pub fn input_box(max_length: usize) -> ComponentWrapper { - ComponentWrapper::new(Input { - max_length, - key: String::new(), - data: String::new(), - }) -}