mirror of
https://codeberg.org/unspeaker/perch.git
synced 2025-12-06 09:36:42 +01:00
move trimmed strings to tengri
This commit is contained in:
parent
c67d3c8803
commit
6e0bf1e4b6
7 changed files with 135 additions and 200 deletions
104
src/view/table.rs
Normal file
104
src/view/table.rs
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
use crate::*;
|
||||
|
||||
pub struct TreeTable<'a>(pub(crate) &'a Taggart);
|
||||
|
||||
impl<'a> Content<TuiOut> for TreeTable<'a> {
|
||||
fn render (&self, to: &mut TuiOut) {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
to.area = area;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Column<T> {
|
||||
pub title: Arc<str>,
|
||||
pub width: usize,
|
||||
pub value: Box<dyn Fn(&T)->Option<Arc<str>> + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<T> Column<T> {
|
||||
pub fn new (
|
||||
title: &impl AsRef<str>,
|
||||
width: usize,
|
||||
value: impl Fn(&T)->Option<Arc<str>> + Send + Sync + 'static
|
||||
) -> Self {
|
||||
Self { width, value: Box::new(value), title: title.as_ref().into() }
|
||||
}
|
||||
}
|
||||
|
||||
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()),
|
||||
Column::new(&"FILE", 80, |entry: &Entry|entry.name()),
|
||||
Column::new(&"ARTIST", 30, |entry: &Entry|entry.artist()),
|
||||
Column::new(&"RELEASE", 30, |entry: &Entry|entry.album()),
|
||||
Column::new(&"TRACK", 5, |entry: &Entry|entry.track()),
|
||||
Column::new(&"TITLE", 80, |entry: &Entry|entry.title()),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue