From 1d1a51271036a4ea2e9ec67d1c7fe5cabeb5d75a Mon Sep 17 00:00:00 2001 From: againstpetra <128837728+againstpetra@users.noreply.github.com> Date: Tue, 5 Dec 2023 17:07:23 +0000 Subject: [PATCH] feat: new config option to suppress pre-caching tasks (#430) --- yazi-config/preset/yazi.toml | 11 ++++++----- yazi-config/src/tasks/tasks.rs | 2 ++ yazi-core/src/files/sorter.rs | 2 +- yazi-scheduler/src/running.rs | 24 +++++++++++++++++++----- yazi-scheduler/src/scheduler.rs | 24 ++++++++++++------------ yazi-scheduler/src/task.rs | 12 +++++++++++- 6 files changed, 51 insertions(+), 24 deletions(-) diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 36665aa9..4240f05b 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -66,11 +66,12 @@ rules = [ ] [tasks] -micro_workers = 5 -macro_workers = 10 -bizarre_retry = 5 -image_alloc = 536870912 # 512MB -image_bound = [ 0, 0 ] +micro_workers = 5 +macro_workers = 10 +bizarre_retry = 5 +image_alloc = 536870912 # 512MB +image_bound = [ 0, 0 ] +suppress_preload = false [plugins] preload = [] diff --git a/yazi-config/src/tasks/tasks.rs b/yazi-config/src/tasks/tasks.rs index ec73d54a..21900dfe 100644 --- a/yazi-config/src/tasks/tasks.rs +++ b/yazi-config/src/tasks/tasks.rs @@ -14,6 +14,8 @@ pub struct Tasks { pub image_alloc: u32, pub image_bound: [u16; 2], + + pub suppress_preload: bool, } impl Default for Tasks { diff --git a/yazi-core/src/files/sorter.rs b/yazi-core/src/files/sorter.rs index bdf620ba..75c0c5fe 100644 --- a/yazi-core/src/files/sorter.rs +++ b/yazi-core/src/files/sorter.rs @@ -1,7 +1,7 @@ use std::{cmp::Ordering, collections::BTreeMap, mem}; use yazi_config::manager::SortBy; -use yazi_shared::{fs::File, fs::Url, natsort}; +use yazi_shared::{fs::{File, Url}, natsort}; #[derive(Clone, Copy, Default, PartialEq)] pub struct FilesSorter { diff --git a/yazi-scheduler/src/running.rs b/yazi-scheduler/src/running.rs index d353a970..e2d67b37 100644 --- a/yazi-scheduler/src/running.rs +++ b/yazi-scheduler/src/running.rs @@ -1,8 +1,10 @@ use std::collections::BTreeMap; use futures::future::BoxFuture; +use yazi_config::TASKS; use super::{Task, TaskStage}; +use crate::TaskKind; #[derive(Default)] pub struct Running { @@ -14,9 +16,9 @@ pub struct Running { } impl Running { - pub(super) fn add(&mut self, name: String) -> usize { + pub(super) fn add(&mut self, kind: TaskKind, name: String) -> usize { self.incr += 1; - self.all.insert(self.incr, Task::new(self.incr, name)); + self.all.insert(self.incr, Task::new(self.incr, kind, name)); self.incr } @@ -30,16 +32,28 @@ impl Running { pub fn get_id(&self, idx: usize) -> Option { self.values().nth(idx).map(|t| t.id) } #[inline] - pub fn len(&self) -> usize { self.all.len() } + pub fn len(&self) -> usize { + if TASKS.suppress_preload { + self.all.values().filter(|t| t.kind != TaskKind::Preload).count() + } else { + self.all.len() + } + } #[inline] pub(super) fn exists(&self, id: usize) -> bool { self.all.contains_key(&id) } #[inline] - pub fn values(&self) -> impl Iterator { self.all.values() } + pub fn values(&self) -> Box + '_> { + if TASKS.suppress_preload { + Box::new(self.all.values().filter(|t| t.kind != TaskKind::Preload)) + } else { + Box::new(self.all.values()) + } + } #[inline] - pub fn is_empty(&self) -> bool { self.all.is_empty() } + pub fn is_empty(&self) -> bool { self.len() == 0 } pub(super) fn try_remove( &mut self, diff --git a/yazi-scheduler/src/scheduler.rs b/yazi-scheduler/src/scheduler.rs index 55faa5a5..b67f1811 100644 --- a/yazi-scheduler/src/scheduler.rs +++ b/yazi-scheduler/src/scheduler.rs @@ -7,7 +7,7 @@ use yazi_config::{open::Opener, TASKS}; use yazi_shared::{emit, event::Exec, fs::{unique_path, Url}, Layer, Throttle}; use super::{Running, TaskOp, TaskStage}; -use crate::workers::{File, FileOpDelete, FileOpLink, FileOpPaste, FileOpTrash, Precache, PrecacheOpMime, PrecacheOpSize, Process, ProcessOpOpen}; +use crate::{workers::{File, FileOpDelete, FileOpLink, FileOpPaste, FileOpTrash, Precache, PrecacheOpMime, PrecacheOpSize, Process, ProcessOpOpen}, TaskKind}; pub struct Scheduler { file: Arc, @@ -174,7 +174,7 @@ impl Scheduler { pub fn file_cut(&self, from: Url, mut to: Url, force: bool) { let mut running = self.running.write(); - let id = running.add(format!("Cut {:?} to {:?}", from, to)); + let id = running.add(TaskKind::User, format!("Cut {:?} to {:?}", from, to)); running.hooks.insert(id, { let from = from.clone(); @@ -205,7 +205,7 @@ impl Scheduler { pub fn file_copy(&self, from: Url, mut to: Url, force: bool) { let name = format!("Copy {:?} to {:?}", from, to); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::User, name); _ = self.todo.send_blocking({ let file = self.file.clone(); @@ -221,7 +221,7 @@ impl Scheduler { pub fn file_link(&self, from: Url, mut to: Url, relative: bool, force: bool) { let name = format!("Link {from:?} to {to:?}"); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::User, name); _ = self.todo.send_blocking({ let file = self.file.clone(); @@ -240,7 +240,7 @@ impl Scheduler { pub fn file_delete(&self, target: Url) { let mut running = self.running.write(); - let id = running.add(format!("Delete {:?}", target)); + let id = running.add(TaskKind::User, format!("Delete {:?}", target)); running.hooks.insert(id, { let target = target.clone(); @@ -268,7 +268,7 @@ impl Scheduler { pub fn file_trash(&self, target: Url) { let name = format!("Trash {:?}", target); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::User, name); _ = self.todo.send_blocking({ let file = self.file.clone(); @@ -287,7 +287,7 @@ impl Scheduler { }; let mut running = self.running.write(); - let id = running.add(name); + let id = running.add(TaskKind::User, name); let (cancel_tx, mut cancel_rx) = oneshot::channel(); running.hooks.insert(id, { @@ -335,7 +335,7 @@ impl Scheduler { continue; } - let id = running.add(format!("Calculate the size of {:?}", target)); + let id = running.add(TaskKind::Preload, format!("Calculate the size of {:?}", target)); _ = self.todo.send_blocking({ let precache = self.precache.clone(); let target = target.clone(); @@ -350,7 +350,7 @@ impl Scheduler { pub fn precache_mime(&self, targets: Vec) { let name = format!("Preload mimetype for {} files", targets.len()); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::Preload, name); _ = self.todo.send_blocking({ let precache = self.precache.clone(); @@ -363,21 +363,21 @@ impl Scheduler { pub fn precache_image(&self, targets: Vec) { let name = format!("Precache of {} image files", targets.len()); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::Preload, name); self.precache.image(id, targets).ok(); } pub fn precache_video(&self, targets: Vec) { let name = format!("Precache of {} video files", targets.len()); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::Preload, name); self.precache.video(id, targets).ok(); } pub fn precache_pdf(&self, targets: Vec) { let name = format!("Precache of {} PDF files", targets.len()); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::Preload, name); self.precache.pdf(id, targets).ok(); } diff --git a/yazi-scheduler/src/task.rs b/yazi-scheduler/src/task.rs index ffb7dd0f..6c404453 100644 --- a/yazi-scheduler/src/task.rs +++ b/yazi-scheduler/src/task.rs @@ -3,6 +3,7 @@ use tokio::sync::mpsc; #[derive(Debug, Default)] pub struct Task { pub id: usize, + pub kind: TaskKind, pub name: String, pub stage: TaskStage, @@ -18,7 +19,16 @@ pub struct Task { } impl Task { - pub fn new(id: usize, name: String) -> Self { Self { id, name, ..Default::default() } } + pub fn new(id: usize, kind: TaskKind, name: String) -> Self { + Self { id, kind, name, ..Default::default() } + } +} + +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub enum TaskKind { + #[default] + User, + Preload, } #[derive(Debug)]