From fa786d931d17d374657ba50c9918b732aeb1d169 Mon Sep 17 00:00:00 2001 From: "alterwain@protonmail.com" Date: Mon, 17 Feb 2025 05:57:12 +0300 Subject: [PATCH] start of self-implemented scrollbox. --- Cargo.lock | 12 +++++------ Cargo.toml | 2 +- src/file_system.rs | 52 +++++++++++++++++++++++++++++++++++++++------- src/main_screen.rs | 6 +++++- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index daf4a50..33caf00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -374,7 +374,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -936,8 +936,8 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "itunesdb" -version = "0.1.19" -source = "git+https://gitea.awain.net/alterwain/ITunesDB.git#e1e8d0a12ca2c3825191ff1d815645a704e1b646" +version = "0.1.50" +source = "git+https://gitea.awain.net/alterwain/ITunesDB.git#2c38d91cd89908b20a2b08da1b3202dacf5d1f9b" dependencies = [ "bincode", "env_logger", @@ -1488,7 +1488,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -1831,7 +1831,7 @@ dependencies = [ "getrandom 0.3.1", "once_cell", "rustix", - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] @@ -2297,7 +2297,7 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.52.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 0ed3241..9ae576d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ futures = "0.3" tokio = { version = "1", features = ["full"] } tokio-util = { version = "0.7.12", features = ["codec"] } soundcloud = { version = "0.1.8", git = "https://gitea.awain.net/alterwain/soundcloud_api.git" } -itunesdb = { version = "0.1.19", git = "https://gitea.awain.net/alterwain/ITunesDB.git" } +itunesdb = { version = "0.1.50", git = "https://gitea.awain.net/alterwain/ITunesDB.git" } puremp3 = "0.1.0" mp3-duration = "0.1.10" rand = "0.8.5" \ No newline at end of file diff --git a/src/file_system.rs b/src/file_system.rs index d363c4b..ea6ae84 100644 --- a/src/file_system.rs +++ b/src/file_system.rs @@ -1,11 +1,25 @@ use crate::{screen::AppScreen, theme::Theme}; +use chrono::{DateTime, Utc}; use ratatui::layout::{Constraint, Direction, Layout, Rect}; use ratatui::prelude::{Color, Line, Style, Stylize}; use ratatui::widgets::{Block, Borders, Paragraph, Row, Table}; use ratatui::Frame; +use std::cmp::Ordering; +use std::fs::DirEntry; +use std::os::unix::fs::MetadataExt; +use std::path::PathBuf; -#[derive(Default)] -pub struct FileSystem {} +pub struct FileSystem { + dir: Vec, +} + +impl Default for FileSystem { + fn default() -> Self { + let mut a = Self { dir: Vec::new() }; + a.get_path(dirs::document_dir().unwrap()); + a + } +} impl AppScreen for FileSystem { fn handle_key_event(&mut self, key_event: crossterm::event::KeyEvent) {} @@ -43,15 +57,39 @@ impl AppScreen for FileSystem { } impl FileSystem { + fn get_path(&mut self, p: PathBuf) { + let paths = std::fs::read_dir(p).unwrap(); + self.dir = paths + .filter_map(|res| res.ok()) + .filter(|p| p.path().extension().map_or(false, |ext| ext == "mp3") || p.path().is_dir()) + .collect(); + self.dir.sort_by(|a, b| { + if a.file_type().unwrap().is_dir() { + Ordering::Less + } else { + Ordering::Greater + } + }); + } + fn render_main(&self, frame: &mut Frame, area: Rect) { let mut v = vec![Row::new(vec!["Name", "Type", "Size", "Modified"]) .style(Style::default().fg(Color::Gray))]; - // move this out to make hdd not suffer - let paths = std::fs::read_dir("~/Documents").unwrap(); - - for path in paths { - v.push(); + for entry in self.dir.iter() { + let datetime: DateTime = entry.metadata().unwrap().modified().unwrap().into(); + let datetime = datetime.format("%d/%m/%Y %T").to_string(); + let size = entry.metadata().unwrap().size().to_string(); + let file_type = entry.file_type().unwrap().is_file().to_string(); + v.push( + Row::new(vec![ + entry.file_name().to_str().unwrap().to_string(), + file_type, + size, + datetime, + ]) + .style(Style::default()), + ); } let table = Table::new( diff --git a/src/main_screen.rs b/src/main_screen.rs index 11f384d..c8ea046 100644 --- a/src/main_screen.rs +++ b/src/main_screen.rs @@ -293,7 +293,7 @@ impl MainScreen { frame.render_widget(table, chunks[0]); - let rows = match self.selected_tab { + let mut rows = match self.selected_tab { 1 => { // sc let mut v = Vec::new(); @@ -352,6 +352,10 @@ impl MainScreen { _ => Vec::new(), }; + if chunks[1].rows().count() <= self.selected_song as usize { + rows = rows[self.selected_song as usize..].to_vec(); + } + // Create the table let table = Table::new( rows,