(and broken)

This commit is contained in:
🪞👃🪞 2024-09-15 17:30:27 +03:00
parent f5fbc11b24
commit 530cba2bc8

View file

@ -15,18 +15,8 @@ pub struct Tui {
exited: Arc<AtomicBool>,
buffer: Buffer,
backend: CrosstermBackend<Stdout>,
event: RwLock<Option<TuiEvent>>,
area: [u16;4],
}
pub struct TuiInput {
event: TuiEvent,
exited: Arc<AtomicBool>,
}
pub struct TuiOutput {
pub buffer: Buffer,
pub area: [u16;4],
}
impl Engine for Tui {
type Unit = u16;
type Size = [Self::Unit;2];
@ -55,99 +45,6 @@ impl Engine for Tui {
disable_raw_mode().map_err(Into::into)
}
}
impl Input<Tui> for TuiInput {
type Event = TuiEvent;
fn event (&self) -> &TuiEvent {
&self.event
}
fn is_done (&self) -> bool {
self.exited.fetch_and(true, Ordering::Relaxed)
}
fn done (&self) {
self.exited.store(true, Ordering::Relaxed);
}
}
//impl Output<Tui> for Tui {
//#[inline] fn area (&self) -> <Self as Engine>::Area {
//self.area
//}
//#[inline] fn area_mut (&mut self) -> &mut <Self as Engine>::Area {
//&mut self.area
//}
//#[inline] fn render_in (&mut self,
//area: <Self as Engine>::Area,
//widget: &dyn Widget<Engine = Self>
//) -> Perhaps<<Self as Engine>::Area> {
//let last = self.area();
//*self.area_mut() = area;
//let next = widget.render(self)?;
//*self.area_mut() = last;
//Ok(next)
//}
//}
impl Output<Tui> for TuiOutput {
#[inline] fn area (&self) -> [u16;4] {
self.area
}
#[inline] fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.area
}
#[inline] fn render_in (&mut self,
area: [u16;4],
widget: &dyn Widget<Engine = Tui>
) -> Usually<()> {
let last = self.area();
*self.area_mut() = area;
widget.render(self)?;
*self.area_mut() = last;
Ok(())
}
}
impl TuiOutput {
pub fn buffer_update (&mut self,
area: [u16;4],
callback: &impl Fn(&mut Cell, u16, u16)
) {
buffer_update(&mut self.buffer, area, callback);
}
pub fn fill_bg (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{cell.set_bg(color);})
}
pub fn fill_fg (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{cell.set_fg(color);})
}
pub fn fill_ul (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{
cell.modifier = ratatui::prelude::Modifier::UNDERLINED;
cell.underline_color = color;
})
}
pub fn fill_char (&mut self, area: [u16;4], c: char) {
self.buffer_update(area, &|cell,_,_|{cell.set_char(c);})
}
pub fn make_dim (&mut self) {
for cell in self.buffer.content.iter_mut() {
cell.bg = ratatui::style::Color::Rgb(30,30,30);
cell.fg = ratatui::style::Color::Rgb(100,100,100);
cell.modifier = ratatui::style::Modifier::DIM;
}
}
pub fn blit (
&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>
) {
let text = text.as_ref();
let buf = &mut self.buffer;
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, text, style.unwrap_or(Style::default()));
}
}
#[inline]
pub fn with_rect (&mut self, area: [u16;4]) -> &mut Self {
self.area = area;
self
}
}
impl Tui {
/// Run the main loop.
pub fn run <R: Component<Tui> + Sized + 'static> (
@ -157,7 +54,6 @@ impl Tui {
let area = backend.size()?;
let engine = Self {
exited: Arc::new(AtomicBool::new(false)),
event: RwLock::new(None),
buffer: Buffer::empty(area),
area: area.xywh(),
backend,
@ -231,6 +127,88 @@ impl Tui {
buffer
}
}
pub struct TuiInput {
event: TuiEvent,
exited: Arc<AtomicBool>,
}
impl Input<Tui> for TuiInput {
type Event = TuiEvent;
fn event (&self) -> &TuiEvent {
&self.event
}
fn is_done (&self) -> bool {
self.exited.fetch_and(true, Ordering::Relaxed)
}
fn done (&self) {
self.exited.store(true, Ordering::Relaxed);
}
}
pub struct TuiOutput {
pub buffer: Buffer,
pub area: [u16;4],
}
impl Output<Tui> for TuiOutput {
#[inline] fn area (&self) -> [u16;4] {
self.area
}
#[inline] fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.area
}
#[inline] fn render_in (&mut self,
area: [u16;4],
widget: &dyn Widget<Engine = Tui>
) -> Usually<()> {
let last = self.area();
*self.area_mut() = area;
widget.render(self)?;
*self.area_mut() = last;
Ok(())
}
}
impl TuiOutput {
pub fn buffer_update (&mut self,
area: [u16;4],
callback: &impl Fn(&mut Cell, u16, u16)
) {
buffer_update(&mut self.buffer, area, callback);
}
pub fn fill_bg (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{cell.set_bg(color);})
}
pub fn fill_fg (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{cell.set_fg(color);})
}
pub fn fill_ul (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{
cell.modifier = ratatui::prelude::Modifier::UNDERLINED;
cell.underline_color = color;
})
}
pub fn fill_char (&mut self, area: [u16;4], c: char) {
self.buffer_update(area, &|cell,_,_|{cell.set_char(c);})
}
pub fn make_dim (&mut self) {
for cell in self.buffer.content.iter_mut() {
cell.bg = ratatui::style::Color::Rgb(30,30,30);
cell.fg = ratatui::style::Color::Rgb(100,100,100);
cell.modifier = ratatui::style::Modifier::DIM;
}
}
pub fn blit (
&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>
) {
let text = text.as_ref();
let buf = &mut self.buffer;
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, text, style.unwrap_or(Style::default()));
}
}
#[inline]
pub fn with_rect (&mut self, area: [u16;4]) -> &mut Self {
self.area = area;
self
}
}
#[derive(Debug, Clone)]
pub enum TuiEvent {
/// Terminal input