emit tasks for columns

This commit is contained in:
🪞👃🪞 2025-03-27 19:50:38 +02:00
parent e16e8131aa
commit 59c2fdcbcd
6 changed files with 79 additions and 55 deletions

View file

@ -44,7 +44,7 @@ impl Handle<TuiIn> for Taggart {
press!(Left) => { self.column = self.column.saturating_sub(1); },
press!(Right) => { self.column = self.column + 1; },
press!(Enter) => { self.edit_begin() },
press!(Char(' ')) => { open(&self.entries[self.cursor].path)?; }
press!(Char(' ')) => { open(self.entries[self.cursor].path.as_ref())?; }
press!(Char(']')) => { self.columns.0[self.column].width += 1; }
press!(Char('[')) => { self.columns.0[self.column].width =
self.columns.0[self.column].width.saturating_sub(1).max(5); }

View file

@ -48,26 +48,3 @@ impl Taggart {
})
}
}
pub(crate) fn entries_under (
entries: &mut [Entry], index: usize
) -> Option<Vec<Arc<RwLock<Metadata>>>> {
let path = if let Some(Entry { path, info, .. }) = entries.get(index)
&& let Metadata::Directory { .. } = &*info.read().unwrap()
{
Some(path.clone())
} else {
None
};
if let Some(path) = path {
let mut others = vec![];
for other in entries.iter_mut() {
if other.path.starts_with(&path) && !other.is_directory() {
others.push(other.info.clone());
}
}
Some(others)
} else {
None
}
}

View file

@ -38,14 +38,14 @@ macro_rules! setter {
value: &str
) {
if let Some(entries) = entries_under(&mut state.entries, index) {
for entry in entries.iter() {
if let Some(task) = entry.write().unwrap().$name(&value) {
state.tasks.push(task);
for (path, entry) in entries.into_iter() {
if let Some(item) = entry.write().unwrap().$name(&value) {
state.tasks.push(Task { path, item, });
};
}
} else if let Some(entry) = state.entries.get_mut(index) {
if let Some(task) = entry.$name(&value) {
state.tasks.push(task);
if let Some(item) = entry.$name(&value) {
state.tasks.push(Task { path: entry.path.clone(), item, });
};
}
}
@ -53,8 +53,37 @@ macro_rules! setter {
}}
}
pub(crate) fn entries_under (
entries: &mut [Entry],
index: usize
) -> Option<Vec<(Arc<PathBuf>, Arc<RwLock<Metadata>>)>> {
let path = if let Some(Entry { path, info, .. }) = entries.get(index)
&& let Metadata::Directory { .. } = &*info.read().unwrap()
{
Some(path.clone())
} else {
None
};
if let Some(path) = path {
let mut others = vec![];
for other in entries.iter_mut() {
if other.path.starts_with(path.as_ref()) && !other.is_directory() {
others.push((other.path.clone(), other.info.clone()));
}
}
Some(others)
} else {
None
}
}
pub struct Columns<G, S>(pub Vec<Column<G, S>>);
impl<G, S> Columns<G, S> {
const SCROLL_LEFT: &'static str = "";
const SCROLL_RIGHT: &'static str = "";
}
impl Default for Columns<fn(&Entry)->Option<Arc<str>>, fn(&mut Taggart, usize, &str)> {
fn default () -> Self {
Self(vec![

View file

@ -1,11 +1,15 @@
use crate::*;
use std::cmp::{Eq, PartialEq, Ord, PartialOrd, Ordering};
use lofty::tag::TagItem;
#[derive(Debug)]
pub struct Entry {
pub path: PathBuf,
/// How many levels deep is this from the working directory
pub depth: usize,
pub info: Arc<RwLock<Metadata>>,
/// Full path to this entry
pub path: Arc<PathBuf>,
/// Type-specific metadata
pub info: Arc<RwLock<Metadata>>,
}
impl Entry {
@ -22,7 +26,7 @@ impl Entry {
fn new_dir (_: &impl AsRef<Path>, path: &Path, depth: usize) -> Perhaps<Self> {
Ok(Some(Self {
depth,
path: path.into(),
path: path.to_path_buf().into(),
info: Arc::new(RwLock::new(Metadata::Directory {
hash_file: None,
catalog_file: None,
@ -34,8 +38,8 @@ impl Entry {
fn new_file (_: &impl AsRef<Path>, path: &Path, depth: usize) -> Perhaps<Self> {
Ok(Some(Self {
depth,
path: path.to_path_buf().into(),
info: Arc::new(RwLock::new(Metadata::new(path)?)),
path: path.into(),
}))
}
pub fn is_directory (&self) -> bool {
@ -68,19 +72,19 @@ impl Entry {
pub fn track (&self) -> Option<Arc<str>> {
self.info.read().unwrap().track()
}
pub fn set_artist (&self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_artist (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_artist(value)
}
pub fn set_year (&self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_year (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_year(value)
}
pub fn set_album (&self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_album (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_album(value)
}
pub fn set_title (&self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_title (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_title(value)
}
pub fn set_track (&self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_track (&self, value: &impl AsRef<str>) -> Option<TagItem> {
self.info.write().unwrap().set_track(value)
}
}

View file

@ -1,8 +1,8 @@
use crate::*;
use std::fs::File;
use std::io::{BufReader, Read};
use lofty::{file::TaggedFileExt, probe::Probe, tag::Accessor};
use byte_unit::{Byte, Unit::MB};
use lofty::{file::TaggedFileExt, probe::Probe, tag::{Accessor, TagItem, ItemKey, ItemValue}};
#[derive(Ord, Eq, PartialEq, PartialOrd, Debug)]
pub enum Metadata {
@ -159,56 +159,70 @@ impl Metadata {
_ => None
}
}
pub fn set_artist (&mut self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_artist (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
match self {
Metadata::Music { artist, .. } => {
*artist = Some(value.as_ref().into());
if artist.as_deref() != Some(value.as_ref()) {
*artist = Some(value.as_ref().into());
Some(TagItem::new(
ItemKey::TrackArtist,
ItemValue::Text(value.as_ref().into())
));
}
//todo!("emit task");
None
},
_ => None
}
}
pub fn set_year (&mut self, value: &impl AsRef<str> ) -> Option<Task> {
pub fn set_year (&mut self, value: &impl AsRef<str> ) -> Option<TagItem> {
match self {
Metadata::Music { year, .. } => {
if let Ok(value) = value.as_ref().trim().parse::<u32>() {
*year = Some(value);
}
//todo!("emit task");
None
Some(TagItem::new(
ItemKey::Year,
ItemValue::Text(value.as_ref().into())
))
},
_ => None
}
}
pub fn set_album (&mut self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_album (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
match self {
Metadata::Music { album, .. } => {
*album = Some(value.as_ref().into());
//todo!("emit task");
None
Some(TagItem::new(
ItemKey::AlbumTitle,
ItemValue::Text(value.as_ref().into())
))
},
_ => None
}
}
pub fn set_title (&mut self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_title (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
match self {
Metadata::Music { title, .. } => {
*title = Some(value.as_ref().into());
//todo!("emit task");
None
Some(TagItem::new(
ItemKey::TrackTitle,
ItemValue::Text(value.as_ref().into())
))
},
_ => None
}
}
pub fn set_track (&mut self, value: &impl AsRef<str>) -> Option<Task> {
pub fn set_track (&mut self, value: &impl AsRef<str>) -> Option<TagItem> {
match self {
Metadata::Music { track, .. } => {
if let Ok(value) = value.as_ref().trim().parse::<u32>() {
*track = Some(value);
}
//todo!("emit task");
None
Some(TagItem::new(
ItemKey::TrackNumber,
ItemValue::Text(value.as_ref().into())
))
},
_ => None
}

View file

@ -3,14 +3,14 @@ use lofty::tag::TagItem;
/// An update to a file's metadata to be performed.
pub struct Task {
pub path: PathBuf,
pub path: Arc<PathBuf>,
pub item: TagItem
}
impl Task {
fn new (path: &impl AsRef<Path>, item: TagItem) -> Self {
Self {
path: path.as_ref().into(),
path: path.as_ref().to_path_buf().into(),
item
}
}