diff --git a/src/keys.rs b/src/keys.rs index fa07d5a..b7f7d52 100644 --- a/src/keys.rs +++ b/src/keys.rs @@ -84,9 +84,9 @@ impl Taggart { if let Some((_edit_index, value)) = &self.editing && let Some(column) = self.columns.0.get(self.column) && let Some(setter) = &column.setter - && let Some(entry) = self.paths.get_mut(self.cursor) + && self.paths.get_mut(self.cursor).is_some() { - setter(entry, value) + setter(self.paths.as_mut_slice(), self.cursor, value) } self.editing = None; } diff --git a/src/model.rs b/src/model.rs index 9d53010..21caaec 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,11 +1,7 @@ use crate::*; use std::fs::File; use std::io::{BufReader, Read}; -use lofty::{ - file::TaggedFileExt, - probe::Probe, - tag::Accessor, -}; +use lofty::{file::TaggedFileExt, probe::Probe, tag::Accessor}; pub struct Taggart { pub _root: PathBuf, @@ -76,6 +72,94 @@ impl Taggart { } } +pub struct Column { + pub title: Arc, + pub width: usize, + pub getter: fn(&T)->Option>, + pub setter: Option, +} + +impl Column { + pub fn new ( + title: &impl AsRef, + width: usize, + getter: fn(&T)->Option>, + ) -> Self { + Self { + width, + title: title.as_ref().into(), + getter, + setter: None, + } + } + fn setter (mut self, setter: fn(&mut [T], usize, &str)) -> Self { + self.setter = Some(setter); + self + } +} + +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()) + .setter(|entries: &mut [Entry], index: usize, value: &str|{ + if let Some(entry) = entries.get_mut(index) { + match entry.info { + EntryInfo::Directory { .. } => { + todo!("set artist for whole directory") + }, + _ => entry.set_artist(&value) + } + } + }), + + Column::new(&"RELEASE", 30, |entry: &Entry|entry.album()) + .setter(|entries: &mut [Entry], index: usize, value: &str|{ + if let Some(entry) = entries.get_mut(index) { + match entry.info { + EntryInfo::Directory { .. } => { + todo!("set album for whole directory") + }, + _ => entry.set_album(&value) + } + } + }), + + Column::new(&"TRACK", 5, |entry: &Entry|entry.track()) + .setter(|entries: &mut [Entry], index: usize, value: &str|{ + if let Some(entry) = entries.get_mut(index) { + match entry.info { + EntryInfo::Directory { .. } => { + todo!("set title for whole directory") + }, + _ => entry.set_track(&value) + } + } + }), + + Column::new(&"TITLE", 80, |entry: &Entry|entry.title()) + .setter(|entries: &mut [Entry], index: usize, value: &str|{ + if let Some(entry) = entries.get_mut(index) { + match entry.info { + EntryInfo::Directory { .. } => { + todo!("set track for whole directory") + }, + _ => entry.set_title(&value) + } + } + }), + + ]) + } +} + impl Entry { pub fn new (root: &impl AsRef, path: &impl AsRef, depth: usize) -> Perhaps { let path = path.as_ref(); @@ -148,21 +232,18 @@ impl Entry { } pub fn set_artist (&mut self, value: &impl AsRef ) { match self.info { - EntryInfo::Directory { .. } => todo!("set artist for whole directory"), EntryInfo::Music { ref mut artist, .. } => *artist = Some(value.as_ref().into()), _ => {} } } pub fn set_album (&mut self, value: &impl AsRef ) { match self.info { - EntryInfo::Directory { .. } => todo!("set album for whole directory"), EntryInfo::Music { ref mut album, .. } => *album = Some(value.as_ref().into()), _ => {} } } pub fn set_title (&mut self, value: &impl AsRef ) { match self.info { - EntryInfo::Directory { .. } => todo!("set title for whole directory"), EntryInfo::Music { ref mut title, .. } => *title = Some(value.as_ref().into()), _ => {} } diff --git a/src/view/table.rs b/src/view/table.rs index c24735e..b5a6a88 100644 --- a/src/view/table.rs +++ b/src/view/table.rs @@ -72,52 +72,6 @@ impl<'a> TreeTable<'a> { } } -pub struct Column { - pub title: Arc, - pub width: usize, - pub getter: fn(&T)->Option>, - pub setter: Option, -} - -impl Column { - pub fn new ( - title: &impl AsRef, - width: usize, - getter: fn(&T)->Option>, - setter: Option, - ) -> Self { - Self { - width, - title: title.as_ref().into(), - getter, - setter, - } - } -} - -pub struct Columns(pub Vec>); - -impl Default for Columns { - 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 Columns { pub fn header (&self) -> Arc { let mut output = String::new();