working structure

This commit is contained in:
2024-10-15 13:20:04 +02:00
parent eaf5828d27
commit 9e013f737f
6 changed files with 60 additions and 96 deletions
+16
View File
@@ -0,0 +1,16 @@
use crate::{Component, ComponentParams};
#[derive(Debug)]
pub struct Div {
pub test: i32,
pub children: Vec<Box<dyn Component>>,
}
impl Component for Div {}
pub fn div(params: ComponentParams) -> Div {
Div {
test: 0,
children: params.children,
}
}
-8
View File
@@ -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";
+8 -17
View File
@@ -1,19 +1,10 @@
#[macro_export] use std::fmt::Debug;
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() };
}
#[macro_export] pub mod components;
macro_rules! xml { pub mod macros;
(<$tag:ident> {$($inner:tt)*}) => {
osui::__xml!(<$tag> {$($inner)*}) pub trait Component: Debug {}
};
pub struct ComponentParams {
pub children: Vec<Box<dyn Component>>,
} }
+31
View File
@@ -0,0 +1,31 @@
#[macro_export]
macro_rules! __oml {
($( ($tag:ident $($k:ident = $v:expr)*) {$($inner:tt)*} )+) => {{
let mut comps: Vec<Box<dyn Component>> = 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)
}};
}
+5 -6
View File
@@ -1,12 +1,11 @@
use osui::xml; use osui::{components::div, oml, Component, ComponentParams};
fn main() { fn main() {
let doc = xml!( let doc = oml!(
<div> { (div) {
<div> { (div) {}
}
} }
); );
println!("{doc}"); println!("{:?}", doc);
} }
-65
View File
@@ -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<String> {
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<Input> {
ComponentWrapper::new(Input {
max_length,
key: String::new(),
data: String::new(),
})
}