feat: new config option to suppress pre-caching tasks (#430)

This commit is contained in:
againstpetra 2023-12-05 17:07:23 +00:00 committed by GitHub
parent dc1649f9ba
commit 1d1a512710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 24 deletions

View File

@ -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 = []

View File

@ -14,6 +14,8 @@ pub struct Tasks {
pub image_alloc: u32,
pub image_bound: [u16; 2],
pub suppress_preload: bool,
}
impl Default for Tasks {

View File

@ -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 {

View File

@ -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<usize> { 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<Item = &Task> { self.all.values() }
pub fn values(&self) -> Box<dyn Iterator<Item = &Task> + '_> {
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,

View File

@ -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<File>,
@ -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<Url>) {
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<Url>) {
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<Url>) {
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<Url>) {
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();
}

View File

@ -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)]