From 54f289018e92de5f38d71c6093ef46e7bf23b649 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 19 Jul 2023 19:42:59 +0800 Subject: [PATCH] feat: compressing images in a separate blocking thread --- README.md | 3 +++ src/core/manager/preview.rs | 4 ++-- src/core/tasks/precache.rs | 24 +++++++++++++----------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 4ce4bf19..9d323a44 100644 --- a/README.md +++ b/README.md @@ -42,11 +42,14 @@ cargo build --release ./target/release/yazi ``` +If you want to use your own config, copy the [config folder](https://github.com/sxyazi/yazi/tree/main/config) to `~/.config/yazi`, and modify it as you like. + ## TODO - [x] Add example config for general usage, currently please see my [another repo](https://github.com/sxyazi/dotfiles/tree/main/yazi) instead - [x] Integration with fzf, zoxide for fast directory navigation - [x] Integration with fd, rg for fuzzy file searching +- [ ] Documentation of commands and options - [ ] Support for Überzug++ for image previews with X11/wayland environment - [ ] Batch renaming support diff --git a/src/core/manager/preview.rs b/src/core/manager/preview.rs index 52672390..8a0b5153 100644 --- a/src/core/manager/preview.rs +++ b/src/core/manager/preview.rs @@ -91,9 +91,9 @@ impl Preview { (w.min(PREVIEW.max_width), h.min(PREVIEW.max_height)) }; - let file = fs::read(path).await?; + let img = fs::read(path).await?; tokio::task::spawn_blocking(move || -> Result> { - let img = image::load_from_memory(&file)?; + let img = image::load_from_memory(&img)?; Kitty::image_show(if img.width() > w || img.height() > h { img.resize(w, h, FilterType::Triangle) } else { diff --git a/src/core/tasks/precache.rs b/src/core/tasks/precache.rs index d295ef86..852feb2b 100644 --- a/src/core/tasks/precache.rs +++ b/src/core/tasks/precache.rs @@ -1,6 +1,6 @@ use std::path::{Path, PathBuf}; -use anyhow::Result; +use anyhow::{Error, Result}; use image::{imageops::FilterType, ImageFormat}; use tokio::{fs, sync::mpsc}; @@ -60,17 +60,19 @@ impl Precache { return Ok(self.sch.send(TaskOp::Adv(task.id, 1, 0))?); } - let img = fs::read(&task.target).await.map(|b| image::load_from_memory(&b)); - let img = if let Ok(Ok(img)) = img { - img - } else { - return Ok(self.sch.send(TaskOp::Adv(task.id, 1, 0))?); - }; + let img = fs::read(&task.target).await; + tokio::task::spawn_blocking(move || { + let img = image::load_from_memory(&img?)?; + let (w, h) = (PREVIEW.max_width, PREVIEW.max_height); + + if img.width() > w || img.height() > h { + img.resize(w, h, FilterType::Triangle).save_with_format(&cache, ImageFormat::Jpeg).ok(); + } + Ok::<(), Error>(()) + }) + .await + .ok(); - let (w, h) = (PREVIEW.max_width, PREVIEW.max_height); - if img.width() > w || img.height() > h { - img.resize(w, h, FilterType::Triangle).save_with_format(&cache, ImageFormat::Jpeg).ok(); - } self.sch.send(TaskOp::Adv(task.id, 1, 0))?; } PrecacheOp::Video(task) => {