From f9885713ccdfe81033f8e040abeb9720a149047a Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sat, 22 Mar 2025 22:04:11 +0200 Subject: [PATCH] implement setters --- src/model.rs | 28 ++++++++++++++++++++++++++++ src/view/table.rs | 20 ++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/model.rs b/src/model.rs index 293389a..117d332 100644 --- a/src/model.rs +++ b/src/model.rs @@ -128,24 +128,52 @@ impl Entry { _ => None } } + pub fn set_artist (&mut self, value: &impl AsRef ) { + match self.info { + EntryInfo::Music { ref mut artist, .. } => *artist = Some(value.as_ref().into()), + _ => {} + } + } pub fn album (&self) -> Option> { match self.info { EntryInfo::Music { ref album, .. } => album.clone(), _ => None } } + pub fn set_album (&mut self, value: &impl AsRef ) { + match self.info { + EntryInfo::Music { ref mut album, .. } => *album = Some(value.as_ref().into()), + _ => {} + } + } pub fn title (&self) -> Option> { match self.info { EntryInfo::Music { ref title, .. } => title.clone(), _ => None } } + pub fn set_title (&mut self, value: &impl AsRef ) { + match self.info { + EntryInfo::Music { ref mut title, .. } => *title = Some(value.as_ref().into()), + _ => {} + } + } pub fn track (&self) -> Option> { match self.info { EntryInfo::Music { ref track, .. } => track.map(|t|format!("{t}").into()).clone(), _ => None } } + pub fn set_track (&mut self, value: &impl AsRef ) { + match self.info { + EntryInfo::Music { ref mut track, .. } => { + if let Ok(value) = value.as_ref().trim().parse::() { + *track = Some(value) + } + }, + _ => {} + } + } } impl EntryInfo { pub fn new (path: &Path) -> Usually { diff --git a/src/view/table.rs b/src/view/table.rs index 02b1a3e..4e7ae5d 100644 --- a/src/view/table.rs +++ b/src/view/table.rs @@ -76,7 +76,7 @@ pub struct Column { pub title: Arc, pub width: usize, pub getter: fn(&T)->Option>, - pub setter: OptionOption>>, + pub setter: Option, } impl Column { @@ -84,7 +84,7 @@ impl Column { title: &impl AsRef, width: usize, getter: fn(&T)->Option>, - setter: OptionOption>>, + setter: Option, ) -> Self { Self { width, @@ -102,10 +102,18 @@ impl Default for Columns { 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(), 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), + 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))), ]) } }