few changes

This commit is contained in:
2025-01-17 01:01:14 +01:00
parent eeefd1f002
commit a6472f8e2e
4 changed files with 43 additions and 56 deletions
+27 -13
View File
@@ -1,3 +1,4 @@
pub mod console;
pub mod utils; pub mod utils;
pub mod widgets; pub mod widgets;
@@ -62,8 +63,8 @@ impl Frame {
println!( println!(
"\x1b[{};{}H{}", "\x1b[{};{}H{}",
props.x + (i as u16) + 1, props.y + (i as u16) + 1,
props.y + 1, props.x + 1,
line.chars().take(props.width as usize).collect::<String>() line.chars().take(props.width as usize).collect::<String>()
); );
} }
@@ -71,7 +72,7 @@ impl Frame {
Ok(()) Ok(())
} }
pub fn new(width: u16, height: u16) -> Self { pub fn new((width, height): (u16, u16)) -> Self {
let mut f = Self::default(); let mut f = Self::default();
f.area.width(width); f.area.width(width);
f.area.height(height); f.area.height(height);
@@ -84,20 +85,23 @@ impl Area {
Self::default() Self::default()
} }
pub fn center_x(&mut self) -> Self { pub fn center_x() -> Self {
self.center_x = true; let mut s = Self::default();
*self s.center_x = true;
s
} }
pub fn center_y(&mut self) -> Self { pub fn center_y() -> Self {
self.center_y = true; let mut s = Self::default();
*self s.center_y = true;
s
} }
pub fn center(&mut self) -> Self { pub fn center() -> Self {
self.center_x = true; let mut s = Self::default();
self.center_y = true; s.center_x = true;
*self s.center_y = true;
s
} }
pub fn width(&mut self, w: u16) -> Self { pub fn width(&mut self, w: u16) -> Self {
@@ -111,6 +115,16 @@ impl Area {
self.height_auto = false; self.height_auto = false;
*self *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 { impl Default for Area {
+11 -10
View File
@@ -1,15 +1,16 @@
use osui::*; use osui::*;
fn main() -> osui::Result<()> { fn main() -> osui::Result<()> {
utils::init()?; let con = console::init()?;
let (width, height) = crossterm::terminal::size()?; loop {
con.draw(|frame| -> Result<()> {
let frame = Frame::new(width, height); frame.draw(&"testing", Area::center())?;
frame.draw(&"\x1b[32mX\x1b[0m", Area::center_x())?;
frame.draw(&"Hello?", Area::new())?; frame.draw(&"\x1b[31mY\x1b[0m", Area::center_y())?;
Ok(())
utils::read()?; })?;
}
utils::end()
console::end()
} }
-34
View File
@@ -19,18 +19,6 @@ pub fn flush() -> crate::Result<()> {
std::io::stdout().flush() 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) { pub fn str_size(s: &str) -> (u16, u16) {
let mut height = 1; let mut height = 1;
let mut max_width = 0; let mut max_width = 0;
@@ -50,25 +38,3 @@ pub fn str_size(s: &str) -> (u16, u16) {
(max_width, height) (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<Event> {
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),
})
}
+6
View File
@@ -5,3 +5,9 @@ impl Widget for &str {
self.to_string() self.to_string()
} }
} }
impl Widget for String {
fn render(&self) -> String {
self.clone()
}
}