dynamic columns; macro keydefs

This commit is contained in:
🪞👃🪞 2025-03-09 04:11:07 +02:00
parent b29511c23e
commit 8cc9418272
6 changed files with 113 additions and 147 deletions

View file

@ -3,17 +3,6 @@ use walkdir::DirEntry;
use id3::{Tag, TagLike};
use std::io::Read;
pub struct Taggart {
pub root: PathBuf,
pub paths: Vec<Entry>,
pub cursor: usize,
pub offset: usize,
pub column: usize,
pub size: Measure<TuiOut>,
pub editing: Option<(usize, usize)>,
pub show_hash: bool,
}
#[derive(Ord, Eq, PartialEq, PartialOrd)]
pub struct Entry {
pub path: PathBuf,
@ -54,7 +43,6 @@ pub enum EntryInfo {
}
impl Entry {
pub fn new (root: &impl AsRef<Path>, entry: &DirEntry) -> Perhaps<Self> {
if entry.path().is_dir() {
Self::new_dir(root, entry)
@ -64,11 +52,10 @@ impl Entry {
Ok(None)
}
}
fn new_dir (root: &impl AsRef<Path>, entry: &DirEntry) -> Perhaps<Self> {
Ok(Some(Self {
depth: entry.depth(),
path: entry.path().into(),
path: entry.path().strip_prefix(root.as_ref())?.into(),
info: EntryInfo::Directory {
hash_file: None,
catalog_file: None,
@ -77,7 +64,6 @@ impl Entry {
},
}))
}
fn new_file (root: &impl AsRef<Path>, entry: &DirEntry) -> Perhaps<Self> {
let bytes = read(entry.path())?;
let hash = hex::encode(xxh3_64(&bytes).to_be_bytes());
@ -85,7 +71,7 @@ impl Entry {
let mime_type = file_type.media_types().get(0);
return Ok(Some(Self {
depth: entry.depth(),
path: entry.path().into(),
path: entry.path().strip_prefix(root.as_ref())?.into(),
info: match mime_type {
Some(&"audio/mpeg3") => {
let id3 = Tag::read_from_path(entry.path())?;
@ -123,23 +109,18 @@ impl Entry {
},
}))
}
pub fn short_path (&self, root: &impl AsRef<Path>) -> Usually<&Path> {
Ok(self.path.strip_prefix(root.as_ref())?)
}
pub fn is_dir (&self) -> bool {
matches!(self.info, EntryInfo::Directory { .. })
}
pub fn is_mus (&self) -> bool {
matches!(self.info, EntryInfo::Music { .. })
}
pub fn is_img (&self) -> bool {
matches!(self.info, EntryInfo::Image { .. })
}
pub fn hash (&self) -> Option<Arc<str>> {
match self.info {
EntryInfo::Image { ref hash, .. } => Some(hash.clone()),
@ -165,4 +146,10 @@ impl Entry {
_ => None
}
}
pub fn track (&self) -> Option<Arc<str>> {
match self.info {
EntryInfo::Music { ref track, .. } => track.map(|t|format!("{t}").into()).clone(),
_ => None
}
}
}