diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index d98c8d15..5a4c7799 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -11,8 +11,6 @@ use crossterm::terminal::{ enable_raw_mode, disable_raw_mode }; -submod! { tui_border tui_buffer tui_colors tui_layout } - pub struct Tui { exited: Arc, buffer: usize, @@ -224,3 +222,539 @@ pub fn half_block (lower: bool, upper: bool) -> Option { _ => None } } + +#[derive(Default)] +pub struct BigBuffer { + pub width: usize, + pub height: usize, + pub content: Vec +} + +impl BigBuffer { + pub fn new (width: usize, height: usize) -> Self { + Self { width, height, content: vec![Cell::default(); width*height] } + } + pub fn get (&self, x: usize, y: usize) -> Option<&Cell> { + let i = self.index_of(x, y); + self.content.get(i) + } + pub fn get_mut (&mut self, x: usize, y: usize) -> Option<&mut Cell> { + let i = self.index_of(x, y); + self.content.get_mut(i) + } + pub fn index_of (&self, x: usize, y: usize) -> usize { + y * self.width + x + } +} + +pub fn buffer_update (buf: &mut Buffer, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) { + for row in 0..area.h() { + let y = area.y() + row; + for col in 0..area.w() { + let x = area.x() + col; + if x < buf.area.width && y < buf.area.height { + callback(buf.get_mut(x, y), col, row); + } + } + } +} + +impl Widget for &str { + type Engine = Tui; + fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> { + let [x, y, ..] = area; + // TODO: line breaks + Ok(Some([x, y, self.len() as u16, 1])) + } + fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> { + let area = self.layout(to.area())?.unwrap(); + to.blit(&self, area.x(), area.y(), None)?; + Ok(Some(area)) + } +} + +pub struct Styled>(pub Option