Skip to content

Commit ce13dc3

Browse files
ze0987sxyazi
authored andcommitted
perf!: faster image preview with optimized magick arguments (sxyazi#2533)
Co-authored-by: sxyazi <[email protected]>
1 parent 596eccd commit ce13dc3

File tree

4 files changed

+72
-19
lines changed

4 files changed

+72
-19
lines changed

yazi-config/preset/yazi-default.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ spotters = [
9898
{ mime = "text/*", run = "code" },
9999
{ mime = "application/{mbox,javascript,wine-extension-ini}", run = "code" },
100100
# Image
101-
{ mime = "image/{avif,hei?,jxl,svg+xml}", run = "magick" },
101+
{ mime = "image/{avif,hei?,jxl}", run = "magick" },
102+
{ mime = "image/svg+xml", run = "svg" },
102103
{ mime = "image/*", run = "image" },
103104
# Video
104105
{ mime = "video/*", run = "video" },
@@ -107,7 +108,8 @@ spotters = [
107108
]
108109
preloaders = [
109110
# Image
110-
{ mime = "image/{avif,hei?,jxl,svg+xml}", run = "magick" },
111+
{ mime = "image/{avif,hei?,jxl}", run = "magick" },
112+
{ mime = "image/svg+xml", run = "svg" },
111113
{ mime = "image/*", run = "image" },
112114
# Video
113115
{ mime = "video/*", run = "video" },
@@ -125,7 +127,8 @@ previewers = [
125127
# JSON
126128
{ mime = "application/{json,ndjson}", run = "json" },
127129
# Image
128-
{ mime = "image/{avif,hei?,jxl,svg+xml}", run = "magick" },
130+
{ mime = "image/{avif,hei?,jxl}", run = "magick" },
131+
{ mime = "image/svg+xml", run = "svg" },
129132
{ mime = "image/*", run = "image" },
130133
# Video
131134
{ mime = "video/*", run = "video" },

yazi-plugin/preset/plugins/magick.lua

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,19 @@ function M:preload(job)
2424
return true
2525
end
2626

27-
local cmd = Command("magick"):args {
28-
"-density",
29-
200,
30-
tostring(job.file.url),
31-
"-flatten",
32-
"-resize",
33-
string.format("%dx%d^", rt.preview.max_width, rt.preview.max_height),
34-
"-quality",
35-
rt.preview.image_quality,
36-
"-auto-orient",
37-
"JPG:" .. tostring(cache),
38-
}
39-
40-
if rt.tasks.image_alloc > 0 then
41-
cmd = cmd:env("MAGICK_MEMORY_LIMIT", rt.tasks.image_alloc)
27+
local cmd = M.with_env()
28+
if job.args.flatten then
29+
cmd = cmd:arg("-flatten")
4230
end
4331

44-
local status, err = cmd:env("MAGICK_THREAD_LIMIT", 1):status()
32+
-- stylua: ignore
33+
local status, err = cmd:args {
34+
tostring(job.file.url), "-auto-orient", "-strip",
35+
"-sample", string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
36+
"-quality", rt.preview.image_quality,
37+
string.format("JPG:%s", cache),
38+
}:status()
39+
4540
if status then
4641
return status.success
4742
else
@@ -51,4 +46,12 @@ end
5146

5247
function M:spot(job) require("file"):spot(job) end
5348

49+
function M.with_env()
50+
local cmd = Command("magick"):env("MAGICK_THREAD_LIMIT", 1)
51+
if rt.tasks.image_alloc > 0 then
52+
cmd = cmd:env("MAGICK_MEMORY_LIMIT", rt.tasks.image_alloc)
53+
end
54+
return cmd
55+
end
56+
5457
return M

yazi-plugin/preset/plugins/svg.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
local M = {}
2+
3+
function M:peek(job)
4+
local start, cache = os.clock(), ya.file_cache(job)
5+
if not cache then
6+
return
7+
end
8+
9+
local ok, err = self:preload(job)
10+
if not ok or err then
11+
return
12+
end
13+
14+
ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock()))
15+
ya.image_show(cache, job.area)
16+
ya.preview_widgets(job, {})
17+
end
18+
19+
function M:seek() end
20+
21+
function M:preload(job)
22+
local cache = ya.file_cache(job)
23+
if not cache or fs.cha(cache) then
24+
return true
25+
end
26+
27+
-- stylua: ignore
28+
local cmd = require("magick").with_env():args {
29+
"-density", 200,
30+
tostring(job.file.url), "-strip",
31+
"-resize", string.format("%dx%d^", rt.preview.max_width, rt.preview.max_height),
32+
"-quality", rt.preview.image_quality,
33+
string.format("JPG:%s", cache),
34+
}
35+
36+
local status, err = cmd:status()
37+
if status then
38+
return status.success
39+
else
40+
return true, Err("Failed to start `magick`, error: %s", err)
41+
end
42+
end
43+
44+
function M:spot(job) require("file"):spot(job) end
45+
46+
return M

yazi-plugin/src/loader/loader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl Default for Loader {
4242
("noop".to_owned(), preset!("plugins/noop").into()),
4343
("pdf".to_owned(), preset!("plugins/pdf").into()),
4444
("session".to_owned(), preset!("plugins/session").into()),
45+
("svg".to_owned(), preset!("plugins/svg").into()),
4546
("video".to_owned(), preset!("plugins/video").into()),
4647
("zoxide".to_owned(), preset!("plugins/zoxide").into()),
4748
]);

0 commit comments

Comments
 (0)