mirror of
https://codeberg.org/unspeaker/perch.git
synced 2025-12-06 09:36:42 +01:00
add SIZE column and paths_under
This commit is contained in:
parent
1a00ef1ff6
commit
29435bb937
5 changed files with 87 additions and 65 deletions
9
Justfile
9
Justfile
|
|
@ -1,3 +1,8 @@
|
|||
run:
|
||||
cargo run -- -j16 ~/Music
|
||||
build-release:
|
||||
time cargo build -j4 --release
|
||||
|
||||
covfig := "CARGO_INCREMENTAL=0 RUSTFLAGS='-Cinstrument-coverage' RUSTDOCFLAGS='-Cinstrument-coverage' LLVM_PROFILE_FILE='cov/cargo-test-%p-%m.profraw'"
|
||||
grcov-binary := "--binary-path ./target/coverage/deps/"
|
||||
grcov-ignore := "--ignore-not-existing --ignore '../*' --ignore \"/*\" --ignore 'target/*'"
|
||||
|
|
@ -14,7 +19,3 @@ cov-md-ci:
|
|||
doc:
|
||||
cargo doc
|
||||
|
||||
build-release:
|
||||
time cargo build -j4 --release
|
||||
run:
|
||||
cargo run -- -j16 ~/Music
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ macro_rules! press {
|
|||
impl Handle<TuiIn> for Taggart {
|
||||
fn handle (&mut self, input: &TuiIn) -> Perhaps<bool> {
|
||||
let x_min = self.offset;
|
||||
let x_max = self.offset + self.size.h().saturating_sub(1);
|
||||
let x_max = self.offset + self.display.h().saturating_sub(1);
|
||||
let event = &*input.event();
|
||||
match &self.editing {
|
||||
None => match event {
|
||||
|
|
|
|||
|
|
@ -15,8 +15,6 @@ use crate::crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventSt
|
|||
use clap::{arg, command, value_parser};
|
||||
use walkdir::WalkDir;
|
||||
use xxhash_rust::xxh3::xxh3_64;
|
||||
use file_type::FileType;
|
||||
|
||||
mod keys;
|
||||
mod view; use self::view::*;
|
||||
mod model; pub(crate) use self::model::*;
|
||||
|
|
|
|||
103
src/model.rs
103
src/model.rs
|
|
@ -2,6 +2,7 @@ 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,
|
||||
|
|
@ -10,7 +11,7 @@ pub struct Taggart {
|
|||
pub offset: usize,
|
||||
pub column: usize,
|
||||
pub columns: Columns<Entry>,
|
||||
pub size: Measure<TuiOut>,
|
||||
pub display: Measure<TuiOut>,
|
||||
pub editing: Option<(usize, String)>,
|
||||
}
|
||||
|
||||
|
|
@ -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,8 +321,8 @@ 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(),
|
||||
size: format!("{:#>8.2}", size).into(),
|
||||
title: None,
|
||||
author: None,
|
||||
invalid: false,
|
||||
|
|
@ -317,15 +340,15 @@ 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(),
|
||||
size: format!("{:#>8.2}", size).into(),
|
||||
title: None,
|
||||
author: None,
|
||||
invalid: false,
|
||||
})
|
||||
}
|
||||
Ok(Self::Unknown {
|
||||
file_type: None,
|
||||
size: format!("{:#>8.2}", size).into(),
|
||||
hash: hex::encode(xxh3_64(&bytes).to_be_bytes()).into(),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,8 @@ impl Taggart {
|
|||
|
||||
impl Content<TuiOut> for Taggart {
|
||||
fn content (&self) -> impl Render<TuiOut> {
|
||||
let sizer = Fill::xy(&self.size);
|
||||
let size = format!("{}x{}", self.size.w(), self.size.h());
|
||||
let sizer = Fill::xy(&self.display);
|
||||
let size = format!("{}x{}", self.display.w(), self.display.h());
|
||||
let titlebar = status_bar(Align::w(self.columns.header()));
|
||||
let size_bar = status_bar(Fill::x(Bsp::a(
|
||||
Fill::x(Align::w(Tui::bold(true, if self.editing.is_some() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue