diff options
author | qkzk <qu3nt1n@gmail.com> | 2024-11-07 21:13:20 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2024-11-07 21:13:20 +0100 |
commit | 1d9d05df81d520b1fc1e117fcac8e56796ce1534 (patch) | |
tree | dbc0a8bdb20aaac8f78542f4e6dc95064e567cf5 | |
parent | 02c38b20731daa66d48b5279e2eb40f2a33ec9ff (diff) |
WIP: preview manager follows dir, not all, not perfectly
-rw-r--r-- | src/app/pw2.rs | 2 | ||||
-rw-r--r-- | src/app/status.rs | 13 | ||||
-rw-r--r-- | src/common/mod.rs | 2 | ||||
-rw-r--r-- | src/common/random.rs | 2 | ||||
-rw-r--r-- | src/event/event_action.rs | 4 | ||||
-rw-r--r-- | src/modes/display/directory.rs | 14 | ||||
-rw-r--r-- | src/modes/display/uber.rs | 33 | ||||
-rw-r--r-- | src/modes/utils/leave_menu.rs | 1 |
8 files changed, 53 insertions, 18 deletions
diff --git a/src/app/pw2.rs b/src/app/pw2.rs index 01c6f5ac..69190f98 100644 --- a/src/app/pw2.rs +++ b/src/app/pw2.rs @@ -40,7 +40,7 @@ status has Arc<Mutex<Queue>> unlock the queue # Second step: follow cd & clear - +# Thrid use it for ueberzug # Third step: quit gracefully, require mpsc for worker */ diff --git a/src/app/status.rs b/src/app/status.rs index 94cc9e46..21af48d0 100644 --- a/src/app/status.rs +++ b/src/app/status.rs @@ -591,6 +591,19 @@ impl Status { Ok(()) } + pub fn preview_directory(&mut self) { + self.clear_preview_queue(); + self.tabs[self.index] + .directory + .index_to_index() + .iter() + .for_each(|task| self.preview_manager.enqueue(task)); + } + + pub fn clear_preview_queue(&mut self) { + self.preview_manager.clear(); + } + /// Check if the previewer has sent a preview. /// /// If the previewer has sent a preview, it's attached to the correct tab. diff --git a/src/common/mod.rs b/src/common/mod.rs index 19ee4fb5..ae0c443d 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -12,5 +12,5 @@ mod utils; pub use constant_strings_paths::*; pub use format::*; -pub use random::{random_alpha_chars, randomize_path}; +pub use random::{hash_path, random_alpha_chars}; pub use utils::*; diff --git a/src/common/random.rs b/src/common/random.rs index 26ac3541..3670930f 100644 --- a/src/common/random.rs +++ b/src/common/random.rs @@ -39,7 +39,7 @@ where /// Hash a path (as str, string, Cow<str>, Path, PathBuf) into a predictable string. /// It will allow to check quicly if a file is already created. -pub fn randomize_path<P: AsRef<std::path::Path>>(p: P) -> String { +pub fn hash_path<P: AsRef<std::path::Path>>(p: P) -> String { let h = hasher(p.as_ref()); h.to_string() } diff --git a/src/event/event_action.rs b/src/event/event_action.rs index d6609ba1..b36f42db 100644 --- a/src/event/event_action.rs +++ b/src/event/event_action.rs @@ -391,7 +391,9 @@ impl EventAction { return Ok(()); } if tab.directory.is_selected_dir()? { - tab.go_to_selected_dir() + tab.go_to_selected_dir()?; + status.preview_directory(); + Ok(()) } else { EventAction::open_file(status) } diff --git a/src/modes/display/directory.rs b/src/modes/display/directory.rs index 0b30a4d8..49a63b90 100644 --- a/src/modes/display/directory.rs +++ b/src/modes/display/directory.rs @@ -176,6 +176,20 @@ impl Directory { .collect() } + pub fn index_to_index(&self) -> Vec<&Path> { + self.content + .iter() + .map(|fileinfo| fileinfo.path.borrow()) + .skip(self.index) + .chain( + self.content + .iter() + .map(|fileinfo| fileinfo.path.borrow()) + .take(self.index), + ) + .collect() + } + /// True iff the selected path is ".." which is the parent dir. pub fn is_dotdot_selected(&self) -> bool { let Some(selected) = &self.selected() else { diff --git a/src/modes/display/uber.rs b/src/modes/display/uber.rs index 449dfe00..032e8286 100644 --- a/src/modes/display/uber.rs +++ b/src/modes/display/uber.rs @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf}; use std::time::Instant; use crate::common::{ - filename_from_path, path_to_string, randomize_path, FFMPEG, FONTIMAGE, LIBREOFFICE, PDFINFO, + filename_from_path, hash_path, path_to_string, FFMPEG, FONTIMAGE, LIBREOFFICE, PDFINFO, PDFTOPPM, RSVG_CONVERT, THUMBNAIL_PATH_NO_EXT, THUMBNAIL_PATH_PNG, }; use crate::io::{execute_and_capture_output, execute_and_output_no_log}; @@ -167,13 +167,15 @@ pub struct UeberBuilder { } impl UeberBuilder { - const VIDEO_THUMBNAILS: [&'static str; 5] = [ - "/tmp/fm_thumbnail_1.jpg", - "/tmp/fm_thumbnail_2.jpg", - "/tmp/fm_thumbnail_3.jpg", - "/tmp/fm_thumbnail_4.jpg", - "/tmp/fm_thumbnail_5.jpg", - ]; + pub fn video_thumbnails(hashed_path: &str) -> [String; 5] { + [ + format!("/tmp/fm-previews/{hashed_path}_1.jpg"), + format!("/tmp/fm-previews/{hashed_path}_2.jpg"), + format!("/tmp/fm-previews/{hashed_path}_3.jpg"), + format!("/tmp/fm-previews/{hashed_path}_4.jpg"), + format!("/tmp/fm-previews/{hashed_path}_5.jpg"), + ] + } pub fn new(source: &Path, kind: Kind) -> Self { let source = source.to_path_buf(); @@ -265,7 +267,8 @@ impl UeberBuilder { .to_str() .context("make_thumbnail: couldn't parse the path into a string")?; Thumbnail::create(&self.kind, path_str); - let images: Vec<PathBuf> = Self::VIDEO_THUMBNAILS + let hashed_path = hash_path(path_str); + let images: Vec<PathBuf> = Self::video_thumbnails(&hashed_path) .map(PathBuf::from) .into_iter() .filter(|p| p.exists()) @@ -338,10 +341,12 @@ impl Thumbnail { } fn create_video(path_str: &str) -> Result<()> { - let filename = format!( - "/tmp/fm-previews/{rand}_%d.jpg", - rand = randomize_path(path_str) - ); + let rand = hash_path(path_str); + let ffmpeg_filename = format!("/tmp/fm-previews/{rand}_%d.jpg",); + if Path::new(&UeberBuilder::video_thumbnails(&rand)[0]).exists() { + return Ok(()); + } + let ffmpeg_args = [ "-i", path_str, @@ -353,7 +358,7 @@ impl Thumbnail { "2", "-frames:v", "5", - &filename, + &ffmpeg_filename, // &format!("{THUMBNAIL_PATH_NO_EXT}_%d.jpg"), ]; Thumbnail::execute(FFMPEG, &ffmpeg_args) diff --git a/src/modes/utils/leave_menu.rs b/src/modes/utils/leave_menu.rs index 882f16bf..d878aba6 100644 --- a/src/modes/utils/leave_menu.rs +++ b/src/modes/utils/leave_menu.rs @@ -249,6 +249,7 @@ impl LeaveMenu { } let completed = status.menu.completion.current_proposition(); let path = string_to_path(completed)?; + status.clear_preview_queue(); status.menu.input.reset(); status.current_tab_mut().cd_to_file(&path)?; let len = status.current_tab().directory.content.len(); |