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