From 5a83577259d5dbff77479f3c27217cd23fd72f4c 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: Fri, 17 May 2024 15:33:05 +0800 Subject: [PATCH] feat: SVG, HEIC, and JPEG XL preview support (#1050) --- cspell.json | 2 +- yazi-config/preset/yazi.toml | 9 ++++-- yazi-plugin/preset/plugins/magick.lua | 43 +++++++++++++++++++++++++++ yazi-plugin/src/loader/loader.rs | 1 + 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 yazi-plugin/preset/plugins/magick.lua diff --git a/cspell.json b/cspell.json index e69845bf..28ac714a 100644 --- a/cspell.json +++ b/cspell.json @@ -1 +1 @@ -{"version":"0.2","flagWords":[],"language":"en","words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","imagesize","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime"]} \ No newline at end of file +{"language":"en","flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","imagesize","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick"],"version":"0.2"} \ No newline at end of file diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 2338b4d5..0bad8712 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -82,7 +82,10 @@ suppress_preload = false preloaders = [ { name = "*", cond = "!mime", run = "mime", multi = true, prio = "high" }, # Image - { mime = "image/*", run = "image" }, + { mime = "image/svg+xml", run = "magick" }, + { mime = "image/heic", run = "magick" }, + { mime = "image/jxl", run = "magick" }, + { mime = "image/*", run = "image" }, # Video { mime = "video/*", run = "video" }, # PDF @@ -99,7 +102,9 @@ previewers = [ # JSON { mime = "application/json", run = "json" }, # Image - { mime = "image/vnd.djvu", run = "noop" }, + { mime = "image/svg+xml", run = "magick" }, + { mime = "image/heic", run = "magick" }, + { mime = "image/jxl", run = "magick" }, { mime = "image/*", run = "image" }, # Video { mime = "video/*", run = "video" }, diff --git a/yazi-plugin/preset/plugins/magick.lua b/yazi-plugin/preset/plugins/magick.lua new file mode 100644 index 00000000..1f865b4a --- /dev/null +++ b/yazi-plugin/preset/plugins/magick.lua @@ -0,0 +1,43 @@ +local M = {} + +function M:peek() + local cache = ya.file_cache(self) + if not cache then + return + end + + if self:preload() == 1 then + ya.image_show(cache, self.area) + ya.preview_widgets(self, {}) + end +end + +function M:seek() end + +function M:preload() + local cache = ya.file_cache(self) + if not cache or fs.cha(cache) then + return 1 + end + + local child, code = Command("convert"):args({ + "-density", + "200", + "-resize", + string.format("%dx%d^", PREVIEW.max_width, PREVIEW.max_height), + "-quality", + tostring(PREVIEW.image_quality), + tostring(self.file.url), + "JPG:" .. tostring(cache), + }):spawn() + + if not child then + ya.err("spawn `convert` command returns " .. tostring(code)) + return 0 + end + + local status = child:wait() + return status and status:success() and 1 or 2 +end + +return M diff --git a/yazi-plugin/src/loader/loader.rs b/yazi-plugin/src/loader/loader.rs index eb059c3b..c00fddb7 100644 --- a/yazi-plugin/src/loader/loader.rs +++ b/yazi-plugin/src/loader/loader.rs @@ -44,6 +44,7 @@ impl Loader { "fzf" => include_bytes!("../../preset/plugins/fzf.lua"), "image" => include_bytes!("../../preset/plugins/image.lua"), "json" => include_bytes!("../../preset/plugins/json.lua"), + "magick" => include_bytes!("../../preset/plugins/magick.lua"), "mime" => include_bytes!("../../preset/plugins/mime.lua"), "pdf" => include_bytes!("../../preset/plugins/pdf.lua"), "video" => include_bytes!("../../preset/plugins/video.lua"),