From dc371b782c8a641a74eb83582ff15d112d394cb6 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Fri, 4 Aug 2023 22:42:47 +0800 Subject: [PATCH] .. --- adaptor/Cargo.toml | 2 +- adaptor/src/image.rs | 70 +++++++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 38 deletions(-) diff --git a/adaptor/Cargo.toml b/adaptor/Cargo.toml index 4a623a68..9372bc6b 100644 --- a/adaptor/Cargo.toml +++ b/adaptor/Cargo.toml @@ -15,7 +15,7 @@ image = "^0" md-5 = "^0" once_cell = "^1" ratatui = "^0" -tokio = { version = "^1", features = ["parking_lot"] } +tokio = { version = "^1", features = [ "parking_lot" ] } # Logging tracing = "^0" diff --git a/adaptor/src/image.rs b/adaptor/src/image.rs index 8b1fa53a..39bdc369 100644 --- a/adaptor/src/image.rs +++ b/adaptor/src/image.rs @@ -10,47 +10,43 @@ use tokio::fs; pub struct Image; impl Image { - pub(super) async fn crop(path: &Path, size: (u16, u16)) -> Result { - let (w, h) = { - let r = tty_ratio(); - let (w, h) = ((size.0 as f64 * r.0) as u32, (size.1 as f64 * r.1) as u32); - (w.min(PREVIEW.max_width), h.min(PREVIEW.max_height)) - }; + pub(super) async fn crop(path: &Path, size: (u16, u16)) -> Result { + let (w, h) = { + let r = tty_ratio(); + let (w, h) = ((size.0 as f64 * r.0) as u32, (size.1 as f64 * r.1) as u32); + (w.min(PREVIEW.max_width), h.min(PREVIEW.max_height)) + }; - let img = fs::read(path).await?; - let img = tokio::task::spawn_blocking(move || -> Result { - let img = image::load_from_memory(&img)?; - Ok(if img.width() > w || img.height() > h { - img.resize(w, h, FilterType::Triangle) - } else { - img - }) - }); + let img = fs::read(path).await?; + let img = tokio::task::spawn_blocking(move || -> Result { + let img = image::load_from_memory(&img)?; + Ok(if img.width() > w || img.height() > h { + img.resize(w, h, FilterType::Triangle) + } else { + img + }) + }); - img.await? - } + img.await? + } - pub async fn precache(img: Vec, cache: PathBuf) -> Result<()> { - let result = tokio::task::spawn_blocking(move || { - let img = image::load_from_memory(&img)?; - let (w, h) = (PREVIEW.max_width, PREVIEW.max_height); + pub async fn precache(img: Vec, cache: PathBuf) -> Result<()> { + let result = 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::<(), Error>(()) - }); + if img.width() > w || img.height() > h { + img.resize(w, h, FilterType::Triangle).save_with_format(cache, ImageFormat::Jpeg)?; + } + Ok::<(), Error>(()) + }); - result.await? - } + result.await? + } - #[inline] - pub fn cache(path: &Path) -> PathBuf { - format!( - "/tmp/yazi/{:x}", - Md5::new_with_prefix(path.to_string_lossy().as_bytes()).finalize() - ) - .into() - } + #[inline] + pub fn cache(path: &Path) -> PathBuf { + format!("/tmp/yazi/{:x}", Md5::new_with_prefix(path.to_string_lossy().as_bytes()).finalize()) + .into() + } }