add SIZE column and paths_under

This commit is contained in:
🪞👃🪞 2025-03-23 18:14:09 +02:00
parent 1a00ef1ff6
commit 29435bb937
5 changed files with 87 additions and 65 deletions

View file

@ -2,16 +2,17 @@ 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};
pub struct Taggart {
pub _root: PathBuf,
pub paths: Vec<Entry>,
pub cursor: usize,
pub offset: usize,
pub column: usize,
pub columns: Columns<Entry>,
pub size: Measure<TuiOut>,
pub editing: Option<(usize, String)>,
pub _root: PathBuf,
pub paths: Vec<Entry>,
pub cursor: usize,
pub offset: usize,
pub column: usize,
pub columns: Columns<Entry>,
pub display: Measure<TuiOut>,
pub editing: Option<(usize, String)>,
}
#[derive(Ord, Eq, PartialEq, PartialOrd, Debug)]
@ -31,7 +32,7 @@ pub enum EntryInfo {
},
Music {
hash: Arc<str>,
file_type: Option<&'static FileType>,
size: Arc<str>,
artist: Option<Arc<str>>,
album: Option<Arc<str>>,
track: Option<u32>,
@ -46,14 +47,14 @@ pub enum EntryInfo {
},
Image {
hash: Arc<str>,
file_type: Option<&'static FileType>,
size: Arc<str>,
title: Option<String>,
author: Option<String>,
invalid: bool,
},
Unknown {
hash: Arc<str>,
file_type: Option<&'static FileType>,
size: Arc<str>,
}
}
@ -63,8 +64,8 @@ impl Taggart {
_root: root.as_ref().into(),
cursor: 0,
offset: 0,
column: 0,
size: Measure::new(),
column: 2,
display: Measure::new(),
editing: None,
columns: Columns::default(),
paths,
@ -100,59 +101,70 @@ impl<T> Column<T> {
pub struct Columns<T>(pub Vec<Column<T>>);
fn paths_under (entries: &mut [Entry], index: usize) -> Option<Vec<&mut Entry>> {
let path = if let Some(Entry {
path, info: EntryInfo::Directory { .. }, ..
}) = entries.get(index) {
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_dir() {
others.push(other);
}
}
Some(others)
} else {
None
}
}
impl Default for Columns<Entry> {
fn default () -> Self {
Self(vec![
Column::new(&"HASH", 16, |entry: &Entry|entry.hash()),
Column::new(&"SIZE", 8, |entry: &Entry|entry.size()),
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)
}
if let Some(entries) = paths_under(entries, index) {
todo!("set artist for whole directory {:#?}", entries);
} else if let Some(entry) = entries.get_mut(index) {
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)
}
if let Some(entries) = paths_under(entries, index) {
todo!("set album for whole directory {:#?}", entries);
} else if let Some(entry) = entries.get_mut(index) {
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)
}
if let Some(entries) = paths_under(entries, index) {
todo!("set track for whole directory {:#?}", entries);
} else if let Some(entry) = entries.get_mut(index) {
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)
}
if let Some(entries) = paths_under(entries, index) {
todo!("set title for whole directory {:#?}", entries);
} else if let Some(entry) = entries.get_mut(index) {
entry.set_title(&value)
}
}),
@ -206,6 +218,13 @@ impl Entry {
_ => None
}
}
pub fn size (&self) -> Option<Arc<str>> {
match self.info {
EntryInfo::Image { ref size, .. } => Some(size.clone()),
EntryInfo::Music { ref size, .. } => Some(size.clone()),
_ => None
}
}
pub fn artist (&self) -> Option<Arc<str>> {
match self.info {
EntryInfo::Music { ref artist, .. } => artist.clone(),
@ -269,10 +288,12 @@ impl EntryInfo {
if probe.file_type().is_some() {
let file = lofty::read_from_path(path)?;
let tag = file.primary_tag();
let hash = hex::encode(xxh3_64(std::fs::read(path)?.as_slice()).to_be_bytes()).into();
let data = std::fs::read(path)?;
let hash = hex::encode(xxh3_64(data.as_slice()).to_be_bytes()).into();
let size = Byte::from_u64(data.len() as u64).get_adjusted_unit(MB);
Ok(Self::Music {
hash,
file_type: None,
size: format!("{:#>8.2}", size).into(),
artist: tag.map(|t|t.artist().map(|t|t.into())).flatten(),
album: tag.map(|t|t.album().map(|t|t.into())).flatten(),
track: tag.map(|t|t.track().map(|t|t.into())).flatten(),
@ -290,7 +311,9 @@ impl EntryInfo {
}
}
pub fn new_fallback (path: &Path) -> Usually<Self> {
let mut reader = BufReader::new(File::open(path)?);
let file = File::open(path)?;
let size = Byte::from_u64(file.metadata()?.len() as u64).get_adjusted_unit(MB);
let mut reader = BufReader::new(file);
let mut bytes = vec![0;16];
reader.read(&mut bytes)?;
// PNG
@ -298,11 +321,11 @@ impl EntryInfo {
let mut bytes = vec![];
BufReader::new(File::open(path)?).read(&mut bytes)?;
return Ok(Self::Image {
file_type: None,
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
title: None,
author: None,
invalid: false,
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
size: format!("{:#>8.2}", size).into(),
title: None,
author: None,
invalid: false,
})
}
// JPG
@ -317,16 +340,16 @@ impl EntryInfo {
bytes.get(10) == Some(&0x00) && bytes.get(11) == Some(&0x00))
{
return Ok(Self::Image {
file_type: None,
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
title: None,
author: None,
invalid: false,
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
size: format!("{:#>8.2}", size).into(),
title: None,
author: None,
invalid: false,
})
}
Ok(Self::Unknown {
file_type: None,
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
size: format!("{:#>8.2}", size).into(),
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
})
}
}