From a6472f8e2eff3e7f25bb1bb93f8057ddae91f6f4 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 17 Jan 2025 01:01:14 +0100 Subject: [PATCH] few changes --- src/lib.rs | 40 +++++++++++++++++++++++++++------------- src/main.rs | 19 ++++++++++--------- src/utils.rs | 34 ---------------------------------- src/widgets.rs | 6 ++++++ 4 files changed, 43 insertions(+), 56 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d965919..1cf0f6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod console; pub mod utils; pub mod widgets; @@ -62,8 +63,8 @@ impl Frame { println!( "\x1b[{};{}H{}", - props.x + (i as u16) + 1, - props.y + 1, + props.y + (i as u16) + 1, + props.x + 1, line.chars().take(props.width as usize).collect::() ); } @@ -71,7 +72,7 @@ impl Frame { Ok(()) } - pub fn new(width: u16, height: u16) -> Self { + pub fn new((width, height): (u16, u16)) -> Self { let mut f = Self::default(); f.area.width(width); f.area.height(height); @@ -84,20 +85,23 @@ impl Area { Self::default() } - pub fn center_x(&mut self) -> Self { - self.center_x = true; - *self + pub fn center_x() -> Self { + let mut s = Self::default(); + s.center_x = true; + s } - pub fn center_y(&mut self) -> Self { - self.center_y = true; - *self + pub fn center_y() -> Self { + let mut s = Self::default(); + s.center_y = true; + s } - pub fn center(&mut self) -> Self { - self.center_x = true; - self.center_y = true; - *self + pub fn center() -> Self { + let mut s = Self::default(); + s.center_x = true; + s.center_y = true; + s } pub fn width(&mut self, w: u16) -> Self { @@ -111,6 +115,16 @@ impl Area { self.height_auto = false; *self } + + pub fn x(&mut self, x: u16) -> Self { + self.x = x; + *self + } + + pub fn y(&mut self, y: u16) -> Self { + self.y = y; + *self + } } impl Default for Area { diff --git a/src/main.rs b/src/main.rs index d96cc29..44c41c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,16 @@ use osui::*; fn main() -> osui::Result<()> { - utils::init()?; + let con = console::init()?; - let (width, height) = crossterm::terminal::size()?; + loop { + con.draw(|frame| -> Result<()> { + frame.draw(&"testing", Area::center())?; + frame.draw(&"\x1b[32mX\x1b[0m", Area::center_x())?; + frame.draw(&"\x1b[31mY\x1b[0m", Area::center_y())?; + Ok(()) + })?; + } - let frame = Frame::new(width, height); - - frame.draw(&"Hello?", Area::new())?; - - utils::read()?; - - utils::end() + console::end() } diff --git a/src/utils.rs b/src/utils.rs index 30214eb..99d37a4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,18 +19,6 @@ pub fn flush() -> crate::Result<()> { std::io::stdout().flush() } -pub fn init() -> crate::Result<()> { - crossterm::terminal::enable_raw_mode()?; - clear()?; - hide_cursor() -} - -pub fn end() -> crate::Result<()> { - crossterm::terminal::disable_raw_mode()?; - clear()?; - show_cursor() -} - pub fn str_size(s: &str) -> (u16, u16) { let mut height = 1; let mut max_width = 0; @@ -50,25 +38,3 @@ pub fn str_size(s: &str) -> (u16, u16) { (max_width, height) } - -pub enum Event { - Key(crossterm::event::KeyEvent), - Resize(u16, u16), - Mouse(crossterm::event::MouseEvent), - Paste(String), - FocusGained, - FocusLost, -} - -pub fn read() -> crate::Result { - let event = crossterm::event::read()?; - - Ok(match event { - crossterm::event::Event::Key(k) => Event::Key(k), - crossterm::event::Event::Resize(w, h) => Event::Resize(w, h), - crossterm::event::Event::FocusGained => Event::FocusGained, - crossterm::event::Event::FocusLost => Event::FocusLost, - crossterm::event::Event::Mouse(m) => Event::Mouse(m), - crossterm::event::Event::Paste(p) => Event::Paste(p), - }) -} diff --git a/src/widgets.rs b/src/widgets.rs index b689ace..fb99bb8 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -5,3 +5,9 @@ impl Widget for &str { self.to_string() } } + +impl Widget for String { + fn render(&self) -> String { + self.clone() + } +}