add optional column setters

This commit is contained in:
🪞👃🪞 2025-03-22 21:01:33 +02:00
parent c3fbc6abf1
commit 138bba99cb
2 changed files with 23 additions and 16 deletions

View file

@ -75,7 +75,7 @@ impl Taggart {
})
}
pub fn edit_begin (&mut self) {
let value = (self.columns.0[self.column].value)(&self.paths[self.cursor]);
let value = (self.columns.0[self.column].getter)(&self.paths[self.cursor]);
let value = format!("{}", value.unwrap_or_default());
self.editing = Some((value.len(), value));
}

View file

@ -59,7 +59,7 @@ impl<'a> TreeTable<'a> {
//}
}
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() {
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
@ -68,7 +68,7 @@ impl<'a> TreeTable<'a> {
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) = value(entry) {
} else if let Some(value) = getter(entry) {
Content::render(&TrimStringRef(*width as u16, &value), to);
}
*x += *width as u16 + 1;
@ -77,18 +77,25 @@ impl<'a> TreeTable<'a> {
}
pub struct Column<T> {
pub title: Arc<str>,
pub width: usize,
pub value: Box<dyn Fn(&T)->Option<Arc<str>> + Send + Sync>,
pub title: Arc<str>,
pub width: usize,
pub getter: fn(&T)->Option<Arc<str>>,
pub setter: Option<fn(&T)->Option<Arc<str>>>,
}
impl<T> Column<T> {
pub fn new (
title: &impl AsRef<str>,
width: usize,
value: impl Fn(&T)->Option<Arc<str>> + Send + Sync + 'static
title: &impl AsRef<str>,
width: usize,
getter: fn(&T)->Option<Arc<str>>,
setter: Option<fn(&T)->Option<Arc<str>>>,
) -> Self {
Self { width, value: Box::new(value), title: title.as_ref().into() }
Self {
width,
title: title.as_ref().into(),
getter,
setter,
}
}
}
@ -97,12 +104,12 @@ 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()),
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(), None),
Column::new(&"RELEASE", 30, |entry: &Entry|entry.album(), None),
Column::new(&"TRACK", 5, |entry: &Entry|entry.track(), None),
Column::new(&"TITLE", 80, |entry: &Entry|entry.title(), None),
])
}
}