refactor table render; fix backspace; only works in col 0?

This commit is contained in:
🪞👃🪞 2025-03-22 01:52:58 +02:00
parent 6e0bf1e4b6
commit ac8e3dd198
3 changed files with 51 additions and 26 deletions

View file

@ -1,6 +1,7 @@
#![allow(stable_features)]
#![feature(os_str_display)]
#![feature(str_as_str)]
#![feature(let_chains)]
use std::sync::{Arc, RwLock};
use std::path::{Path, PathBuf};

View file

@ -91,7 +91,11 @@ impl Taggart {
}
}
pub fn edit_backspace (&mut self) {
todo!()
if let Some((index, value)) = &mut self.editing {
let mut chars = value.chars();
chars.next_back();
self.editing = Some((*index, chars.as_str().into()));
}
}
pub fn edit_delete (&mut self) {
todo!()

View file

@ -4,36 +4,26 @@ pub struct TreeTable<'a>(pub(crate) &'a Taggart);
impl<'a> Content<TuiOut> for TreeTable<'a> {
fn render (&self, to: &mut TuiOut) {
let Taggart { offset, paths, cursor, columns, column, .. } = self.0;
let (active_x, active_w) = columns.xw(*column);
let active_w = active_w.saturating_sub(1);
let area = to.area();
let Taggart { offset, paths, cursor, column, .. } = self.0;
let (x, w) = self.0.columns.xw(*column);
to.fill_bg([area.x() + x, area.y(), w, area.h()], Color::Rgb(0, 0, 0));
for (i, y) in area.iter_y().enumerate() {
let i_offset = i + offset;
let selected = *cursor == i_offset;
let mut x = area.x();
if let Some(entry) = paths.get(i_offset) {
to.fill_bg([area.x() + active_x, area.y(), active_w, area.h()], Color::Rgb(0, 0, 0));
for (row_index, row_y) in area.iter_y().enumerate() {
let row_index_scrolled = row_index + offset;
let selected = *cursor == row_index_scrolled;
let mut column_x = area.x();
if let Some(entry) = paths.get(row_index_scrolled) {
for (index, _fragment) in entry.path.iter().enumerate() {
if index == entry.depth - 1 {
let _cursor = if selected { ">" } else { " " };
to.area[1] = y;
for Column { width, value, .. } in self.0.columns.0.iter() {
to.area[0] = x;
if let Some(value) = value(entry) {
Content::render(&TrimStringRef(*width as u16, &value), to);
}
x += *width as u16 + 1;
}
to.area[1] = row_y;
if selected {
let fill = [area.x(), y, area.w(), 1];
to.fill_fg(fill, Color::Rgb(0, 0, 0));
to.fill_bg(fill, Color::Rgb(192, 128, 0));
let fill = [area.x() + x as u16, y, w, 1];
to.fill_bg(fill, Color::Rgb(224, 192, 0));
if let Some((_index, value)) = &self.0.editing {
let x = area.x() + if x > 0 { x + 1 } else { x } as u16;
to.blit(&value, x, y, None)
Self::row_cursor(to, area.x(), row_y, area.w());
}
self.row_data(to, entry, row_index_scrolled, &mut column_x);
if selected {
self.col_cursor(to, area.x(), active_x, column_x, row_y, active_w);
}
}
}
@ -45,6 +35,36 @@ impl<'a> Content<TuiOut> for TreeTable<'a> {
}
}
impl<'a> TreeTable<'a> {
fn row_cursor (to: &mut TuiOut, x: u16, y: u16, w: u16) {
let fill = [x, y, w, 1];
to.fill_fg(fill, Color::Rgb(0, 0, 0));
to.fill_bg(fill, Color::Rgb(192, 128, 0));
}
fn row_data (&self, to: &mut TuiOut, entry: &Entry, row: usize, x: &mut u16) {
for (column_index, Column { width, value, .. }) in self.0.columns.0.iter().enumerate() {
to.area[0] = *x;
if let Some((edit_index, value)) = self.0.editing.as_ref()
&& *edit_index == column_index
&& row == self.0.cursor
{
Content::render(&TrimStringRef(*width as u16, &value), to);
} else if let Some(value) = value(entry) {
Content::render(&TrimStringRef(*width as u16, &value), to);
}
*x += *width as u16 + 1;
}
}
fn col_cursor (&self, to: &mut TuiOut, xa: u16, x0: u16, x: u16, y: u16, w: u16) {
let fill = [xa + x0, y, w, 1];
to.fill_bg(fill, Color::Rgb(224, 192, 0));
if let Some((_index, value)) = &self.0.editing {
let x = xa + if x > 0 { x + 1 } else { x } as u16;
to.blit(&value, x, y, None)
}
}
}
pub struct Column<T> {
pub title: Arc<str>,
pub width: usize,