diff --git a/src/elements.rs b/src/elements.rs index 6226c60..a54b906 100644 --- a/src/elements.rs +++ b/src/elements.rs @@ -2,6 +2,12 @@ use crate::Element; impl Element for &str { fn render(&self, frame: &mut dyn crate::Framing) { - frame.draw_str("test"); + frame.draw_str(self); + } +} + +impl Element for crate::Ui { + fn render(&self, frame: &mut dyn crate::Framing) { + self(frame) } } diff --git a/src/lib.rs b/src/lib.rs index 626010c..ea0dcfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,14 +5,14 @@ pub mod style; pub mod utils; pub use std::io::Result; -use std::{io::Write, sync::Arc}; pub mod prelude { pub use crate::{ func, + state::State, style::{Color, Style}, utils::init, - Props, Result, Ui, + Framing, Props, Result, Ui, }; #[cfg(feature = "state")] @@ -23,7 +23,7 @@ pub mod prelude { // Traits pub trait Framing { - fn draw(&mut self, element: &mut dyn Element, props: &Props); + fn draw(&mut self, element: &dyn Element, props: &Props); fn draw_str(&mut self, s: &str) { _ = s } @@ -38,7 +38,7 @@ pub trait Element { // Types pub type Handler = std::sync::Arc>; -pub type Ui = Arc; +pub type Ui = Box; #[derive(Debug, Clone)] pub struct Props { @@ -50,11 +50,10 @@ pub struct Props { } /// Represents the console state, containing a frame for rendering and a mouse capture flag. -pub struct Console<'a> { +pub struct Console { mouse: bool, width: u16, height: u16, - handle: Option>, pub mouse_position: Option<(u16, u16)>, } @@ -139,8 +138,8 @@ impl Props { } } -impl Framing for Console<'_> { - fn draw(&mut self, element: &mut dyn Element, props: &Props) { +impl Framing for Console { + fn draw(&mut self, element: &dyn Element, props: &Props) { let mut written = String::new(); element.render(&mut written); @@ -175,33 +174,22 @@ impl Framing for Console<'_> { &format!("{}\n", " ".repeat(width as usize)).repeat(props.style.py as usize), ); - if let Some(handle) = &mut self.handle { - for (i, line) in written.lines().enumerate() { - write!( - handle, - "\x1b[{};{}H{ansi}{px}{}{px}\x1b[0m", - y as usize + i + 1, - x + 1, - line, - ) - .unwrap(); - } - } else { - panic!("Handle not found"); + for (i, line) in written.lines().enumerate() { + println!( + "\x1b[{};{}H{ansi}{px}{}{px}\x1b[0m", + y as usize + i + 1, + x + 1, + line, + ); } } } -impl Console<'_> { - pub fn render(&mut self, ui: Ui) -> Result<()> { +impl Console { + pub fn render(&mut self, ui: &Ui) -> Result<()> { (self.width, self.height) = crossterm::terminal::size()?; - let stdout = std::io::stdout(); - self.handle = Some(stdout.lock()); utils::clear()?; ui(self); - if let Some(handle) = &mut self.handle { - handle.flush()?; - } Ok(()) } @@ -215,8 +203,8 @@ impl Console<'_> { } } -impl crate::Framing for String { - fn draw(&mut self, element: &mut dyn crate::Element, _props: &crate::Props) { +impl Framing for String { + fn draw(&mut self, element: &dyn Element, _props: &Props) { element.render(self); } diff --git a/src/main.rs b/src/main.rs index ffe3109..84e6520 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,37 @@ -use std::sync::Arc; - use osmium_engine::prelude::*; fn main() -> Result<()> { - let mut console = init(false)?; + let mut console = init(true)?; - let ui = app(); + let (mut count, ui) = app(); - console.render(ui.clone())?; - - read()?; + loop { + console.render(&ui)?; + read()?; + count += 1; + } console.close() } -pub fn app() -> Ui { - Arc::new(move |frame| { - frame.draw(&mut "test", &Props::center()); - }) +pub fn app() -> (State, Ui) { + let (count, count_ui) = counter(); + + ( + count, + Box::new(move |frame| { + frame.draw(&count_ui, &Props::center()); + }), + ) +} + +pub fn counter() -> (State, Ui) { + let count = use_state(0); + + ( + count, + Box::new(move |frame| { + frame.draw(&format!("Events: {count}").as_str(), &Props::center()); + }), + ) } diff --git a/src/utils.rs b/src/utils.rs index adb36fe..d91ff45 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -100,7 +100,7 @@ pub fn str_size(s: &str) -> (u16, u16) { /// /// # Returns /// A `Console` instance wrapped in a `Result`. -pub fn init<'a>(mouse: bool) -> crate::Result> { +pub fn init<'a>(mouse: bool) -> crate::Result { crossterm::terminal::enable_raw_mode()?; clear()?; hide_cursor()?; @@ -112,7 +112,6 @@ pub fn init<'a>(mouse: bool) -> crate::Result> { width, height, mouse, - handle: None, mouse_position: None, }) }