allow for multisetters

This commit is contained in:
🪞👃🪞 2025-03-23 17:23:07 +02:00
parent 13600a5837
commit 1a00ef1ff6
3 changed files with 91 additions and 56 deletions

View file

@ -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;
}

View file

@ -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<T> {
pub title: Arc<str>,
pub width: usize,
pub getter: fn(&T)->Option<Arc<str>>,
pub setter: Option<fn(&mut [T], usize, &str)>,
}
impl<T> Column<T> {
pub fn new (
title: &impl AsRef<str>,
width: usize,
getter: fn(&T)->Option<Arc<str>>,
) -> 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<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())
.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>, path: &impl AsRef<Path>, depth: usize) -> Perhaps<Self> {
let path = path.as_ref();
@ -148,21 +232,18 @@ impl Entry {
}
pub fn set_artist (&mut self, value: &impl AsRef<str> ) {
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<str> ) {
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<str> ) {
match self.info {
EntryInfo::Directory { .. } => todo!("set title for whole directory"),
EntryInfo::Music { ref mut title, .. } => *title = Some(value.as_ref().into()),
_ => {}
}

View file

@ -72,52 +72,6 @@ impl<'a> TreeTable<'a> {
}
}
pub struct Column<T> {
pub title: Arc<str>,
pub width: usize,
pub getter: fn(&T)->Option<Arc<str>>,
pub setter: Option<fn(&mut T, &str)>,
}
impl<T> Column<T> {
pub fn new (
title: &impl AsRef<str>,
width: usize,
getter: fn(&T)->Option<Arc<str>>,
setter: Option<fn(&mut T, &str)>,
) -> Self {
Self {
width,
title: title.as_ref().into(),
getter,
setter,
}
}
}
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(), 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<T> Columns<T> {
pub fn header (&self) -> Arc<str> {
let mut output = String::new();