state management
This commit is contained in:
+2
-5
@@ -1,4 +1,4 @@
|
|||||||
use crate::Frame;
|
use crate::{Element, Frame};
|
||||||
|
|
||||||
pub struct Console(Frame);
|
pub struct Console(Frame);
|
||||||
|
|
||||||
@@ -25,10 +25,7 @@ pub fn end() -> crate::Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Console {
|
impl Console {
|
||||||
pub fn draw<F>(&self, mut f: F) -> crate::Result<()>
|
pub fn draw(&self, f: Element) -> crate::Result<()> {
|
||||||
where
|
|
||||||
F: FnMut(&Frame) -> crate::Result<()>,
|
|
||||||
{
|
|
||||||
crate::utils::clear()?;
|
crate::utils::clear()?;
|
||||||
f(&self.0)
|
f(&self.0)
|
||||||
}
|
}
|
||||||
|
|||||||
+87
@@ -1,9 +1,12 @@
|
|||||||
pub mod console;
|
pub mod console;
|
||||||
|
pub mod rsx;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
pub mod widgets;
|
pub mod widgets;
|
||||||
|
|
||||||
pub use std::io::Result;
|
pub use std::io::Result;
|
||||||
|
|
||||||
|
pub type Element = std::sync::Arc<dyn Fn(&Frame) -> crate::Result<()>>;
|
||||||
|
|
||||||
pub trait Widget {
|
pub trait Widget {
|
||||||
fn render(&self) -> String;
|
fn render(&self) -> String;
|
||||||
}
|
}
|
||||||
@@ -25,6 +28,8 @@ pub struct Frame {
|
|||||||
pub area: Area,
|
pub area: Area,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct State<T>(*mut T);
|
||||||
|
|
||||||
impl Frame {
|
impl Frame {
|
||||||
pub fn draw<W>(&self, w: &W, mut props: Area) -> Result<()>
|
pub fn draw<W>(&self, w: &W, mut props: Area) -> Result<()>
|
||||||
where
|
where
|
||||||
@@ -141,3 +146,85 @@ impl Default for Area {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_state<T>(v: T) -> State<T> {
|
||||||
|
State(Box::into_raw(Box::new(v)))
|
||||||
|
}
|
||||||
|
|||||||
+20
-6
@@ -3,14 +3,28 @@ use osui::*;
|
|||||||
fn main() -> osui::Result<()> {
|
fn main() -> osui::Result<()> {
|
||||||
let con = console::init()?;
|
let con = console::init()?;
|
||||||
|
|
||||||
|
let (ui, count) = app();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
con.draw(|frame| -> Result<()> {
|
let count = count.copy_state();
|
||||||
frame.draw(&"testing", Area::center())?;
|
let mut count1 = count.copy_state();
|
||||||
frame.draw(&"\x1b[32mX\x1b[0m", Area::center_x())?;
|
con.draw(std::sync::Arc::clone(&ui))?;
|
||||||
frame.draw(&"\x1b[31mY\x1b[0m", Area::center_y())?;
|
|
||||||
Ok(())
|
console::read()?;
|
||||||
})?;
|
count1 += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
console::end()
|
console::end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn app() -> (Element, State<i32>) {
|
||||||
|
let count = use_state(0);
|
||||||
|
let count1 = count.copy_state();
|
||||||
|
|
||||||
|
(
|
||||||
|
rsx! {
|
||||||
|
"{count}"
|
||||||
|
},
|
||||||
|
count1,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
#[macro_export]
|
||||||
|
macro_rules! rsx {
|
||||||
|
($($e:literal)*) => {
|
||||||
|
std::sync::Arc::new(
|
||||||
|
move |frame: &$crate::Frame| -> $crate::Result<()> {
|
||||||
|
$(
|
||||||
|
frame.draw(&format!($e), Area::center())?;
|
||||||
|
)*
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user