shorten TuiIn, TuiOut

This commit is contained in:
🪞👃🪞 2024-12-31 23:42:35 +01:00
parent ca16a91015
commit 21741ebc52
20 changed files with 77 additions and 75 deletions

View file

@ -68,7 +68,7 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
}
}
impl Handle<Tui> for TestComponent {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
fn handle (&mut self, from: &TuiIn) -> Perhaps<bool> {
Ok(None)
}
}

View file

@ -39,9 +39,9 @@ impl Engine for Tui {
type Unit = u16;
type Size = [Self::Unit;2];
type Area = [Self::Unit;4];
type Input = TuiInput;
type Input = TuiIn;
type Handled = bool;
type Output = TuiOutput;
type Output = TuiOut;
fn exited (&self) -> bool {
self.exited.fetch_and(true, Relaxed)
}
@ -125,7 +125,7 @@ impl<T: Content<Tui> + Handle<Tui> + Sized + 'static> TuiRun<T> for Arc<RwLock<T
},
_ => {
let exited = exited.clone();
if let Err(e) = state.write().unwrap().handle(&TuiInput { event, exited }) {
if let Err(e) = state.write().unwrap().handle(&TuiIn { event, exited }) {
panic!("{e}")
}
}
@ -153,7 +153,7 @@ impl<T: Content<Tui> + Handle<Tui> + Sized + 'static> TuiRun<T> for Arc<RwLock<T
buffer.resize(size);
buffer.reset();
}
let mut output = TuiOutput { buffer, area: [0, 0, width, height] };
let mut output = TuiOut { buffer, area: [0, 0, width, height] };
state.render(&mut output);
buffer = engine.write().unwrap().flip(output.buffer, size);
}

View file

@ -1,12 +1,12 @@
use crate::{*, tui::*};
#[derive(Debug, Clone)]
pub struct TuiInput {
pub struct TuiIn {
pub(crate) exited: Arc<AtomicBool>,
pub(crate) event: crossterm::event::Event,
}
impl Input<Tui> for TuiInput {
impl Input<Tui> for TuiIn {
type Event = crossterm::event::Event;
fn event (&self) -> &crossterm::event::Event {
&self.event
@ -124,7 +124,7 @@ pub const fn shift (key: KeyEvent) -> KeyEvent {
/*
impl TuiInput {
impl TuiIn {
// TODO remove
pub fn handle_keymap <T> (&self, state: &mut T, keymap: &KeyMap<T>) -> Usually<bool> {
match self.event() {

View file

@ -1,11 +1,11 @@
use crate::{*, tui::*};
pub struct TuiOutput {
pub struct TuiOut {
pub buffer: Buffer,
pub area: [u16;4]
}
impl Output<Tui> for TuiOutput {
impl Output<Tui> for TuiOut {
#[inline] fn area (&self) -> [u16;4] { self.area }
#[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area }
#[inline] fn place (&mut self, area: [u16;4], content: &impl Content<Tui>) {
@ -20,7 +20,7 @@ impl Output<Tui> for TuiOutput {
}
}
impl TuiOutput {
impl TuiOut {
pub fn buffer_update (&mut self, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) {
buffer_update(&mut self.buffer, area, callback);
}
@ -69,6 +69,38 @@ impl TuiOutput {
}
}
impl Content<Tui> for &str {
fn area (&self, to: [u16;4]) -> [u16;4] {
[to[0], to[1], self.chars().count() as u16, 1]
}
fn render (&self, to: &mut TuiOut) {
to.blit(self, to.area.x(), to.area.y(), None)
}
}
impl Content<Tui> for String {
fn area (&self, to: [u16;4]) -> [u16;4] {
[to[0], to[1], self.chars().count() as u16, 1]
}
fn render (&self, to: &mut TuiOut) {
to.blit(self, to.area.x(), to.area.y(), None)
}
}
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 Area<u16> for Rect {
//fn x (&self) -> u16 { self.x }
//fn y (&self) -> u16 { self.y }
@ -86,33 +118,3 @@ pub fn half_block (lower: bool, upper: bool) -> Option<char> {
}
//impl<T: Content<Tui>> Render<Tui> for T {}
impl Content<Tui> for &str {
fn area (&self, to: [u16;4]) -> [u16;4] {
[to[0], to[1], self.chars().count() as u16, 1]
}
fn render (&self, to: &mut TuiOutput) {
to.blit(self, to.area.x(), to.area.y(), None)
}
}
impl Content<Tui> for String {
fn area (&self, to: [u16;4]) -> [u16;4] {
[to[0], to[1], self.chars().count() as u16, 1]
}
fn render (&self, to: &mut TuiOutput) {
to.blit(self, to.area.x(), to.area.y(), None)
}
}
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);
}
}
}
}