From 950750f66e9aadf9d96f52b014c406f3f640959d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Wed, 26 Feb 2025 20:47:11 +0800 Subject: [PATCH] fix: respect the user's `image_alloc` setting for the built-in ImageMagick previewer (#2403) --- yazi-plugin/preset/plugins/magick.lua | 32 ++++++++++++++------------- yazi-plugin/src/config/runtime.rs | 13 ++++++++++- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/yazi-plugin/preset/plugins/magick.lua b/yazi-plugin/preset/plugins/magick.lua index 185c7780..8524725e 100644 --- a/yazi-plugin/preset/plugins/magick.lua +++ b/yazi-plugin/preset/plugins/magick.lua @@ -24,22 +24,24 @@ function M:preload(job) return true end - local status, err = Command("magick") - :args({ - "-density", - 200, - tostring(job.file.url), - "-flatten", - "-resize", - string.format("%dx%d^", rt.preview.max_width, rt.preview.max_height), - "-quality", - rt.preview.image_quality, - "-auto-orient", - "JPG:" .. tostring(cache), - }) - :env("MAGICK_THREAD_LIMIT", 1) - :status() + local cmd = Command("magick"):args { + "-density", + 200, + tostring(job.file.url), + "-flatten", + "-resize", + string.format("%dx%d^", rt.preview.max_width, rt.preview.max_height), + "-quality", + rt.preview.image_quality, + "-auto-orient", + "JPG:" .. tostring(cache), + } + if rt.tasks.image_alloc > 0 then + cmd = cmd:env("MAGICK_MEMORY_LIMIT", rt.tasks.image_alloc) + end + + local status, err = cmd:env("MAGICK_THREAD_LIMIT", 1):status() if status then return status.success else diff --git a/yazi-plugin/src/config/runtime.rs b/yazi-plugin/src/config/runtime.rs index a3482614..00f840cd 100644 --- a/yazi-plugin/src/config/runtime.rs +++ b/yazi-plugin/src/config/runtime.rs @@ -1,6 +1,6 @@ use mlua::{IntoLua, Lua, LuaSerdeExt, SerializeOptions, Value}; use yazi_boot::ARGS; -use yazi_config::{MGR, PREVIEW, THEME}; +use yazi_config::{MGR, PREVIEW, TASKS, THEME}; use crate::{Composer, url::Url}; @@ -19,6 +19,7 @@ impl<'a> Runtime<'a> { b"mgr" => Self::mgr(lua)?, b"plugin" => super::Plugin::compose(lua)?, b"preview" => Self::preview(lua)?, + b"tasks" => Self::tasks(lua)?, _ => return Ok(Value::Nil), } .into_lua(lua) @@ -62,6 +63,16 @@ impl<'a> Runtime<'a> { }) } + fn tasks(lua: &Lua) -> mlua::Result { + Composer::make(lua, 5, |lua, key| { + match key { + b"image_alloc" => lua.to_value_with(&TASKS.image_alloc, OPTS)?, + _ => return Ok(Value::Nil), + } + .into_lua(lua) + }) + } + pub fn new(lua: &'a Lua) -> Self { Self { lua } } // TODO: remove this