read id3 tags

This commit is contained in:
🪞👃🪞 2025-03-07 23:49:08 +02:00
parent 72bd6148d6
commit b29511c23e
6 changed files with 251 additions and 272 deletions

View file

@ -10,15 +10,14 @@ use tek_tui::tek_output::*;
use tek_tui::tek_input::*;
use crate::crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventState, KeyEventKind};
use clap::{arg, command, value_parser, ArgAction, Command};
use clap::{arg, command, value_parser};
use walkdir::WalkDir;
use sha2::{Sha256, Digest};
use xxhash_rust::xxh3::xxh3_64;
use base64::prelude::*;
use file_type::FileType;
mod keys;
mod view;
mod model; pub(crate) use self::model::*;
pub(crate) type Usually<T> = std::result::Result<T, Box<dyn std::error::Error>>;
pub(crate) type Perhaps<T> = Usually<Option<T>>;
@ -31,49 +30,6 @@ fn cli () -> clap::Command {
command!()
.arg(arg!([path] "Path to root directory").value_parser(value_parser!(PathBuf)))
}
struct Taggart {
root: PathBuf,
paths: Vec<Entry>,
cursor: usize,
offset: usize,
column: usize,
size: Measure<TuiOut>,
editing: Option<(usize, usize)>,
show_hash: bool,
}
#[derive(Default, Ord, Eq, PartialEq, PartialOrd)]
struct Entry {
path: PathBuf,
is_dir: Option<DirInfo>,
is_mus: Option<MusInfo>,
is_img: Option<ImgInfo>,
depth: usize,
hash: Option<String>,
file_type: Option<&'static FileType>,
}
#[derive(Default, Ord, Eq, PartialEq, PartialOrd)]
struct DirInfo {
hash_file: Option<()>,
catalog_file: Option<()>,
artist_file: Option<()>,
release_file: Option<()>,
}
#[derive(Default, Ord, Eq, PartialEq, PartialOrd)]
struct MusInfo {
artist: Option<String>,
release: Option<String>,
track: Option<usize>,
title: Option<String>,
date: Option<String>,
year: Option<String>,
people: Option<Vec<String>>,
publisher: Option<String>,
key: Option<String>,
}
#[derive(Default, Ord, Eq, PartialEq, PartialOrd)]
struct ImgInfo {
author: Option<String>,
}
fn main () -> Usually<()> {
let args = cli().get_matches();
@ -109,78 +65,11 @@ impl Taggart {
if entry.depth() == 0 {
continue
}
let depth = entry.depth();
let path = entry.into_path();
let short_path: PathBuf = path.strip_prefix(&root)?.into();
let (is_dir, is_mus, is_img, hash, file_type) = if path.is_dir() {
(Some(Default::default()), None, None, None, None)
} else {
let bytes = read(&path)?;
let hash = hex::encode(xxh3_64(&bytes).to_be_bytes());
let file_type = FileType::try_from_reader(&*bytes)?;
let mime_type = file_type.media_types().get(0);
let is_mus = match mime_type {
Some(&"audio/mpeg3") => Some(Default::default()),
_ => None,
};
let is_img = match mime_type {
Some(&"image/png") => Some(Default::default()),
_ => None,
};
println!("{hash} {:>10}b {}", bytes.len(), short_path.display());
(None, is_mus, is_img, Some(hash), Some(file_type))
};
paths.push(Entry {
path: short_path,
is_dir,
is_mus,
is_img,
depth,
hash,
file_type
});
if let Some(entry) = Entry::new(root, &entry)? {
paths.push(entry);
}
}
paths.sort();
Ok(paths)
}
}
//pub enum Entry {
//Dir {
//path: PathBuf,
//name: OsString,
//entries: Vec<Box<FileTree>>,
//},
//File {
//path: PathBuf,
//name: OsString,
//}
//}
//impl Entry {
//fn new (path: &impl AsRef<Path>) -> Usually<Self> {
//let mut paths = vec![];
//for entry in WalkDir::new(&root)
//.into_iter()
//.filter_entry(|e|!e
//.file_name()
//.to_str()
//.map(|s|s.starts_with("."))
//.unwrap_or(false))
//{
//let path = entry?.into_path().strip_prefix(&root)?.into();
//paths.push(path);
//}
//paths.sort();
//}
//}
//struct FileTree {
//path: PathBuf,
//name: OsString,
//entries: Vec<Box<FileTree>>,
//}
//impl FileTree {
//}

168
src/model.rs Normal file
View file

@ -0,0 +1,168 @@
use crate::*;
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,
pub depth: usize,
pub info: EntryInfo,
}
#[derive(Ord, Eq, PartialEq, PartialOrd)]
pub enum EntryInfo {
Directory {
hash_file: Option<()>,
catalog_file: Option<()>,
artist_file: Option<()>,
release_file: Option<()>,
},
Music {
hash: Arc<str>,
file_type: &'static FileType,
artist: Option<Arc<str>>,
album: Option<Arc<str>>,
track: Option<u32>,
title: Option<Arc<str>>,
date: Option<Arc<str>>,
year: Option<i32>,
people: Option<Vec<Arc<str>>>,
publisher: Option<Arc<str>>,
key: Option<Arc<str>>,
bpm: Option<Arc<str>>,
invalid: bool,
},
Image {
hash: Arc<str>,
file_type: &'static FileType,
title: Option<String>,
author: Option<String>,
invalid: bool,
},
}
impl Entry {
pub fn new (root: &impl AsRef<Path>, entry: &DirEntry) -> Perhaps<Self> {
if entry.path().is_dir() {
Self::new_dir(root, entry)
} else if entry.path().is_file() {
Self::new_file(root, entry)
} else {
Ok(None)
}
}
fn new_dir (root: &impl AsRef<Path>, entry: &DirEntry) -> Perhaps<Self> {
Ok(Some(Self {
depth: entry.depth(),
path: entry.path().into(),
info: EntryInfo::Directory {
hash_file: None,
catalog_file: None,
artist_file: None,
release_file: None,
},
}))
}
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());
let file_type = FileType::try_from_reader(&*bytes)?;
let mime_type = file_type.media_types().get(0);
return Ok(Some(Self {
depth: entry.depth(),
path: entry.path().into(),
info: match mime_type {
Some(&"audio/mpeg3") => {
let id3 = Tag::read_from_path(entry.path())?;
EntryInfo::Music {
file_type,
hash: hash.into(),
artist: id3.artist().map(|x|x.into()),
album: id3.album().map(|x|x.into()),
track: id3.track().map(|x|x.into()),
title: id3.title().map(|x|x.into()),
date: None,
year: id3.year().map(|x|x.into()),
people: None,
publisher: None,
key: None,
bpm: None,
invalid: false,
}
},
Some(&"image/png") => EntryInfo::Image {
file_type,
hash: hash.into(),
title: None,
author: None,
invalid: false,
},
Some(&"image/jpeg") => EntryInfo::Image {
file_type,
hash: hash.into(),
title: None,
author: None,
invalid: false,
},
_ => return Ok(None)
},
}))
}
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()),
EntryInfo::Music { ref hash, .. } => Some(hash.clone()),
_ => None
}
}
pub fn artist (&self) -> Option<Arc<str>> {
match self.info {
EntryInfo::Music { ref artist, .. } => artist.clone(),
_ => None
}
}
pub fn album (&self) -> Option<Arc<str>> {
match self.info {
EntryInfo::Music { ref album, .. } => album.clone(),
_ => None
}
}
pub fn title (&self) -> Option<Arc<str>> {
match self.info {
EntryInfo::Music { ref title, .. } => title.clone(),
_ => None
}
}
}

View file

@ -1,22 +1,7 @@
use crate::*;
use tek_tui::ratatui::{style::{Color, Style}, prelude::Stylize};
use pad::PadStr;
fn table_row (
hash: Option<&str>, label: &str, artist: &str, album: &str, track: &str, title: &str
) -> String {
let hash = hash.unwrap_or("").pad_to_width(COLUMN_WIDTHS[0] as usize);
let label = label.pad_to_width(COLUMN_WIDTHS[1] as usize);
let artist = artist.pad_to_width(COLUMN_WIDTHS[2] as usize);
let album = album.pad_to_width(COLUMN_WIDTHS[3] as usize);
let track = track.pad_to_width(COLUMN_WIDTHS[4] as usize);
let title = title.pad_to_width(COLUMN_WIDTHS[5] as usize);
format!("{hash}{label}{artist}{album}{track}{title}")
}
fn status_bar (content: impl Content<TuiOut>) -> impl Content<TuiOut> {
Fixed::y(1, Fill::x(Tui::bold(true, Tui::fg_bg(Color::Rgb(0,0,0), Color::Rgb(255,255,255), content))))
}
use std::fmt::Display;
impl Content<TuiOut> for Taggart {
fn content (&self) -> impl Render<TuiOut> {
@ -24,7 +9,12 @@ impl Content<TuiOut> for Taggart {
let size = format!("{}x{}", self.size.w(), self.size.h());
let size_bar = status_bar(Align::e(size));
let titlebar = status_bar(Align::w(table_row(
Some("HASH"), "FILE", "ARTIST", "RELEASE", "TRACK", "TITLE"
Some("HASH".into()),
"FILE",
Some("ARTIST".into()),
Some("RELEASE".into()),
"TRACK",
Some("TITLE".into())
)));
let table = Fill::xy(TreeTable(self));
Bsp::n(size_bar, Bsp::s(titlebar, Bsp::b(sizer, table)))
@ -38,7 +28,7 @@ impl<'a> Content<TuiOut> for TreeTable<'a> {
let area = to.area();
let Taggart { offset, paths, cursor, column, .. } = self.0;
let mut x = 0;
for (index, width) in COLUMN_WIDTHS.iter().enumerate() {
for (index, _width) in COLUMN_WIDTHS.iter().enumerate() {
let w = COLUMN_WIDTHS[index] + 1;
if index == *column {
to.fill_bg([area.x() + x, area.y(), w, area.h()], Color::Rgb(0, 0, 0));
@ -54,30 +44,9 @@ impl<'a> Content<TuiOut> for TreeTable<'a> {
for (index, fragment) in entry.path.iter().enumerate() {
if index == entry.depth - 1 {
let cursor = if selected { ">" } else { " " };
let icon = if entry.is_dir.is_some() {
"" //"+"
} else if entry.is_img.is_some() {
""
} else if entry.is_mus.is_some() {
""
} else {
" "
};
let style = if entry.is_dir.is_some() {
None
} else {
Some(Style::default().bold())
};
let name = fragment.display();
let indent = "".pad_to_width((entry.depth - 1) * 2);
let label = table_row(
entry.hash.as_deref(),
&format!("{indent}{icon} {name}"),
"",
"",
"",
""
);
let icon = entry.icon();
let style = entry.style();
let label = entry.label(icon, &fragment.display());
to.blit(&label, area.x(), y, style);
if selected {
let fill = [area.x(), y, area.w(), 1];
@ -92,3 +61,56 @@ impl<'a> Content<TuiOut> for TreeTable<'a> {
}
}
}
impl Entry {
fn icon (&self) -> &'static str {
if self.is_dir() {
"" //"+"
} else if self.is_img() {
""
} else if self.is_mus() {
""
} else {
" "
}
}
fn style (&self) -> Option<Style> {
if self.is_dir() {
None
} else {
Some(Style::default().bold())
}
}
fn label (&self, icon: &str, name: &impl Display) -> String {
let indent = "".pad_to_width((self.depth - 1) * 2);
table_row(
self.hash(),
&format!("{indent}{icon} {name}"),
self.artist(),
self.album(),
"",
self.title()
)
}
}
fn table_row (
hash: Option<Arc<str>>,
label: &str,
artist: Option<Arc<str>>,
album: Option<Arc<str>>,
track: &str,
title: Option<Arc<str>>,
) -> String {
let hash = hash.unwrap_or_default().pad_to_width(COLUMN_WIDTHS[0] as usize);
let label = label.pad_to_width(COLUMN_WIDTHS[1] as usize);
let artist = artist.unwrap_or_default().pad_to_width(COLUMN_WIDTHS[2] as usize);
let album = album.unwrap_or_default().pad_to_width(COLUMN_WIDTHS[3] as usize);
let track = track.pad_to_width(COLUMN_WIDTHS[4] as usize);
let title = title.unwrap_or_default().pad_to_width(COLUMN_WIDTHS[5] as usize);
format!("{hash}{label}{artist}{album}{track}{title}")
}
fn status_bar (content: impl Content<TuiOut>) -> impl Content<TuiOut> {
Fixed::y(1, Fill::x(Tui::bold(true, Tui::fg_bg(Color::Rgb(0,0,0), Color::Rgb(255,255,255), content))))
}