improvements
This commit is contained in:
+7
-1
@@ -2,6 +2,12 @@ use crate::Element;
|
||||
|
||||
impl Element for &str {
|
||||
fn render(&self, frame: &mut dyn crate::Framing) {
|
||||
frame.draw_str("test");
|
||||
frame.draw_str(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl Element for crate::Ui {
|
||||
fn render(&self, frame: &mut dyn crate::Framing) {
|
||||
self(frame)
|
||||
}
|
||||
}
|
||||
|
||||
+13
-25
@@ -5,14 +5,14 @@ pub mod style;
|
||||
pub mod utils;
|
||||
|
||||
pub use std::io::Result;
|
||||
use std::{io::Write, sync::Arc};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::{
|
||||
func,
|
||||
state::State,
|
||||
style::{Color, Style},
|
||||
utils::init,
|
||||
Props, Result, Ui,
|
||||
Framing, Props, Result, Ui,
|
||||
};
|
||||
|
||||
#[cfg(feature = "state")]
|
||||
@@ -23,7 +23,7 @@ pub mod prelude {
|
||||
|
||||
// Traits
|
||||
pub trait Framing {
|
||||
fn draw(&mut self, element: &mut dyn Element, props: &Props);
|
||||
fn draw(&mut self, element: &dyn Element, props: &Props);
|
||||
fn draw_str(&mut self, s: &str) {
|
||||
_ = s
|
||||
}
|
||||
@@ -38,7 +38,7 @@ pub trait Element {
|
||||
|
||||
// Types
|
||||
pub type Handler<T> = std::sync::Arc<std::sync::Mutex<T>>;
|
||||
pub type Ui = Arc<dyn Fn(&mut dyn Framing)>;
|
||||
pub type Ui = Box<dyn Fn(&mut dyn Framing)>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Props {
|
||||
@@ -50,11 +50,10 @@ pub struct Props {
|
||||
}
|
||||
|
||||
/// Represents the console state, containing a frame for rendering and a mouse capture flag.
|
||||
pub struct Console<'a> {
|
||||
pub struct Console {
|
||||
mouse: bool,
|
||||
width: u16,
|
||||
height: u16,
|
||||
handle: Option<std::io::StdoutLock<'a>>,
|
||||
pub mouse_position: Option<(u16, u16)>,
|
||||
}
|
||||
|
||||
@@ -139,8 +138,8 @@ impl Props {
|
||||
}
|
||||
}
|
||||
|
||||
impl Framing for Console<'_> {
|
||||
fn draw(&mut self, element: &mut dyn Element, props: &Props) {
|
||||
impl Framing for Console {
|
||||
fn draw(&mut self, element: &dyn Element, props: &Props) {
|
||||
let mut written = String::new();
|
||||
element.render(&mut written);
|
||||
|
||||
@@ -175,33 +174,22 @@ impl Framing for Console<'_> {
|
||||
&format!("{}\n", " ".repeat(width as usize)).repeat(props.style.py as usize),
|
||||
);
|
||||
|
||||
if let Some(handle) = &mut self.handle {
|
||||
for (i, line) in written.lines().enumerate() {
|
||||
write!(
|
||||
handle,
|
||||
println!(
|
||||
"\x1b[{};{}H{ansi}{px}{}{px}\x1b[0m",
|
||||
y as usize + i + 1,
|
||||
x + 1,
|
||||
line,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
} else {
|
||||
panic!("Handle not found");
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Console<'_> {
|
||||
pub fn render(&mut self, ui: Ui) -> Result<()> {
|
||||
impl Console {
|
||||
pub fn render(&mut self, ui: &Ui) -> Result<()> {
|
||||
(self.width, self.height) = crossterm::terminal::size()?;
|
||||
let stdout = std::io::stdout();
|
||||
self.handle = Some(stdout.lock());
|
||||
utils::clear()?;
|
||||
ui(self);
|
||||
if let Some(handle) = &mut self.handle {
|
||||
handle.flush()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -215,8 +203,8 @@ impl Console<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::Framing for String {
|
||||
fn draw(&mut self, element: &mut dyn crate::Element, _props: &crate::Props) {
|
||||
impl Framing for String {
|
||||
fn draw(&mut self, element: &dyn Element, _props: &Props) {
|
||||
element.render(self);
|
||||
}
|
||||
|
||||
|
||||
+26
-10
@@ -1,21 +1,37 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use osmium_engine::prelude::*;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut console = init(false)?;
|
||||
let mut console = init(true)?;
|
||||
|
||||
let ui = app();
|
||||
|
||||
console.render(ui.clone())?;
|
||||
let (mut count, ui) = app();
|
||||
|
||||
loop {
|
||||
console.render(&ui)?;
|
||||
read()?;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
console.close()
|
||||
}
|
||||
|
||||
pub fn app() -> Ui {
|
||||
Arc::new(move |frame| {
|
||||
frame.draw(&mut "test", &Props::center());
|
||||
})
|
||||
pub fn app() -> (State<i32>, Ui) {
|
||||
let (count, count_ui) = counter();
|
||||
|
||||
(
|
||||
count,
|
||||
Box::new(move |frame| {
|
||||
frame.draw(&count_ui, &Props::center());
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn counter() -> (State<i32>, Ui) {
|
||||
let count = use_state(0);
|
||||
|
||||
(
|
||||
count,
|
||||
Box::new(move |frame| {
|
||||
frame.draw(&format!("Events: {count}").as_str(), &Props::center());
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
+1
-2
@@ -100,7 +100,7 @@ pub fn str_size(s: &str) -> (u16, u16) {
|
||||
///
|
||||
/// # Returns
|
||||
/// A `Console` instance wrapped in a `Result`.
|
||||
pub fn init<'a>(mouse: bool) -> crate::Result<crate::Console<'a>> {
|
||||
pub fn init<'a>(mouse: bool) -> crate::Result<crate::Console> {
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
clear()?;
|
||||
hide_cursor()?;
|
||||
@@ -112,7 +112,6 @@ pub fn init<'a>(mouse: bool) -> crate::Result<crate::Console<'a>> {
|
||||
width,
|
||||
height,
|
||||
mouse,
|
||||
handle: None,
|
||||
mouse_position: None,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user