This commit is contained in:
2026-01-24 01:00:21 +01:00
parent c2b9bb3aa1
commit 2a62e53ed5
3 changed files with 68 additions and 14 deletions
+58
View File
@@ -0,0 +1,58 @@
use std::sync::{Arc, Mutex};
use crate::{prelude::Context, DrawContext, View};
pub trait Engine {
fn render(&self, ctx: &mut DrawContext, view: View);
fn draw_text(&self, ctx: &mut DrawContext, text: &str);
}
pub struct Console {
threads: Mutex<Vec<Arc<dyn Fn(Arc<Context>) + Send + Sync>>>,
}
impl Console {
pub fn new() -> Self {
Self {
threads: Mutex::new(Vec::new()),
}
}
pub fn thread<F: Fn(Arc<Context>) + Send + Sync + 'static>(&self, run: F) {
self.threads.lock().unwrap().push(Arc::new(run));
}
pub fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) {
let cx = Context::new(component);
cx.refresh();
for thread in self.threads.lock().unwrap().iter() {
let thread = thread.clone();
std::thread::spawn({
let cx = cx.clone();
move || thread(cx)
});
}
let (width, height) = crossterm::terminal::size().unwrap();
loop {
cx.get_view()(&mut DrawContext::new(width, height));
std::thread::sleep(std::time::Duration::from_millis(16));
}
}
}
impl Engine for Console {
fn render(&self, ctx: &mut DrawContext, view: View) {
view(&mut DrawContext::new(
ctx.available.width,
ctx.available.height,
));
}
fn draw_text(&self, _ctx: &mut DrawContext, text: &str) {
println!("{}", text);
}
}
+5
View File
@@ -1,10 +1,12 @@
use std::sync::Arc;
pub mod component;
pub mod engine;
pub mod state;
pub mod prelude {
pub use crate::component::*;
pub use crate::engine::*;
pub use crate::state::*;
pub use crate::{DrawContext, View};
}
@@ -23,6 +25,9 @@ pub struct DrawContext {
used: Size,
}
#[derive(Debug, Clone)]
pub struct RenderContext {}
impl DrawContext {
pub fn new(width: u16, height: u16) -> Self {
Self {