From a2ec2f9d416dcf399b69431a2df6739466d9fa4a Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 17 Jan 2025 13:18:39 +0100 Subject: [PATCH] few changes --- src/lib.rs | 91 ++++++----------------------------------------------- src/main.rs | 10 ++++-- 2 files changed, 16 insertions(+), 85 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cfd7c26..69e0e59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,15 @@ pub mod console; pub mod rsx; +pub mod state; pub mod utils; pub mod widgets; +pub mod prelude { + pub use crate::*; + pub use console::Event; + pub use crossterm::event::{KeyCode, KeyEvent}; +} + pub use std::io::Result; pub type Element = std::sync::Arc crate::Result<()>>; @@ -28,8 +35,6 @@ pub struct Frame { pub area: Area, } -pub struct State(*mut T); - impl Frame { pub fn draw(&self, w: &W, mut props: Area) -> Result<()> where @@ -147,84 +152,6 @@ impl Default for Area { } } -impl std::fmt::Display for State { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", unsafe { &*self.0 }) - } -} - -impl std::fmt::Debug for State { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "State({:?})", unsafe { &*self.0 }) - } -} - -//// State ops //// - -impl + Clone> std::ops::Add for State { - type Output = T; - - fn add(self, rhs: T) -> Self::Output { - unsafe { - let lhs = &mut *self.0; - lhs.clone() + rhs - } - } -} - -impl + Clone> std::ops::Sub for State { - type Output = T; - - fn sub(self, rhs: T) -> Self::Output { - unsafe { - let lhs = &mut *self.0; - lhs.clone() - rhs - } - } -} - -impl + Clone> std::ops::Div for State { - type Output = T; - - fn div(self, rhs: T) -> Self::Output { - unsafe { - let lhs = &mut *self.0; - lhs.clone() / rhs - } - } -} - -impl std::ops::AddAssign for State { - fn add_assign(&mut self, rhs: T) { - unsafe { *self.0 += rhs } - } -} - -impl std::ops::SubAssign for State { - fn sub_assign(&mut self, rhs: T) { - unsafe { *self.0 -= rhs } - } -} - -impl std::ops::Deref for State { - type Target = T; - fn deref(&self) -> &Self::Target { - unsafe { &*self.0 } - } -} - -impl std::ops::DerefMut for State { - fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { &mut *self.0 } - } -} - -impl State { - pub fn copy_state(&self) -> Self { - Self(self.0) - } -} - -pub fn use_state(v: T) -> State { - State(Box::into_raw(Box::new(v))) +pub fn use_state(v: T) -> state::State { + state::State(Box::into_raw(Box::new(v))) } diff --git a/src/main.rs b/src/main.rs index 0865a9c..8cea794 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use osui::*; +use osui::prelude::*; fn main() -> osui::Result<()> { let con = console::init()?; @@ -10,14 +10,18 @@ fn main() -> osui::Result<()> { let mut count1 = count.copy_state(); con.draw(std::sync::Arc::clone(&ui))?; - console::read()?; + if let Event::Key(KeyEvent { code, .. }) = console::read()? { + if code == KeyCode::Char('q') { + break; + } + } count1 += 1; } console::end() } -pub fn app() -> (Element, State) { +pub fn app() -> (Element, state::State) { let count = use_state(0); let count1 = count.copy_state();