mirror of
https://codeberg.org/unspeaker/perch.git
synced 2025-12-06 09:36:42 +01:00
refactor table render; fix backspace; only works in col 0?
This commit is contained in:
parent
6e0bf1e4b6
commit
ac8e3dd198
3 changed files with 51 additions and 26 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
#![allow(stable_features)]
|
#![allow(stable_features)]
|
||||||
#![feature(os_str_display)]
|
#![feature(os_str_display)]
|
||||||
#![feature(str_as_str)]
|
#![feature(str_as_str)]
|
||||||
|
#![feature(let_chains)]
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
|
||||||
|
|
@ -91,7 +91,11 @@ impl Taggart {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn edit_backspace (&mut self) {
|
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) {
|
pub fn edit_delete (&mut self) {
|
||||||
todo!()
|
todo!()
|
||||||
|
|
|
||||||
|
|
@ -4,36 +4,26 @@ pub struct TreeTable<'a>(pub(crate) &'a Taggart);
|
||||||
|
|
||||||
impl<'a> Content<TuiOut> for TreeTable<'a> {
|
impl<'a> Content<TuiOut> for TreeTable<'a> {
|
||||||
fn render (&self, to: &mut TuiOut) {
|
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 area = to.area();
|
||||||
let Taggart { offset, paths, cursor, column, .. } = self.0;
|
to.fill_bg([area.x() + active_x, area.y(), active_w, area.h()], Color::Rgb(0, 0, 0));
|
||||||
let (x, w) = self.0.columns.xw(*column);
|
for (row_index, row_y) in area.iter_y().enumerate() {
|
||||||
to.fill_bg([area.x() + x, area.y(), w, area.h()], Color::Rgb(0, 0, 0));
|
let row_index_scrolled = row_index + offset;
|
||||||
for (i, y) in area.iter_y().enumerate() {
|
let selected = *cursor == row_index_scrolled;
|
||||||
let i_offset = i + offset;
|
let mut column_x = area.x();
|
||||||
let selected = *cursor == i_offset;
|
if let Some(entry) = paths.get(row_index_scrolled) {
|
||||||
let mut x = area.x();
|
|
||||||
if let Some(entry) = paths.get(i_offset) {
|
|
||||||
for (index, _fragment) in entry.path.iter().enumerate() {
|
for (index, _fragment) in entry.path.iter().enumerate() {
|
||||||
if index == entry.depth - 1 {
|
if index == entry.depth - 1 {
|
||||||
let _cursor = if selected { ">" } else { " " };
|
let _cursor = if selected { ">" } else { " " };
|
||||||
to.area[1] = y;
|
to.area[1] = row_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;
|
|
||||||
}
|
|
||||||
if selected {
|
if selected {
|
||||||
let fill = [area.x(), y, area.w(), 1];
|
Self::row_cursor(to, area.x(), row_y, area.w());
|
||||||
to.fill_fg(fill, Color::Rgb(0, 0, 0));
|
}
|
||||||
to.fill_bg(fill, Color::Rgb(192, 128, 0));
|
self.row_data(to, entry, row_index_scrolled, &mut column_x);
|
||||||
let fill = [area.x() + x as u16, y, w, 1];
|
if selected {
|
||||||
to.fill_bg(fill, Color::Rgb(224, 192, 0));
|
self.col_cursor(to, area.x(), active_x, column_x, row_y, active_w);
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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 struct Column<T> {
|
||||||
pub title: Arc<str>,
|
pub title: Arc<str>,
|
||||||
pub width: usize,
|
pub width: usize,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue