mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Simplify the code
This commit is contained in:
parent
04316e6242
commit
dba0c643b7
5 changed files with 43 additions and 56 deletions
|
|
@ -66,12 +66,12 @@ rules = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[tasks]
|
[tasks]
|
||||||
micro_workers = 5
|
micro_workers = 5
|
||||||
macro_workers = 10
|
macro_workers = 10
|
||||||
bizarre_retry = 5
|
bizarre_retry = 5
|
||||||
image_alloc = 536870912 # 512MB
|
image_alloc = 536870912 # 512MB
|
||||||
image_bound = [ 0, 0 ]
|
image_bound = [ 0, 0 ]
|
||||||
ignore_precaching_tasks = true
|
suppress_preload = false
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
preload = []
|
preload = []
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ pub struct Tasks {
|
||||||
pub image_alloc: u32,
|
pub image_alloc: u32,
|
||||||
pub image_bound: [u16; 2],
|
pub image_bound: [u16; 2],
|
||||||
|
|
||||||
pub ignore_precaching_tasks: bool,
|
pub suppress_preload: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Tasks {
|
impl Default for Tasks {
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,9 @@ pub struct Running {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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.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
|
self.incr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,23 +32,28 @@ impl Running {
|
||||||
pub fn get_id(&self, idx: usize) -> Option<usize> { self.values().nth(idx).map(|t| t.id) }
|
pub fn get_id(&self, idx: usize) -> Option<usize> { self.values().nth(idx).map(|t| t.id) }
|
||||||
|
|
||||||
#[inline]
|
#[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]
|
#[inline]
|
||||||
pub(super) fn exists(&self, id: usize) -> bool { self.all.contains_key(&id) }
|
pub(super) fn exists(&self, id: usize) -> bool { self.all.contains_key(&id) }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn values(&self) -> impl Iterator<Item = &Task> {
|
pub fn values(&self) -> Box<dyn Iterator<Item = &Task> + '_> {
|
||||||
let map = self.all.values();
|
if TASKS.suppress_preload {
|
||||||
if TASKS.ignore_precaching_tasks {
|
Box::new(self.all.values().filter(|t| t.kind != TaskKind::Preload))
|
||||||
map.into_iter().filter(|t| t.kind == TaskKind::User).collect::<Vec<_>>().into_iter()
|
|
||||||
} else {
|
} else {
|
||||||
map.into_iter().collect::<Vec<_>>().into_iter()
|
Box::new(self.all.values())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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(
|
pub(super) fn try_remove(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
|
|
@ -174,7 +174,7 @@ impl Scheduler {
|
||||||
|
|
||||||
pub fn file_cut(&self, from: Url, mut to: Url, force: bool) {
|
pub fn file_cut(&self, from: Url, mut to: Url, force: bool) {
|
||||||
let mut running = self.running.write();
|
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, {
|
running.hooks.insert(id, {
|
||||||
let from = from.clone();
|
let from = from.clone();
|
||||||
|
|
@ -205,7 +205,7 @@ impl Scheduler {
|
||||||
|
|
||||||
pub fn file_copy(&self, from: Url, mut to: Url, force: bool) {
|
pub fn file_copy(&self, from: Url, mut to: Url, force: bool) {
|
||||||
let name = format!("Copy {:?} to {:?}", from, to);
|
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({
|
_ = self.todo.send_blocking({
|
||||||
let file = self.file.clone();
|
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) {
|
pub fn file_link(&self, from: Url, mut to: Url, relative: bool, force: bool) {
|
||||||
let name = format!("Link {from:?} to {to:?}");
|
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({
|
_ = self.todo.send_blocking({
|
||||||
let file = self.file.clone();
|
let file = self.file.clone();
|
||||||
|
|
@ -240,7 +240,7 @@ impl Scheduler {
|
||||||
|
|
||||||
pub fn file_delete(&self, target: Url) {
|
pub fn file_delete(&self, target: Url) {
|
||||||
let mut running = self.running.write();
|
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, {
|
running.hooks.insert(id, {
|
||||||
let target = target.clone();
|
let target = target.clone();
|
||||||
|
|
@ -268,7 +268,7 @@ impl Scheduler {
|
||||||
|
|
||||||
pub fn file_trash(&self, target: Url) {
|
pub fn file_trash(&self, target: Url) {
|
||||||
let name = format!("Trash {:?}", target);
|
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({
|
_ = self.todo.send_blocking({
|
||||||
let file = self.file.clone();
|
let file = self.file.clone();
|
||||||
|
|
@ -287,7 +287,7 @@ impl Scheduler {
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut running = self.running.write();
|
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();
|
let (cancel_tx, mut cancel_rx) = oneshot::channel();
|
||||||
running.hooks.insert(id, {
|
running.hooks.insert(id, {
|
||||||
|
|
@ -335,11 +335,7 @@ impl Scheduler {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = running.add(format!("Calculate the size of {:?}", target));
|
let id = running.add(TaskKind::Preload, format!("Calculate the size of {:?}", target));
|
||||||
if let Some(task) = self.running.clone().write().get_mut(id) {
|
|
||||||
task.kind = TaskKind::PreCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = self.todo.send_blocking({
|
_ = self.todo.send_blocking({
|
||||||
let precache = self.precache.clone();
|
let precache = self.precache.clone();
|
||||||
let target = target.clone();
|
let target = target.clone();
|
||||||
|
|
@ -354,11 +350,7 @@ impl Scheduler {
|
||||||
|
|
||||||
pub fn precache_mime(&self, targets: Vec<Url>) {
|
pub fn precache_mime(&self, targets: Vec<Url>) {
|
||||||
let name = format!("Preload mimetype for {} files", targets.len());
|
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);
|
||||||
|
|
||||||
if let Some(task) = self.running.clone().write().get_mut(id) {
|
|
||||||
task.kind = TaskKind::PreCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = self.todo.send_blocking({
|
_ = self.todo.send_blocking({
|
||||||
let precache = self.precache.clone();
|
let precache = self.precache.clone();
|
||||||
|
|
@ -371,33 +363,21 @@ impl Scheduler {
|
||||||
|
|
||||||
pub fn precache_image(&self, targets: Vec<Url>) {
|
pub fn precache_image(&self, targets: Vec<Url>) {
|
||||||
let name = format!("Precache of {} image files", targets.len());
|
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);
|
||||||
|
|
||||||
if let Some(task) = self.running.clone().write().get_mut(id) {
|
|
||||||
task.kind = TaskKind::PreCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.precache.image(id, targets).ok();
|
self.precache.image(id, targets).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn precache_video(&self, targets: Vec<Url>) {
|
pub fn precache_video(&self, targets: Vec<Url>) {
|
||||||
let name = format!("Precache of {} video files", targets.len());
|
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);
|
||||||
|
|
||||||
if let Some(task) = self.running.clone().write().get_mut(id) {
|
|
||||||
task.kind = TaskKind::PreCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.precache.video(id, targets).ok();
|
self.precache.video(id, targets).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn precache_pdf(&self, targets: Vec<Url>) {
|
pub fn precache_pdf(&self, targets: Vec<Url>) {
|
||||||
let name = format!("Precache of {} PDF files", targets.len());
|
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);
|
||||||
|
|
||||||
if let Some(task) = self.running.clone().write().get_mut(id) {
|
|
||||||
task.kind = TaskKind::PreCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.precache.pdf(id, targets).ok();
|
self.precache.pdf(id, targets).ok();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ use tokio::sync::mpsc;
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Task {
|
pub struct Task {
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
|
pub kind: TaskKind,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub stage: TaskStage,
|
pub stage: TaskStage,
|
||||||
pub kind: TaskKind,
|
|
||||||
|
|
||||||
pub total: u32,
|
pub total: u32,
|
||||||
pub succ: u32,
|
pub succ: u32,
|
||||||
|
|
@ -19,7 +19,16 @@ pub struct Task {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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)]
|
#[derive(Debug)]
|
||||||
|
|
@ -70,10 +79,3 @@ pub enum TaskStage {
|
||||||
Dispatched,
|
Dispatched,
|
||||||
Hooked,
|
Hooked,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
|
|
||||||
pub enum TaskKind {
|
|
||||||
#[default]
|
|
||||||
User,
|
|
||||||
PreCache,
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue