perch/src/view/table.rs
2025-03-23 00:01:22 +02:00

146 lines
5.3 KiB
Rust

use crate::*;
pub struct TreeTable<'a>(pub(crate) &'a Taggart);
impl<'a> Content<TuiOut> for TreeTable<'a> {
fn render (&self, to: &mut TuiOut) {
let (active_x, active_w) = self.0.columns.xw(self.0.column);
let active_w = active_w.saturating_sub(1);
let area = to.area();
Self::column_cursor(to, area, active_x, active_w);
self.rows(to, area, active_x, active_w);
to.area = area;
}
}
impl<'a> TreeTable<'a> {
pub(crate) const BG_COLUMN: Color = Color::Rgb(0, 0, 0);
pub(crate) const FG_ROW: Color = Color::Rgb(0, 0, 0);
pub(crate) const BG_ROW: Color = Color::Rgb(192, 128, 0);
pub(crate) const BG_CELL: Color = Color::Rgb(224, 192, 0);
pub(crate) const BG_EDIT: Color = Color::Rgb(48, 96, 0);
pub(crate) const FG_EDIT: Color = Color::Rgb(255, 255, 255);
fn rows (&self, to: &mut TuiOut, area: [u16;4], active_x: u16, active_w: u16) {
for (row_index, row_y) in area.iter_y().enumerate() {
let row_index_scrolled = row_index + self.0.offset;
let selected = self.0.cursor == row_index_scrolled;
let mut column_x = area.x();
if let Some(entry) = self.0.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] = row_y;
if selected {
Self::row_cursor(to, area.x(), row_y, area.w());
self.cell_cursor(to, area.x(), active_x, row_y, active_w);
}
self.row_data(to, entry, row_index_scrolled, &mut column_x);
}
}
} else {
break
}
}
}
fn column_cursor (to: &mut TuiOut, area: [u16;4], active_x: u16, active_w: u16) {
to.fill_bg([area.x() + active_x, area.y(), active_w, area.h()], Self::BG_COLUMN);
}
fn row_cursor (to: &mut TuiOut, x: u16, y: u16, w: u16) {
let fill = [x, y, w, 1];
to.fill_fg(fill, Self::FG_ROW);
to.fill_bg(fill, Self::BG_ROW);
}
fn cell_cursor (&self, to: &mut TuiOut, xa: u16, x0: u16, y: u16, w: u16) {
let fill = [xa + x0, y, w, 1];
to.fill_bg(fill, Self::BG_CELL);
}
fn row_data (&self, to: &mut TuiOut, entry: &Entry, row: usize, x: &mut u16) {
for (column_index, Column { width, getter, .. }) in self.0.columns.0.iter().enumerate() {
to.area[0] = *x;
if let Some((_edit_index, value)) = self.0.editing.as_ref()
&& self.0.column == column_index
&& self.0.cursor == row
{
to.fill_bg([*x, to.area().y(), *width as u16, 1], Self::BG_EDIT);
to.fill_fg([*x, to.area().y(), *width as u16, 1], Self::FG_EDIT);
Content::render(&TrimStringRef(*width as u16, &value), to);
} else if let Some(value) = getter(entry) {
Content::render(&TrimStringRef(*width as u16, &value), to);
}
*x += *width as u16 + 1;
}
}
}
pub struct Column<T> {
pub title: Arc<str>,
pub width: usize,
pub getter: fn(&T)->Option<Arc<str>>,
pub setter: Option<fn(&mut T, &str)>,
}
impl<T> Column<T> {
pub fn new (
title: &impl AsRef<str>,
width: usize,
getter: fn(&T)->Option<Arc<str>>,
setter: Option<fn(&mut T, &str)>,
) -> Self {
Self {
width,
title: title.as_ref().into(),
getter,
setter,
}
}
}
pub struct Columns<T>(pub Vec<Column<T>>);
impl Default for Columns<Entry> {
fn default () -> Self {
Self(vec![
Column::new(&"HASH", 16, |entry: &Entry|entry.hash(), None),
Column::new(&"FILE", 80, |entry: &Entry|entry.name(), None),
Column::new(&"ARTIST", 30,
|entry: &Entry|entry.artist(),
Some(|entry: &mut Entry, value: &str|entry.set_artist(&value))),
Column::new(&"RELEASE", 30,
|entry: &Entry|entry.album(),
Some(|entry: &mut Entry, value: &str|entry.set_album(&value))),
Column::new(&"TRACK", 5,
|entry: &Entry|entry.track(),
Some(|entry: &mut Entry, value: &str|entry.set_track(&value))),
Column::new(&"TITLE", 80,
|entry: &Entry|entry.title(),
Some(|entry: &mut Entry, value: &str|entry.set_title(&value))),
])
}
}
impl<T> Columns<T> {
pub fn header (&self) -> Arc<str> {
let mut output = String::new();
for Column { width, title, .. } in self.0.iter() {
let cell = title.pad_to_width(*width);
output = format!("{output}{cell}");
}
output.into()
}
pub fn xw (&self, column: usize) -> (u16, u16) {
let mut x: u16 = 0;
for (index, Column { width, .. }) in self.0.iter().enumerate() {
let w = *width as u16 + 1;
if index == column {
if x > 0 {
return (x - 1, w + 1)
} else {
return (x, w)
}
} else {
x += w;
}
}
(0, 0)
}
}