few changes

This commit is contained in:
2025-01-17 22:13:42 +01:00
parent a2ec2f9d41
commit bde4438663
4 changed files with 104 additions and 15 deletions
+13
View File
@@ -15,6 +15,7 @@ pub fn init() -> crate::Result<Console> {
crossterm::terminal::enable_raw_mode()?;
crate::utils::clear()?;
crate::utils::hide_cursor()?;
crossterm::execute!(std::io::stdout(), crossterm::event::EnableMouseCapture)?;
Ok(Console(Frame::new(crossterm::terminal::size()?)))
}
@@ -29,6 +30,18 @@ impl Console {
crate::utils::clear()?;
f(&self.0)
}
pub fn run(&mut self, ui: Element) -> crate::Result<()> {
loop {
self.draw(ui.clone())?;
let event = read()?;
if let Event::Resize(w, h) = event {
self.0.area.width = w;
self.0.area.height = h;
}
}
}
}
pub fn read() -> crate::Result<Event> {
+3
View File
@@ -16,6 +16,9 @@ pub type Element = std::sync::Arc<dyn Fn(&Frame) -> crate::Result<()>>;
pub trait Widget {
fn render(&self) -> String;
fn event(&mut self, event: console::Event) {
_ = event;
}
}
#[derive(Debug, Clone, Copy)]
+3 -15
View File
@@ -1,22 +1,10 @@
use osui::prelude::*;
fn main() -> osui::Result<()> {
let con = console::init()?;
let mut con = console::init()?;
let (ui, count) = app();
loop {
let count = count.copy_state();
let mut count1 = count.copy_state();
con.draw(std::sync::Arc::clone(&ui))?;
if let Event::Key(KeyEvent { code, .. }) = console::read()? {
if code == KeyCode::Char('q') {
break;
}
}
count1 += 1;
}
let (ui, _count) = app();
con.run(ui).unwrap();
console::end()
}
+85
View File
@@ -0,0 +1,85 @@
pub struct State<T>(pub(crate) *mut T);
impl<T: std::fmt::Display> std::fmt::Display for State<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", unsafe { &*self.0 })
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for State<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "State({:?})", unsafe { &*self.0 })
}
}
//// State ops ////
impl<T: std::ops::Add<Output = T> + Clone> std::ops::Add<T> for State<T> {
type Output = T;
fn add(self, rhs: T) -> Self::Output {
unsafe {
let lhs = &mut *self.0;
lhs.clone() + rhs
}
}
}
impl<T: std::ops::Sub<Output = T> + Clone> std::ops::Sub<T> for State<T> {
type Output = T;
fn sub(self, rhs: T) -> Self::Output {
unsafe {
let lhs = &mut *self.0;
lhs.clone() - rhs
}
}
}
impl<T: std::ops::Div<Output = T> + Clone> std::ops::Div<T> for State<T> {
type Output = T;
fn div(self, rhs: T) -> Self::Output {
unsafe {
let lhs = &mut *self.0;
lhs.clone() / rhs
}
}
}
impl<T: std::ops::AddAssign + Clone> std::ops::AddAssign<T> for State<T> {
fn add_assign(&mut self, rhs: T) {
unsafe { *self.0 += rhs }
}
}
impl<T: std::ops::SubAssign + Clone> std::ops::SubAssign<T> for State<T> {
fn sub_assign(&mut self, rhs: T) {
unsafe { *self.0 -= rhs }
}
}
impl<T> std::ops::Deref for State<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*self.0 }
}
}
impl<T> std::ops::DerefMut for State<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *self.0 }
}
}
impl<T> State<T> {
pub fn copy_state(&self) -> Self {
Self(self.0)
}
}
impl<T: PartialEq<T>> PartialEq<T> for State<T> {
fn eq(&self, other: &T) -> bool {
unsafe { &*self.0 == other }
}
}