From eaf5828d27399465b1b5a8866767776fa4fd5547 Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 15 Oct 2024 12:33:13 +0200 Subject: [PATCH] working xml --- Cargo.toml | 2 +- src/lib.rs | 141 ++++++---------------------------------------------- src/main.rs | 12 +++-- 3 files changed, 25 insertions(+), 130 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3495c59..a90f369 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,4 @@ name = "osui" path = "src/main.rs" [dependencies] -crossterm = "0.28.1" +crossterm = "0.28.1" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e22e70c..333773e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,130 +1,19 @@ -use crossterm::terminal::{self, disable_raw_mode, enable_raw_mode}; -use std::{ - io::{self, Read, Write}, - str::from_utf8, -}; -pub mod key; -pub mod ui; - -#[derive(Clone, Copy)] -pub struct ComponentWrapper { - pub contents: T, - pub x: usize, - pub y: usize, - update_fn: fn(&mut T) -> bool, -} - -pub trait Component { - fn render(&self) -> String { - String::new() - } - - fn run(&mut self, x: usize, y: usize, update_fn: fn(&mut Self) -> bool) -> String { - let _ = x; - let _ = y; - let _ = update_fn; - String::new() - } -} - -impl ComponentWrapper { - pub fn new(component: T) -> ComponentWrapper { - ComponentWrapper { - contents: component, - x: 0, - y: 0, - update_fn: (|_| false), - } - } - pub fn run(&mut self) -> String { - self.contents.run(self.x, self.y, self.update_fn) - } - pub fn render(&self, frame: &mut Vec) { - for (i, line) in self.contents.render().split("\n").enumerate() { - frame[self.y + i] = _render_line(frame[self.y + i].clone(), line.to_string(), self.x); - } - } - pub fn update(&mut self, update_fn: fn(&mut T) -> bool) { - self.update_fn = update_fn; - } -} - -pub fn clear() { - print!("\x1b[H\x1b[2J\x1b[3J"); - flush(); -} - -pub fn flush() { - io::stdout().flush().unwrap(); -} - -pub fn create_frame() -> Vec { - clear(); - let (w, h) = terminal::size().unwrap(); - vec![" ".repeat(w as usize); h as usize - 1] -} - -fn _render_line(frame_line: String, line: String, x: usize) -> String { - let mut result = String::new(); - if x >= frame_line.len() { - return result; - } - let lchars: Vec = line.chars().collect(); - for (i, c) in frame_line.chars().enumerate() { - if i >= x && lchars.len() > (i - x) { - result += lchars[i - x].to_string().as_str(); - } else { - result += c.to_string().as_str(); - } - } - result -} - -fn read_key_() -> String { - let mut buf = [0u8; 2]; - io::stdin().read(&mut buf).unwrap(); - let mut res = from_utf8(&buf).unwrap().to_string(); - if !res.ends_with("\0") { - res += read_key_().as_str() - } - return res.strip_suffix("\0").unwrap_or(&res).to_string(); -} - -pub fn read_key() -> String { - enable_raw_mode().unwrap(); - let res = read_key_(); - disable_raw_mode().unwrap(); - return res; +#[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() }; } #[macro_export] -macro_rules! render_frame { - ($first:expr) => {{ - let mut frame = osui::create_frame(); - $first.render(&mut frame); - print!("{}", frame.join("\n")); - frame - }}; - ($first:expr, $($rest:expr),+) => {{ - let mut frame = osui::create_frame(); - $first.render(&mut frame); - $( $rest.render(&mut frame); )+ - print!("{}", frame.join("\n")); - frame - }}; -} - -#[macro_export] -macro_rules! srender_frame { - ($first:expr) => {{ - let mut frame = create_frame(); - $first.render(&mut frame); - frame - }}; - ($first:expr, $($rest:expr),+) => {{ - let mut frame = create_frame(); - $first.render(&mut frame); - $( $rest.render(&mut frame); )+ - frame - }}; +macro_rules! xml { + (<$tag:ident> {$($inner:tt)*}) => { + osui::__xml!(<$tag> {$($inner)*}) + }; } diff --git a/src/main.rs b/src/main.rs index 07cf600..504e29c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,12 @@ -use osui::{self, render_frame, ui}; +use osui::xml; fn main() { - let txt = ui::text("Hello World!"); - render_frame!(txt); + let doc = xml!( +
{ +
{ + } + } + ); + + println!("{doc}"); }