collect hashes and file types

This commit is contained in:
🪞👃🪞 2025-03-03 21:05:18 +02:00
parent 01bc1b7b47
commit 747abab922
5 changed files with 183 additions and 31 deletions

View file

@ -53,6 +53,30 @@ impl Handle<TuiIn> for Taggart {
}) => {
self.column = self.column + 1;
},
Event::Key(KeyEvent {
code: KeyCode::Enter,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
state: KeyEventState::NONE
}) => {
self.editing = Some((self.cursor, self.column));
},
Event::Key(KeyEvent {
code: KeyCode::Esc,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
state: KeyEventState::NONE
}) => {
self.editing = None;
},
Event::Key(KeyEvent {
code: KeyCode::Tab,
kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE,
state: KeyEventState::NONE
}) => {
self.show_hash = !self.show_hash;
},
_ => {}
}
if self.cursor < x_min {

View file

@ -1,14 +1,21 @@
#![feature(os_str_display)]
use tek_tui::*;
use tek_tui::tek_input::*;
use tek_tui::tek_output::*;
use crate::crossterm::event::{Event, KeyEvent, KeyCode, KeyModifiers, KeyEventState, KeyEventKind};
use clap::{arg, command, value_parser, ArgAction, Command};
use walkdir::WalkDir;
use std::sync::{Arc, RwLock};
use std::path::{Path, PathBuf};
use std::env::current_dir;
use std::fs::read;
use tek_tui::*;
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 walkdir::WalkDir;
use sha2::{Sha256, Digest};
use xxhash_rust::xxh3::xxh3_64;
use base64::prelude::*;
use file_type::FileType;
mod keys;
mod view;
@ -25,20 +32,24 @@ fn cli () -> clap::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>,
root: PathBuf,
paths: Vec<Entry>,
cursor: usize,
offset: usize,
column: usize,
size: Measure<TuiOut>,
editing: Option<(usize, usize)>,
show_hash: bool,
}
#[derive(Ord, Eq, PartialEq, PartialOrd, Default)]
struct Entry {
path: PathBuf,
is_dir: bool,
is_mus: bool,
is_img: bool,
depth: usize,
path: PathBuf,
is_dir: bool,
is_mus: bool,
is_img: bool,
depth: usize,
hash: Option<String>,
file_type: Option<&'static FileType>,
}
fn main () -> Usually<()> {
@ -55,6 +66,18 @@ impl Taggart {
} else {
current_dir()?
};
Ok(Self {
paths: Self::collect(&root)?,
root,
cursor: 0,
offset: 0,
column: 0,
size: Measure::new(),
editing: None,
show_hash: false,
})
}
fn collect (root: &impl AsRef<Path>) -> Usually<Vec<Entry>> {
let mut paths = vec![];
for entry in WalkDir::new(&root)
.into_iter()
@ -70,28 +93,36 @@ impl Taggart {
}
let depth = entry.depth();
let path = entry.into_path();
let (is_dir, is_mus, is_img) = if path.is_dir() {
(true, false, false)
let (is_dir, is_mus, is_img, hash, file_type) = if path.is_dir() {
(true, false, false, None, None)
} else {
(false, false, false)
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") => true,
_ => false,
};
let is_img = match mime_type {
Some(&"image/png") => true,
_ => false,
};
println!("{hash} {file_type:?} ({}b)\n{}\n", bytes.len(), path.display());
(false, is_mus, is_img, Some(hash), Some(file_type))
};
paths.push(Entry {
path: path.strip_prefix(&root)?.into(),
is_dir: path.is_dir(),
is_mus: false,
is_img: false,
depth
depth,
hash,
file_type
});
}
paths.sort();
Ok(Self {
root,
paths,
cursor: 0,
offset: 0,
column: 0,
size: Measure::new(),
})
Ok(paths)
}
}

View file

@ -2,7 +2,6 @@ use crate::*;
use tek_tui::ratatui::{style::{Color, Style}, prelude::Stylize};
use pad::PadStr;
fn table_row (label: &str, artist: &str, album: &str, track: &str, title: &str) -> String {
let label = label.pad_to_width(COLUMN_WIDTHS[0] as usize);
let artist = artist.pad_to_width(COLUMN_WIDTHS[1] as usize);
@ -11,6 +10,7 @@ fn table_row (label: &str, artist: &str, album: &str, track: &str, title: &str)
let title = title.pad_to_width(COLUMN_WIDTHS[4] as usize);
format!("{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))))
}
@ -49,7 +49,7 @@ 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 {"+"} else {" "};
let icon = if entry.is_dir {"+"} else if entry.is_img {"I"} else if entry.is_mus {"M"} else {" "};
let name = fragment.display();
let indent = "".pad_to_width((entry.depth - 1) * 2);
let label = table_row(&format!("{cursor} {indent}{icon} {name}"), "", "", "", "");