From 138bba99cbe95600b688fb33ee005552c853a00f Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 22 Mar 2025 21:01:33 +0200 Subject: [PATCH] add optional column setters --- src/model.rs | 2 +- src/view/table.rs | 37 ++++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/model.rs b/src/model.rs index fd93e6a..5e7aa57 100644 --- a/src/model.rs +++ b/src/model.rs @@ -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)); } diff --git a/src/view/table.rs b/src/view/table.rs index 51d86eb..263c044 100644 --- a/src/view/table.rs +++ b/src/view/table.rs @@ -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 { - pub title: Arc, - pub width: usize, - pub value: BoxOption> + Send + Sync>, + pub title: Arc, + pub width: usize, + pub getter: fn(&T)->Option>, + pub setter: OptionOption>>, } impl Column { pub fn new ( - title: &impl AsRef, - width: usize, - value: impl Fn(&T)->Option> + Send + Sync + 'static + title: &impl AsRef, + width: usize, + getter: fn(&T)->Option>, + setter: OptionOption>>, ) -> 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(pub Vec>); impl Default for Columns { 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), ]) } }