From 81cc2babeb7a71874b6d820fba2f8cb14ac4341d Mon Sep 17 00:00:00 2001 From: qkzk Date: Thu, 28 Dec 2023 23:39:52 +0100 Subject: WIP: use tokyo to generate cache async. Doesn't work well --- src/app/application.rs | 8 +++---- src/app/session.rs | 3 +++ src/app/status.rs | 10 +++++---- src/app/tab.rs | 45 +++++++++++++++++++++++++++---------- src/modes/display/cache_previews.rs | 15 ++++++++++++- src/modes/display/mod.rs | 2 +- 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/src/app/application.rs b/src/app/application.rs index 1955cb5..9f6b0c1 100644 --- a/src/app/application.rs +++ b/src/app/application.rs @@ -1,6 +1,7 @@ use std::sync::Arc; use anyhow::Result; +use tokio::sync::Mutex; use tuikit::prelude::Event; use crate::app::Refresher; @@ -115,12 +116,11 @@ impl FM { } pub fn cache_previews(&mut self) { - if matches!(self.status.tabs[0].display_mode, DisplayMode::Directory) { + if self.status.display_settings.preview() + && matches!(self.status.tabs[0].display_mode, DisplayMode::Directory) + { self.status.tabs[0].update_next_prev(); } - if matches!(self.status.tabs[1].display_mode, DisplayMode::Directory) { - self.status.tabs[1].update_next_prev(); - } } /// True iff the application must quit. diff --git a/src/app/session.rs b/src/app/session.rs index 4542542..8b1853f 100644 --- a/src/app/session.rs +++ b/src/app/session.rs @@ -86,14 +86,17 @@ impl Session { session_bool } + /// use two panes ? pub fn dual(&self) -> bool { self.dual } + /// do we display all info or only the filenames ? pub fn metadata(&self) -> bool { self.metadata } + /// use the second pane to preview pub fn preview(&self) -> bool { self.preview } diff --git a/src/app/status.rs b/src/app/status.rs index b311b29..7466b22 100644 --- a/src/app/status.rs +++ b/src/app/status.rs @@ -368,11 +368,13 @@ impl Status { let preview = match fileinfo.file_kind { FileKind::Directory => Preview::directory(&fileinfo, &self.tabs[0].users), _ => { - if !self.tabs[0].cache_previews.contains(&fileinfo.path) { - self.tabs[0].cache_previews.update(&fileinfo.path)?; + let mut cache0 = tokio::runtime::Runtime::new() + .unwrap() + .block_on(self.tabs[0].access_cache()); + if !cache0.contains(&fileinfo.path) { + cache0.update(&fileinfo.path)?; } - Ok(self.tabs[0] - .cache_previews + Ok(cache0 .read(&fileinfo.path) .context("nothing in cache...")? .to_owned()) diff --git a/src/app/tab.rs b/src/app/tab.rs index 662a51b..fe813a2 100644 --- a/src/app/tab.rs +++ b/src/app/tab.rs @@ -1,8 +1,10 @@ use std::borrow::Borrow; use std::cmp::min; use std::path; +use std::sync::Arc; use anyhow::{Context, Result}; +use tokio::sync::{Mutex, MutexGuard}; use ueberzug::Ueberzug; use crate::common::{ @@ -97,7 +99,7 @@ pub struct Tab { /// Ueberzug instance used to draw images in previews ueber: Ueberzug, /// Cache preview - pub cache_previews: CachePreviews, + pub cache_previews: Arc>, } impl Tab { @@ -135,7 +137,7 @@ impl Tab { let index = directory.select_file(&path); let tree = Tree::default(); let ueber = Ueberzug::new(); - let cache_previews = CachePreviews::default(); + let cache_previews = Arc::new(Mutex::new(CachePreviews::default())); window.scroll_to(index); Ok(Self { @@ -321,6 +323,10 @@ impl Tab { Ok(()) } + pub async fn access_cache(&self) -> MutexGuard { + self.cache_previews.lock().await + } + /// Creates a new preview for the selected file. pub fn make_preview(&mut self) -> Result<()> { if self.directory.is_empty() { @@ -331,13 +337,18 @@ impl Tab { }; match file_info.file_kind { FileKind::NormalFile => { - if !self.cache_previews.contains(&file_info.path) { - self.cache_previews.update(&file_info.path)?; + let mut cache = tokio::runtime::Runtime::new() + .unwrap() + .block_on(self.cache_previews.lock()); + if !cache.contains(&file_info.path) { + cache.update(&file_info.path)?; } - let preview = match self.cache_previews.read(&file_info.path) { + let opt_preview = cache.read(&file_info.path).to_owned(); + let preview = match opt_preview { Some(preview) => preview.to_owned(), - None => Preview::empty(), + None => Preview::default(), }; + drop(cache); self.set_display_mode(Display::Preview); self.window.reset(preview.len()); self.preview = preview; @@ -561,12 +572,22 @@ impl Tab { } pub fn update_next_prev(&mut self) { - let next_index = (self.directory.index + 1) % self.directory.len(); - let next_fileinfo = &self.directory.content()[next_index]; - let _ = self.cache_previews.update(&next_fileinfo.path); - let prev_index = self.directory.index.checked_sub(1).unwrap_or_default(); - let prev_fileinfo = &self.directory.content()[prev_index]; - let _ = self.cache_previews.update(&prev_fileinfo.path); + let index = self.directory.index; + let mut next_index = index; + let mut prev_index = index; + let mut cache = tokio::runtime::Runtime::new() + .unwrap() + .block_on(self.access_cache()); + for _ in 0..10 { + next_index = (next_index + 1) % self.directory.len(); + let next_fileinfo = &self.directory.content()[next_index]; + let _ = cache.update(&next_fileinfo.path); + } + for _ in 0..10 { + prev_index = prev_index.checked_sub(1).unwrap_or_default(); + let prev_fileinfo = &self.directory.content()[prev_index]; + let _ = cache.update(&prev_fileinfo.path); + } } /// Move to the top of the current directory. diff --git a/src/modes/display/cache_previews.rs b/src/modes/display/cache_previews.rs index 2f31152..26849b1 100644 --- a/src/modes/display/cache_previews.rs +++ b/src/modes/display/cache_previews.rs @@ -1,10 +1,23 @@ use std::path::Path; +use std::sync::Arc; use std::{collections::HashMap, path::PathBuf}; use anyhow::Result; +use tokio::sync::{mpsc, Mutex}; use crate::modes::Preview; +pub async fn update_cache( + mut rx: mpsc::Receiver, + cache_previews: Arc>, +) -> Result<()> { + while let Some(path) = rx.recv().await { + let mut cache = cache_previews.lock().await; + cache.update(&path)?; + } + Ok(()) +} + #[derive(Default)] pub struct CachePreviews { previews: HashMap, @@ -12,7 +25,7 @@ pub struct CachePreviews { } impl CachePreviews { - const PREVIEWS_CAPACITY: usize = 5; + const PREVIEWS_CAPACITY: usize = 100; pub fn contains(&self, path: &Path) -> bool { let res = self.previews.contains_key(path); diff --git a/src/modes/display/mod.rs b/src/modes/display/mod.rs index 2f47b0b..06493ba 100644 --- a/src/modes/display/mod.rs +++ b/src/modes/display/mod.rs @@ -7,7 +7,7 @@ mod skim; mod tree; mod users; -pub use cache_previews::CachePreviews; +pub use cache_previews::{update_cache, CachePreviews}; pub use content_window::ContentWindow; pub use directory::{files_collection, human_size, read_symlink_dest, shorten_path, Directory}; pub use fileinfo::{ -- cgit v1.2.3