[yazi] Init

This commit is contained in:
aloslider committed 2026-07-16 22:04:57 +03:00
1 parent ba3e3927e5
commit 3d9d2532d2
27 files changed
+3414 -3

No files matched your search

+1
View File
@@ -60,6 +60,7 @@ image/bmp=gthumb.desktop;
image/tiff=gthumb.desktop;
image/svg+xml=gthumb.desktop;
image/*=gthumb.desktop;
inode/directory=yazi.desktop
text/html=librewolf.desktop;
text/markdown=vim.desktop;
text/plain=vim.desktop;
+2 -3
View File
@@ -2,10 +2,9 @@
screenshot-path null
environment {
QT_QPA_PLATFORMTHEME "qt6ct"
QT_STYLE_OVERRIDE "kvantum"
QT_AUTO_SCREEN_SCALE_FACTOR "1"
XDG_CURRENT_DESKTOP "Niri"
QT_QPA_PLATFORM "wayland"
QT_QPA_PLATFORMTHEME "xdgdesktopportal"
}
cursor {
@@ -0,0 +1,5 @@
[filechooser]
cmd=$HOME/.config/xdg-desktop-portal-termfilechooser/yazi-wrapper.sh
default_dir=$HOME
open_mode=suggested
save_mode=last
@@ -0,0 +1,37 @@
#!/usr/bin/env sh
set -e
multiple="$1"
directory="$2"
save="$3"
path="$4"
out="$5"
termcmd="kitty --class=filechooser --title=Yazi-FileChooser -e"
cmd="yazi"
if [ "$save" = "1" ]; then
set -- --chooser-file="$out" "$path"
elif [ "$directory" = "1" ]; then
set -- --chooser-file="$out" --cwd-file="$out.1" "$path"
else
set -- --chooser-file="$out" "$path"
fi
# Escape and run
command="$termcmd $cmd"
for arg in "$@"; do
escaped=$(printf '%s' "$arg" | sed 's/"/\\"/g')
command="$command \"$escaped\""
done
sh -c "$command"
# Handle directory selection (Yazi writes cwd on quit)
if [ "$directory" = "1" ]; then
if [ ! -s "$out" ] && [ -s "$out.1" ]; then
cat "$out.1" > "$out"
fi
rm -f "$out.1"
fi
@@ -0,0 +1,7 @@
[preferred]
default=gtk;niri
{{ if .use_yazi_picker }}
org.freedesktop.impl.portal.FileChooser=termfilechooser
{{ else }}
org.freedesktop.impl.portal.FileChooser=gtk
{{ end }}
+14
View File
@@ -0,0 +1,14 @@
[[plugin.deps]]
use = "boydaihungst/mediainfo"
rev = "a6d30a1"
hash = "7d4dd1afcb7ecbd3609c29a82d6dbc5c"
[[plugin.deps]]
use = "yazi-rs/flavors:kanagawa"
rev = "4770a34"
hash = "99aa06d3014798d86001c324468d497f"
[[flavor.deps]]
use = "dangooddd/kanagawa"
rev = "04985d1"
hash = "c69a63981e254a3a06a11c2bf0e6af1d"
Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 286 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 261 KiB

@@ -0,0 +1,20 @@
Copyright (c) 2024 Lauri Niskanen
Copyright (c) 2025 Huy Hoang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,196 @@
# mediainfo.yazi (fork)
<!--toc:start-->
- [mediainfo.yazi (fork)](#mediainfoyazi-fork)
- [Preview](#preview)
- [Installation](#installation)
- [Configuration:](#configuration)
- [Custom theme](#custom-theme)
<!--toc:end-->
This is a Yazi plugin for previewing media files. The preview shows thumbnail
using `ffmpeg` if available and media metadata using `mediainfo`.
> [!IMPORTANT]
> Minimum version: yazi v26.1.22.
> Check it via command `yazi --debug`
## Preview
- Video
![video](assets/2025-02-15-09-15-39.png)
- Audio file with cover
![audio_with_cover_picture](assets/2025-02-15-09-14-23.png)
- Images
![image](assets/2025-02-15-16-52-39.png)
- Subtitle
![subrip](assets/2025-02-15-16-51-11.png)
- SVG+XML file doesn't have useful information, so it only show the image preview.
- There are more file extensions which are supported by mediainfo. Just add file's MIME type to `prepend_previewers`, `prepend_preloaders`.
Use `spotter` to determine File's MIME type. [Default is `<Tab>` key](https://github.com/sxyazi/yazi/blob/1a6abae974370702c8865459344bf256de58359e/yazi-config/preset/keymap-default.toml#L59)
## Installation
- Install mediainfo CLI:
- [https://mediaarea.net/en/MediaInfo/Download](https://mediaarea.net/en/MediaInfo/Download)
- Run this command in terminal to check if it's installed correctly:
```bash
mediainfo --version
```
If it output `Not found` then add it to your PATH environment variable. It's better to ask ChatGPT to help you (Prompt: `Add MediaInfo CLI to PATH environment variable in Windows`).
- Install ImageMagick (for linux, you can use your distro package manager to install):
https://imagemagick.org/script/download.php
- Install this plugin:
```bash
ya pkg add boydaihungst/mediainfo
```
## Configuration:
> [!IMPORTANT]
>
> `mediainfo` use built-in video, image, svg, magick plugins behind the scene to render preview image, song cover.
> So you can remove those 4 plugins from `prepend_preloaders` and `prepend_previewers` sections in `yazi.toml`.
If you have cache problem, run this cmd, and follow the tips: `yazi --clear-cache`
Config folder for each OS: https://yazi-rs.github.io/docs/configuration/overview.
Create `.../yazi/yazi.toml` and add:
```toml
[plugin]
prepend_preloaders = [
# Replace magick, image, video with mediainfo
{ mime = "{audio,video,image}/*", run = "mediainfo" },
{ mime = "application/subrip", run = "mediainfo" },
# Adobe Photoshop is image/adobe.photoshop, already handled above
# Adobe Illustrator
{ mime = "application/postscript", run = "mediainfo" },
{ mime = "application/illustrator", run = "mediainfo" },
{ mime = "application/dvb.ait", run = "mediainfo" },
{ mime = "application/vnd.adobe.illustrator", run = "mediainfo" },
{ mime = "image/x-eps", run = "mediainfo" },
{ mime = "application/eps", run = "mediainfo" },
# Sometimes AI file is recognized as "application/pdf". Lmao.
# In this case use file extension instead:
{ url = "*.{ai,eps,ait}", run = "mediainfo" },
# Hide metadata by default.
# Example for image mimetype:
{ mime = "{image}/*", run = "mediainfo --no-metadata" },
# Hide image preview by default.
# Example for video mimetype:
{ mime = "{video}/*", run = "mediainfo --no-preview" },
# NOTE: Use both --no-metadata and --no-preview will display nothing. :)
# Make sure both of your previewers and preloaders has the same arguments (--no-metadata and --no-preview)
]
prepend_previewers = [
# Replace magick, image, video with mediainfo
{ mime = "{audio,video,image}/*", run = "mediainfo"},
{ mime = "application/subrip", run = "mediainfo" },
# Adobe Photoshop is image/adobe.photoshop, already handled above
# Adobe Illustrator
{ mime = "application/postscript", run = "mediainfo" },
{ mime = "application/illustrator", run = "mediainfo" },
{ mime = "application/dvb.ait", run = "mediainfo" },
{ mime = "application/vnd.adobe.illustrator", run = "mediainfo" },
{ mime = "image/x-eps", run = "mediainfo" },
{ mime = "application/eps", run = "mediainfo" },
# Sometimes AI file is recognized as "application/pdf". Lmao.
# In this case use file extension instead:
{ url = "*.{ai,eps,ait}", run = "mediainfo" },
# Hide metadata by default.
# Example for image mimetype:
{ mime = "{image}/*", run = "mediainfo --no-metadata" },
# Hide image preview by default.
# Example for video mimetype:
{ mime = "{video}/*", run = "mediainfo --no-preview" },
# NOTE: Use both --no-metadata and --no-preview will display nothing. :)
# Make sure both of your previewers and preloaders has the same arguments (--no-metadata and --no-preview)
]
# There are more extensions, mime types which are supported by mediainfo.
# Just add file's MIME type to `previewers`, `preloaders` above.
# https://mediaarea.net/en/MediaInfo/Support/Formats
# If it's not working, file an issue at https://github.com/boydaihungst/mediainfo.yazi/issues
# For a large file like Adobe Illustrator, Adobe Photoshop, etc
# you may need to increase the memory limit if no image is rendered.
# https://yazi-rs.github.io/docs/configuration/yazi#tasks
[tasks]
image_alloc = 1073741824 # = 1024*1024*1024 = 1024MB
```
## Custom theme
Using the same style with spotter windows. [Read more](https://github.com/sxyazi/yazi/pull/2391)
Edit or add `yazi/theme.toml`:
```toml
[spot]
# Section header style.
# Example: Video, Text, Image,... with green color in preview images above
title = { fg = "green" }
# Value style.
# Example: `Format: FLAC` with blue color in preview images above
tbl_col = { fg = "blue" }
```
## (Optional) Keymaps to toggle/show/hide/reset metadata and preview image
> [!IMPORTANT]
> Use any key you want, but make sure there is no conflicts with [default Keybindings](https://github.com/sxyazi/yazi/blob/main/yazi-config/preset/keymap-default.toml).
Since Yazi prioritizes the first matching key, `prepend_keymap` takes precedence over defaults.
Or you can use `keymap` to replace all other keys
```toml
[mgr]
prepend_keymap = [
{ on = "<F3>", run = "plugin mediainfo -- toggle-metadata", desc = "Toggle media preview metadata" },
{ on = "<F4>", run = "plugin mediainfo -- toggle-preview", desc = "Toggle media preview image" },
# Hide and show metadata, image
{ on = "<F6>", run = "plugin mediainfo -- hide-metadata", desc = "Hide media preview metadata" },
{ on = "<F7>", run = "plugin mediainfo -- hide-preview", desc = "Hide media preview image" },
{ on = "<F8>", run = "plugin mediainfo -- show-metadata", desc = "Show media preview metadata" },
{ on = "<F9>", run = "plugin mediainfo -- show-preview", desc = "Show media preview image" },
# Reset to defautl settings in yazi.toml file
{ on = "<F5>", run = "plugin mediainfo -- reset", desc = "Reset media preview to default settings" },
# Use multiple actions
{ on = "<F10>", run = "plugin mediainfo -- --toggle-preview --toggle-metadata", desc = "Toggle both media preview image and metadata" },
{ on = "<F11>", run = "plugin mediainfo -- --show-preview --hide-metadata", desc = "Show media preview image and hide metadata" },
# You can also use all of them together (7 actions above). Priority is reset > toggle > hide > show
{ on = "<F12>", run = "plugin mediainfo -- --show-preview --hide-metadata --reset --show-metadata --hide-preview", desc = "Show media preview image and hide metadata" },
]
```
@@ -0,0 +1,300 @@
--- @since 26.1.22
local M = {}
local const = require(".const")
local utils = require(".utils")
local function image_layer_count(job)
local cache = ya.file_cache({ file = job.file, skip = 0 })
if not cache then
return 0
end
local layer_count = utils.get_state("f" .. tostring(cache))
if layer_count then
return layer_count
end
local output, err = Command("identify")
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
if err or not output then
return 0
end
layer_count = 0
for line in output.stdout:gmatch("[^\r\n]+") do
if line:match("%S") then
layer_count = layer_count + 1
end
end
utils.set_state("f" .. tostring(cache), layer_count)
return layer_count
end
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache(job)
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_job_skip = job.skip
::recalc_mediainfo_job_skip::
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = ui.Line({
ui.Span(label .. ": "):style(ui.Style():fg("reset"):bold()),
ui.Span(value):style(th.spot.tbl_col or ui.Style():fg("blue")),
})
end
elseif str ~= "General" then
line = ui.Line({ ui.Span(str):style(th.spot.title or ui.Style():fg("green")) })
end
if line then
local line_height = ui.height
and ui.height(str, { width = max_width, ansi = true, wrap = rt.preview.wrap })
or (math.max(1, is_wrap and math.ceil(ui.width(line) / max_width) or 1))
if next_str == nil and line_height == 1 then
EOF_mediainfo = true
end
if (last_line + line_height) > mediainfo_job_skip then
table.insert(lines, line)
end
if (last_line + line_height) >= mediainfo_job_skip + limit then
last_line = mediainfo_job_skip + limit
EOF_mediainfo = false
break
end
last_line = last_line + line_height
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata then
if EOF_mediainfo and #lines == 0 and mediainfo_job_skip > 0 then
if
image_layer_count(job)
< (
1
+ math.floor(
math.max(
0,
utils.get_state(const.STATE_KEY.units)
and (math.abs(job.skip / utils.get_state(const.STATE_KEY.units)))
or 0
)
)
)
then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
else
local last_valid_mediainfo_skip = utils.get_state(const.STATE_KEY.last_valid_mediainfo_skip)
mediainfo_job_skip = last_valid_mediainfo_skip
and last_valid_mediainfo_skip[tostring(cache_img_url_no_skip)]
or math.max(0, mediainfo_job_skip - (utils.get_state(const.STATE_KEY.units) or 0))
goto recalc_mediainfo_job_skip
end
else
utils.set_state(
const.STATE_KEY.last_valid_mediainfo_skip,
{ [tostring(cache_img_url_no_skip)] = mediainfo_job_skip }
)
end
end
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
utils.force_render()
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Preload image
local cache_img_url = ya.file_cache(job)
local cache_img_url_cha = cache_img_url and fs.cha(cache_img_url)
-- NOTE: Only generate preview image when cache image is not exist
if not cache_img_url_cha or cache_img_url_cha.len <= 0 then
local cache_img_status, image_preload_err
local layer_index = 0
local units = utils.get_state(const.STATE_KEY.units)
if units ~= nil then
local max_layer = image_layer_count(job)
layer_index = math.floor(math.max(0, math.abs(job.skip / units)))
if layer_index + 1 > max_layer then
layer_index = math.max(0, max_layer - 1)
end
end
local cache_img_url_tmp = Url(cache_img_url .. ".tmp")
if fs.cha(cache_img_url_tmp) then
fs.remove("file", cache_img_url_tmp)
end
local tmp_file_path, _ = type(fs.unique) == "function" and fs.unique("file", cache_img_url_tmp)
or fs.unique_name(cache_img_url_tmp)
cache_img_status, image_preload_err = require("magick")
.with_limit()
:arg({
"-background",
"none",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) .. "[" .. tostring(
layer_index
) .. "]",
"-auto-orient",
"-strip",
"-resize",
string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
"-quality",
rt.preview.image_quality,
string.format("PNG32:%s", tostring(tmp_file_path)),
})
:status()
if cache_img_status then
os.rename(tostring(tmp_file_path), tostring(cache_img_url))
end
if not cache_img_status and image_preload_err then
ya.dbg("mediainfo", image_preload_err)
err_msg = err_msg .. (image_preload_err and (tostring(image_preload_err)) or "")
end
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,336 @@
--- @since 26.5.6
local M = {}
local const = require(".const")
local utils = require(".utils")
local function image_layer_count(job)
local cache = ya.file_cache({ file = job.file, skip = 0 })
if not cache then
return 0
end
local layer_count = utils.get_state("f" .. tostring(cache))
if layer_count then
return layer_count
end
local output, err = Command("identify")
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
if err or not output then
return 0
end
layer_count = 0
for line in output.stdout:gmatch("[^\r\n]+") do
if line:match("%S") then
layer_count = layer_count + 1
end
end
utils.set_state("f" .. tostring(cache), layer_count)
return layer_count
end
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache(job)
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_job_skip = job.skip
::recalc_mediainfo_job_skip::
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
local opt = { ansi = true, tab_size = rt.preview.tab_size, wrap = rt.preview.wrap, width = max_width }
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = label .. ": " .. value
end
elseif str ~= "General" then
line = str
end
if line then
local wrapped = ui.lines(line, opt)
local line_height = #wrapped
local from = 1
local to = math.min(line_height, mediainfo_job_skip + limit - last_line)
local total_rendered_text_len = 1
local total_label_rendered_len = 1
local label_total_len = label and utf8.len(label .. ": ") or 0
for j = from, to do
local current_line_components = {}
local wrapped_line_len = wrapped[j]:width() or 0
local wrapped_raw =
utils.utf8_sub(line, total_rendered_text_len, total_rendered_text_len + wrapped_line_len)
wrapped_line_len = utf8.len(wrapped_raw)
total_rendered_text_len = total_rendered_text_len + wrapped_line_len
if last_line + 1 > mediainfo_job_skip then
local label_raw = label_total_len - total_label_rendered_len <= 0 and ""
or utils.utf8_sub(wrapped_raw, 1, label_total_len - total_label_rendered_len)
local label_raw_len = utf8.len(label_raw)
if label_raw_len > 0 then
table.insert(
current_line_components,
ui.Span(label_raw):style(ui.Style():fg("reset"):bold())
)
total_label_rendered_len = total_label_rendered_len + label_raw_len
wrapped_raw = wrapped_raw:gsub("^" .. utils.is_literal_string(label_raw), "", 1)
end
if total_label_rendered_len >= label_total_len then
local value_raw = wrapped_raw
table.insert(
current_line_components,
ui.Span(value_raw or ""):style(
label and (th.spot.tbl_col or ui.Style():fg("blue"))
or (th.spot.title or ui.Style():fg("green"))
)
)
end
table.insert(lines, ui.Line(current_line_components))
-- last_line = last_line + 1
else
total_label_rendered_len = total_label_rendered_len + total_rendered_text_len
end
last_line = last_line + 1
if next_str == nil and not wrapped[j + 1] then
EOF_mediainfo = true
end
if last_line >= mediainfo_job_skip + limit then
last_line = mediainfo_job_skip + limit
EOF_mediainfo = false
break
end
end
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata then
if EOF_mediainfo and #lines == 0 and mediainfo_job_skip > 0 then
if
image_layer_count(job)
< (
1
+ math.floor(
math.max(
0,
utils.get_state(const.STATE_KEY.units)
and (math.abs(job.skip / utils.get_state(const.STATE_KEY.units)))
or 0
)
)
)
then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
else
local last_valid_mediainfo_skip = utils.get_state(const.STATE_KEY.last_valid_mediainfo_skip)
mediainfo_job_skip = last_valid_mediainfo_skip
and last_valid_mediainfo_skip[tostring(cache_img_url_no_skip)]
or math.max(0, mediainfo_job_skip - (utils.get_state(const.STATE_KEY.units) or 0))
goto recalc_mediainfo_job_skip
end
else
utils.set_state(
const.STATE_KEY.last_valid_mediainfo_skip,
{ [tostring(cache_img_url_no_skip)] = mediainfo_job_skip }
)
end
end
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
utils.force_render()
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Preload image
local cache_img_url = ya.file_cache(job)
local cache_img_url_cha = cache_img_url and fs.cha(cache_img_url)
-- NOTE: Only generate preview image when cache image is not exist
if not cache_img_url_cha or cache_img_url_cha.len <= 0 then
local cache_img_status, image_preload_err
local layer_index = 0
local units = utils.get_state(const.STATE_KEY.units)
if units ~= nil then
local max_layer = image_layer_count(job)
layer_index = math.floor(math.max(0, math.abs(job.skip / units)))
if layer_index + 1 > max_layer then
layer_index = math.max(0, max_layer - 1)
end
end
local cache_img_url_tmp = Url(cache_img_url .. ".tmp")
if fs.cha(cache_img_url_tmp) then
fs.remove("file", cache_img_url_tmp)
end
local tmp_file_path, _ = type(fs.unique) == "function" and fs.unique("file", cache_img_url_tmp)
or fs.unique_name(cache_img_url_tmp)
cache_img_status, image_preload_err = require("magick")
.with_limit()
:arg({
"-background",
"none",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) .. "[" .. tostring(
layer_index
) .. "]",
"-auto-orient",
"-strip",
"-resize",
string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
"-quality",
rt.preview.image_quality,
string.format("PNG32:%s", tostring(tmp_file_path)),
})
:status()
if cache_img_status then
os.rename(tostring(tmp_file_path), tostring(cache_img_url))
end
if not cache_img_status and image_preload_err then
ya.dbg("mediainfo", image_preload_err)
err_msg = err_msg .. (image_preload_err and (tostring(image_preload_err)) or "")
end
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,343 @@
--- @since 26.1.22
local M = {}
local const = require(".const")
local utils = require(".utils")
local function get_cover_layers(job)
local cache = ya.file_cache({ file = job.file, skip = 0 })
if not cache then
return {}
end
local covers = utils.get_state("f" .. tostring(cache))
if covers and type(covers) == "table" then
return covers
end
local output, err = Command("ffprobe"):arg({
"-v",
"error",
"-select_streams",
"v",
"-show_entries",
"stream=index:stream_disposition=attached_pic",
"-of",
"json",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url),
}):output()
if err or not output then
return {}
end
covers = {}
local data = ya.json_decode(output.stdout)
if type(data.streams) == "table" then
for _, stream in ipairs(data.streams) do
if stream.disposition and stream.disposition.attached_pic then
covers[#covers + 1] = stream.index
end
end
end
utils.set_state("f" .. tostring(cache), covers)
return covers
end
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache(job)
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_job_skip = job.skip
::recalc_mediainfo_job_skip::
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = ui.Line({
ui.Span(label .. ": "):style(ui.Style():fg("reset"):bold()),
ui.Span(value):style(th.spot.tbl_col or ui.Style():fg("blue")),
})
end
elseif str ~= "General" then
line = ui.Line({ ui.Span(str):style(th.spot.title or ui.Style():fg("green")) })
end
if line then
local line_height = ui.height
and ui.height(str, { width = max_width, ansi = true, wrap = rt.preview.wrap })
or (math.max(1, is_wrap and math.ceil(ui.width(line) / max_width) or 1))
if next_str == nil and line_height == 1 then
EOF_mediainfo = true
end
if (last_line + line_height) > mediainfo_job_skip then
table.insert(lines, line)
end
if (last_line + line_height) >= mediainfo_job_skip + limit then
last_line = mediainfo_job_skip + limit
EOF_mediainfo = false
break
end
last_line = last_line + line_height
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata then
if EOF_mediainfo and #lines == 0 and mediainfo_job_skip > 0 then
local covers = get_cover_layers(job)
local cover_index = math.floor(
math.max(
1,
utils.get_state(const.STATE_KEY.units)
and (math.abs(job.skip / utils.get_state(const.STATE_KEY.units)))
or 0
)
)
if not covers[cover_index] then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
else
-- NOTE: Recalculate mediainfo using cached latest valid skip value when reach the end of mediainfo output
local last_valid_mediainfo_skip = utils.get_state(const.STATE_KEY.last_valid_mediainfo_skip)
mediainfo_job_skip = last_valid_mediainfo_skip
and last_valid_mediainfo_skip[tostring(cache_img_url_no_skip)]
or math.max(0, mediainfo_job_skip - (utils.get_state(const.STATE_KEY.units) or 0))
goto recalc_mediainfo_job_skip
end
else
utils.set_state(
const.STATE_KEY.last_valid_mediainfo_skip,
{ [tostring(cache_img_url_no_skip)] = mediainfo_job_skip }
)
end
end
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
utils.force_render()
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Preload image
local cache_img_url = ya.file_cache(job)
local cache_img_url_cha = cache_img_url and fs.cha(cache_img_url)
-- NOTE: Only generate preview image when cache image is not exist
if cache_img_url and (not cache_img_url_cha or cache_img_url_cha.len <= 0) then
local units = utils.get_state(const.STATE_KEY.units)
local covers = get_cover_layers(job)
local cover_index = covers[1] or 0
if units ~= nil then
cover_index = math.floor(math.max(1, math.abs(job.skip / units)))
if not covers[cover_index] then
cover_index = math.max(0, #covers - 1)
end
end
local qv = 31 - math.floor(rt.preview.image_quality * 0.3)
local audio_preload_output, audio_preload_err = Command("ffmpeg"):arg({
"-v",
"error",
"-threads",
1,
"-i",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url),
"-map",
string.format("0:v:%d?", cover_index),
"-an",
"-sn",
"-dn",
"-vframes",
1,
"-q:v",
qv,
"-vf",
string.format("scale=-1:'min(%d,ih)':flags=fast_bilinear", rt.preview.max_height / 2),
"-f",
"image2",
"-y",
tostring(cache_img_url),
}):output()
-- NOTE: Some audio types doesn't have cover image -> error ""
if audio_preload_err then
ya.dbg("mediainfo", audio_preload_err)
err_msg = err_msg
.. string.format(
"Failed to start `%s`.\n Error: %s\n",
"ffmpeg",
tostring(audio_preload_err or (audio_preload_output and audio_preload_output.stderr or ""))
)
elseif
audio_preload_output
and type(audio_preload_output.stderr) == "string"
and audio_preload_output.stderr:find("does not contain any stream")
then
ya.dbg("mediainfo", audio_preload_output and audio_preload_output.stderr)
cache_img_url_cha, _ = fs.cha(cache_img_url)
if cache_img_url_cha then
fs.remove("file", Url(cache_img_url))
end
-- NOTE: Workaround case audio has no cover image. Prevent regenerate preview image
audio_preload_output, audio_preload_err = require("magick")
.with_limit()
:arg({
"-size",
"1x1",
"canvas:none",
string.format("PNG32:%s", cache_img_url),
})
:output()
if
(audio_preload_output and audio_preload_output.stderr ~= nil and audio_preload_output.stderr ~= "")
or audio_preload_err
then
ya.dbg("mediainfo", audio_preload_err or (audio_preload_output and audio_preload_output.stderr))
err_msg = err_msg
.. string.format(
"Failed to start `%s`.\n Error: %s\n",
"magick",
tostring(audio_preload_err or (audio_preload_output and audio_preload_output.stderr or ""))
)
end
end
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,379 @@
--- @since 26.5.6
local M = {}
local const = require(".const")
local utils = require(".utils")
local function get_cover_layers(job)
local cache = ya.file_cache({ file = job.file, skip = 0 })
if not cache then
return {}
end
local covers = utils.get_state("f" .. tostring(cache))
if covers and type(covers) == "table" then
return covers
end
local output, err = Command("ffprobe"):arg({
"-v",
"error",
"-select_streams",
"v",
"-show_entries",
"stream=index:stream_disposition=attached_pic",
"-of",
"json",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url),
}):output()
if err or not output then
return {}
end
covers = {}
local data = ya.json_decode(output.stdout)
if type(data.streams) == "table" then
for _, stream in ipairs(data.streams) do
if stream.disposition and stream.disposition.attached_pic then
covers[#covers + 1] = stream.index
end
end
end
utils.set_state("f" .. tostring(cache), covers)
return covers
end
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache(job)
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_job_skip = job.skip
::recalc_mediainfo_job_skip::
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
local opt = { ansi = true, tab_size = rt.preview.tab_size, wrap = rt.preview.wrap, width = max_width }
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = label .. ": " .. value
end
elseif str ~= "General" then
line = str
end
if line then
local wrapped = ui.lines(line, opt)
local line_height = #wrapped
local from = 1
local to = math.min(line_height, mediainfo_job_skip + limit - last_line)
local total_rendered_text_len = 1
local total_label_rendered_len = 1
local label_total_len = label and utf8.len(label .. ": ") or 0
for j = from, to do
local current_line_components = {}
local wrapped_line_len = wrapped[j]:width() or 0
local wrapped_raw =
utils.utf8_sub(line, total_rendered_text_len, total_rendered_text_len + wrapped_line_len)
wrapped_line_len = utf8.len(wrapped_raw)
total_rendered_text_len = total_rendered_text_len + wrapped_line_len
if last_line + 1 > mediainfo_job_skip then
local label_raw = label_total_len - total_label_rendered_len <= 0 and ""
or utils.utf8_sub(wrapped_raw, 1, label_total_len - total_label_rendered_len)
local label_raw_len = utf8.len(label_raw)
if label_raw_len > 0 then
table.insert(
current_line_components,
ui.Span(label_raw):style(ui.Style():fg("reset"):bold())
)
total_label_rendered_len = total_label_rendered_len + label_raw_len
wrapped_raw = wrapped_raw:gsub("^" .. utils.is_literal_string(label_raw), "", 1)
end
if total_label_rendered_len >= label_total_len then
local value_raw = wrapped_raw
table.insert(
current_line_components,
ui.Span(value_raw or ""):style(
label and (th.spot.tbl_col or ui.Style():fg("blue"))
or (th.spot.title or ui.Style():fg("green"))
)
)
end
table.insert(lines, ui.Line(current_line_components))
-- last_line = last_line + 1
else
total_label_rendered_len = total_label_rendered_len + total_rendered_text_len
end
last_line = last_line + 1
if next_str == nil and not wrapped[j + 1] then
EOF_mediainfo = true
end
if last_line >= mediainfo_job_skip + limit then
last_line = mediainfo_job_skip + limit
EOF_mediainfo = false
break
end
end
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata then
if EOF_mediainfo and #lines == 0 and mediainfo_job_skip > 0 then
local covers = get_cover_layers(job)
local cover_index = math.floor(
math.max(
1,
utils.get_state(const.STATE_KEY.units)
and (math.abs(job.skip / utils.get_state(const.STATE_KEY.units)))
or 0
)
)
if not covers[cover_index] then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
else
-- NOTE: Recalculate mediainfo using cached latest valid skip value when reach the end of mediainfo output
local last_valid_mediainfo_skip = utils.get_state(const.STATE_KEY.last_valid_mediainfo_skip)
mediainfo_job_skip = last_valid_mediainfo_skip
and last_valid_mediainfo_skip[tostring(cache_img_url_no_skip)]
or math.max(0, mediainfo_job_skip - (utils.get_state(const.STATE_KEY.units) or 0))
goto recalc_mediainfo_job_skip
end
else
utils.set_state(
const.STATE_KEY.last_valid_mediainfo_skip,
{ [tostring(cache_img_url_no_skip)] = mediainfo_job_skip }
)
end
end
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
utils.force_render()
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Preload image
local cache_img_url = ya.file_cache(job)
local cache_img_url_cha = cache_img_url and fs.cha(cache_img_url)
-- NOTE: Only generate preview image when cache image is not exist
if cache_img_url and (not cache_img_url_cha or cache_img_url_cha.len <= 0) then
local units = utils.get_state(const.STATE_KEY.units)
local covers = get_cover_layers(job)
local cover_index = covers[1] or 0
if units ~= nil then
cover_index = math.floor(math.max(1, math.abs(job.skip / units)))
if not covers[cover_index] then
cover_index = math.max(0, #covers - 1)
end
end
local qv = 31 - math.floor(rt.preview.image_quality * 0.3)
local audio_preload_output, audio_preload_err = Command("ffmpeg"):arg({
"-v",
"error",
"-threads",
1,
"-i",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url),
"-map",
string.format("0:v:%d?", cover_index),
"-an",
"-sn",
"-dn",
"-vframes",
1,
"-q:v",
qv,
"-vf",
string.format("scale=-1:'min(%d,ih)':flags=fast_bilinear", rt.preview.max_height / 2),
"-f",
"image2",
"-y",
tostring(cache_img_url),
}):output()
-- NOTE: Some audio types doesn't have cover image -> error ""
if audio_preload_err then
ya.dbg("mediainfo", audio_preload_err)
err_msg = err_msg
.. string.format(
"Failed to start `%s`.\n Error: %s\n",
"ffmpeg",
tostring(audio_preload_err or (audio_preload_output and audio_preload_output.stderr or ""))
)
elseif
audio_preload_output
and type(audio_preload_output.stderr) == "string"
and audio_preload_output.stderr:find("does not contain any stream")
then
ya.dbg("mediainfo", audio_preload_output and audio_preload_output.stderr)
cache_img_url_cha, _ = fs.cha(cache_img_url)
if cache_img_url_cha then
fs.remove("file", Url(cache_img_url))
end
-- NOTE: Workaround case audio has no cover image. Prevent regenerate preview image
audio_preload_output, audio_preload_err = require("magick")
.with_limit()
:arg({
"-size",
"1x1",
"canvas:none",
string.format("PNG32:%s", cache_img_url),
})
:output()
if
(audio_preload_output and audio_preload_output.stderr ~= nil and audio_preload_output.stderr ~= "")
or audio_preload_err
then
ya.dbg("mediainfo", audio_preload_err or (audio_preload_output and audio_preload_output.stderr))
err_msg = err_msg
.. string.format(
"Failed to start `%s`.\n Error: %s\n",
"magick",
tostring(audio_preload_err or (audio_preload_output and audio_preload_output.stderr or ""))
)
end
end
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,66 @@
--- @since 26.1.22
local M = {}
M.skip_labels = {
["Complete name"] = true,
["CompleteName_Last"] = true,
["Unique ID"] = true,
["File size"] = true,
["Format/Info"] = true,
["Codec ID/Info"] = true,
["MD5 of the unencoded content"] = true,
}
M.ENTRY_ACTION = {
toggle_metadata = "toggle-metadata",
toggle_preview = "toggle-preview",
hide_metadata = "hide-metadata",
hide_preview = "hide-preview",
show_metadata = "show-metadata",
show_preview = "show-preview",
reset = "reset",
}
M.STATE_KEY = {
units = "units",
no_metadata = "no_metadata",
no_preview = "no_preview",
prev_metadata_area = "prev_metadata_area",
prev_image_height = "prev_image_height",
last_valid_mediainfo_skip = "last_valid_mediainfo_skip",
cached_mediainfo = "cached_mediainfo",
cached_job_args = "cached_job_args",
}
M.magick_image_mimes = {
avif = true,
hei = true,
heic = true,
heif = true,
["heif-sequence"] = true,
["heic-sequence"] = true,
jxl = true,
tiff = true,
xml = true,
-- ["svg+xml"] = true,
["canon-cr2"] = true,
}
M.seekable_mimes = {
-- NOTE: Adobe illustrator photoshop mimetypes
["application/postscript"] = true,
["application/dvb.ait"] = true,
["application/illustrator"] = true,
["application/vnd.adobe.illustrator"] = true,
["image/x-eps"] = true,
["application/eps"] = true,
["application/pdf"] = true,
["image/adobe.photoshop"] = true,
}
M.suffix = "_mediainfo"
M.SHELL = os.getenv("SHELL") or ""
return M
@@ -0,0 +1,251 @@
--- @since 26.1.22
local M = {}
local const = require(".const")
local utils = require(".utils")
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache({
skip = 0,
args = job.args,
file = job.file,
area = job.area,
})
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = ui.Line({
ui.Span(label .. ": "):style(ui.Style():fg("reset"):bold()),
ui.Span(value):style(th.spot.tbl_col or ui.Style():fg("blue")),
})
end
elseif str ~= "General" then
line = ui.Line({ ui.Span(str):style(th.spot.title or ui.Style():fg("green")) })
end
if line then
local line_height = ui.height
and ui.height(str, { width = max_width, ansi = true, wrap = rt.preview.wrap })
or (math.max(1, is_wrap and math.ceil(ui.width(line) / max_width) or 1))
if next_str == nil and line_height == 1 then
EOF_mediainfo = true
end
if (last_line + line_height) > job.skip then
table.insert(lines, line)
end
if (last_line + line_height) >= job.skip + limit then
last_line = job.skip + limit
EOF_mediainfo = false
break
end
last_line = last_line + line_height
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata and EOF_mediainfo and #lines == 0 and job.skip > 0 then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
end
utils.force_render()
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Preload image
local mime = job.mime:match(".*/(.*)$")
local is_svg = mime == "svg+xml"
local is_magick = const.magick_image_mimes[mime]
local no_skip_job = { skip = 0, file = job.file, args = job.args, area = job.area }
local cache_img_url = ya.file_cache(no_skip_job)
local cache_img_url_cha = cache_img_url and fs.cha(cache_img_url)
-- NOTE: Only generate preview image when cache image is not exist
if not cache_img_url_cha or cache_img_url_cha.len <= 0 then
local cache_img_status, image_preload_err
if not is_valid_utf8_path then
-- NOTE: Case not valid utf8 path, use trick to generate preview image
if is_svg then
local cache_img_url_tmp = Url(cache_img_url .. ".tmp")
if fs.cha(cache_img_url_tmp) then
fs.remove("file", cache_img_url_tmp)
end
local tmp_file_path, _ = type(fs.unique) == "function" and fs.unique("file", cache_img_url_tmp)
or fs.unique_name(cache_img_url_tmp)
-- svg under invalid utf8 path
cache_img_status, image_preload_err = require("magick")
.with_limit()
:arg({
"-background",
"none",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url),
"-auto-orient",
"-strip",
string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
"-quality",
rt.preview.image_quality,
string.format("PNG32:%s", tostring(tmp_file_path)),
})
:status()
if cache_img_status then
os.rename(tostring(tmp_file_path), tostring(cache_img_url))
end
end
else
-- NOTE: Case valid utf8 path, use image, svg, or magick module
local image_module = is_svg and "svg" or (is_magick and "magick" or "image")
cache_img_status, image_preload_err = require(image_module):preload(no_skip_job)
end
if not cache_img_status and image_preload_err then
ya.dbg("mediainfo", image_preload_err)
err_msg = err_msg .. (image_preload_err and (tostring(image_preload_err)) or "")
end
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,288 @@
--- @since 26.5.6
local M = {}
local const = require(".const")
local utils = require(".utils")
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache({
skip = 0,
args = job.args,
file = job.file,
area = job.area,
})
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_job_skip = job.skip
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
local opt = { ansi = true, tab_size = rt.preview.tab_size, wrap = rt.preview.wrap, width = max_width }
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = label .. ": " .. value
end
elseif str ~= "General" then
line = str
end
if line then
local wrapped = ui.lines(line, opt)
local line_height = #wrapped
local from = 1
local to = math.min(line_height, mediainfo_job_skip + limit - last_line)
local total_rendered_text_len = 1
local total_label_rendered_len = 1
local label_total_len = label and utf8.len(label .. ": ") or 0
for j = from, to do
local current_line_components = {}
local wrapped_line_len = wrapped[j]:width() or 0
local wrapped_raw =
utils.utf8_sub(line, total_rendered_text_len, total_rendered_text_len + wrapped_line_len)
wrapped_line_len = utf8.len(wrapped_raw)
total_rendered_text_len = total_rendered_text_len + wrapped_line_len
if last_line + 1 > mediainfo_job_skip then
local label_raw = label_total_len - total_label_rendered_len <= 0 and ""
or utils.utf8_sub(wrapped_raw, 1, label_total_len - total_label_rendered_len)
local label_raw_len = utf8.len(label_raw)
if label_raw_len > 0 then
table.insert(
current_line_components,
ui.Span(label_raw):style(ui.Style():fg("reset"):bold())
)
total_label_rendered_len = total_label_rendered_len + label_raw_len
wrapped_raw = wrapped_raw:gsub("^" .. utils.is_literal_string(label_raw), "", 1)
end
if total_label_rendered_len >= label_total_len then
local value_raw = wrapped_raw
table.insert(
current_line_components,
ui.Span(value_raw or ""):style(
label and (th.spot.tbl_col or ui.Style():fg("blue"))
or (th.spot.title or ui.Style():fg("green"))
)
)
end
table.insert(lines, ui.Line(current_line_components))
-- last_line = last_line + 1
else
total_label_rendered_len = total_label_rendered_len + total_rendered_text_len
end
last_line = last_line + 1
if next_str == nil and not wrapped[j + 1] then
EOF_mediainfo = true
end
if last_line >= mediainfo_job_skip + limit then
last_line = mediainfo_job_skip + limit
EOF_mediainfo = false
break
end
end
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata and EOF_mediainfo and #lines == 0 and job.skip > 0 then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
end
utils.force_render()
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Preload image
local mime = job.mime:match(".*/(.*)$")
local is_svg = mime == "svg+xml"
local is_magick = const.magick_image_mimes[mime]
local no_skip_job = { skip = 0, file = job.file, args = job.args, area = job.area }
local cache_img_url = ya.file_cache(no_skip_job)
local cache_img_url_cha = cache_img_url and fs.cha(cache_img_url)
-- NOTE: Only generate preview image when cache image is not exist
if not cache_img_url_cha or cache_img_url_cha.len <= 0 then
local cache_img_status, image_preload_err
if not is_valid_utf8_path then
-- NOTE: Case not valid utf8 path, use trick to generate preview image
if is_svg then
local cache_img_url_tmp = Url(cache_img_url .. ".tmp")
if fs.cha(cache_img_url_tmp) then
fs.remove("file", cache_img_url_tmp)
end
local tmp_file_path, _ = type(fs.unique) == "function" and fs.unique("file", cache_img_url_tmp)
or fs.unique_name(cache_img_url_tmp)
-- svg under invalid utf8 path
cache_img_status, image_preload_err = require("magick")
.with_limit()
:arg({
"-background",
"none",
tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url),
"-auto-orient",
"-strip",
string.format("%dx%d>", rt.preview.max_width, rt.preview.max_height),
"-quality",
rt.preview.image_quality,
string.format("PNG32:%s", tostring(tmp_file_path)),
})
:status()
if cache_img_status then
os.rename(tostring(tmp_file_path), tostring(cache_img_url))
end
end
else
-- NOTE: Case valid utf8 path, use image, svg, or magick module
local image_module = is_svg and "svg" or (is_magick and "magick" or "image")
cache_img_status, image_preload_err = require(image_module):preload(no_skip_job)
end
if not cache_img_status and image_preload_err then
ya.dbg("mediainfo", image_preload_err)
err_msg = err_msg .. (image_preload_err and (tostring(image_preload_err)) or "")
end
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,209 @@
--- @since 26.1.22
local M = {}
local using_new_height_api = ui.lines ~= nil
local const
local utils
local adobe
local audio
local image
local video
local none_media_preview
const = require(".const")
utils = require(".utils")
if using_new_height_api then
ya.dbg("mediainfo", "Using yazi version >= 26.5.6")
adobe = require(".adobe-stable")
audio = require(".audio-stable")
image = require(".image-stable")
video = require(".video-stable")
none_media_preview = require(".none-media-preview-stable")
else
ya.dbg("mediainfo", "Using yazi version <= 26.1.22")
adobe = require(".adobe-old")
audio = require(".audio-old")
image = require(".image-old")
video = require(".video-old")
none_media_preview = require(".none-media-preview-old")
end
function M:peek(job)
-- debounce peek
local start = os.clock()
ya.sleep(math.max(0, rt.preview.image_delay / 1000 + start - os.clock()))
-- Need mime to decide which module to use
if not job.mime then
return
end
local no_metadata_user_cfg = utils.get_state(const.STATE_KEY.no_metadata)
if no_metadata_user_cfg ~= nil then
job.args.no_metadata = no_metadata_user_cfg
end
local no_preview_user_cfg = utils.get_state(const.STATE_KEY.no_preview)
if no_preview_user_cfg ~= nil then
job.args.no_preview = no_preview_user_cfg
end
utils.set_states(const.STATE_KEY.cached_job_args, tostring(job.file.url), job.args)
if job.args.no_preview and job.args.no_metadata then
ya.preview_widget(job, {
ui.Clear(job.area),
})
return
end
if job.args.no_preview then
return none_media_preview:peek(job)
end
local is_video = string.find(job.mime, "^video/")
local is_audio = string.find(job.mime, "^audio/")
local is_image = string.find(job.mime, "^image/")
local is_adobe = const.seekable_mimes[job.mime]
if is_adobe then
return adobe:peek(job)
elseif is_image then
return image:peek(job)
elseif is_video then
return video:peek(job)
elseif is_audio then
return audio:peek(job)
else
return none_media_preview:peek(job)
end
end
function M:seek(job)
local h = cx.active.current.hovered
if h and h.url == job.file.url then
utils.set_state(const.STATE_KEY.units, job.units)
ya.emit("peek", {
math.max(0, cx.active.preview.skip + job.units),
only_if = job.file.url,
})
end
end
function M:preload(job)
local cache_img_url = ya.file_cache({ file = job.file, skip = 0 })
if not cache_img_url then
ya.dbg("mediainfo", "Can't access yazi cache folder")
return true
end
if not job.mime then
return false
end
local no_metadata_user_cfg = utils.get_state(const.STATE_KEY.no_metadata)
if no_metadata_user_cfg ~= nil then
job.args.no_metadata = no_metadata_user_cfg
end
local no_preview_user_cfg = utils.get_state(const.STATE_KEY.no_preview)
if no_preview_user_cfg ~= nil then
job.args.no_preview = no_preview_user_cfg
end
if job.args.no_preview and job.args.no_metadata then
return false, nil
end
if job.args.no_preview then
return none_media_preview:peek(job)
end
local is_video = string.find(job.mime, "^video/")
local is_audio = string.find(job.mime, "^audio/")
local is_image = string.find(job.mime, "^image/")
local is_adobe = const.seekable_mimes[job.mime]
if is_adobe then
return adobe:preload(job)
elseif is_image then
return image:preload(job)
elseif is_video then
return video:preload(job)
elseif is_audio then
return audio:preload(job)
else
return none_media_preview:peek(job)
end
end
function M:entry(job)
local action = job.args[1]
if action == const.ENTRY_ACTION.reset or job.args.reset then
utils.set_state(const.STATE_KEY.no_preview, nil)
utils.set_state(const.STATE_KEY.no_metadata, nil)
ya.emit("peek", {
force = true,
})
return
end
if action == const.ENTRY_ACTION.show_metadata or job.args.show_metadata then
utils.set_state(const.STATE_KEY.no_metadata, false)
ya.emit("peek", {
force = true,
})
end
if action == const.ENTRY_ACTION.show_preview or job.args.show_preview then
utils.set_state(const.STATE_KEY.no_preview, false)
ya.emit("peek", {
force = true,
})
end
if action == const.ENTRY_ACTION.hide_metadata or job.args.hide_metadata then
utils.set_state(const.STATE_KEY.no_metadata, true)
ya.emit("peek", {
force = true,
})
end
if action == const.ENTRY_ACTION.hide_preview or job.args.hide_preview then
utils.set_state(const.STATE_KEY.no_preview, true)
ya.emit("peek", {
force = true,
})
end
job.retry = job.retry or 0
if job.retry > 5 then
utils.error("Preview data is loading, try again later")
return
end
local current_file = utils.current_file()
if not current_file then
ya.sleep(0.1)
job.retry = job.retry + 1
return self:entry(job)
end
local job_args = utils.get_states(const.STATE_KEY.cached_job_args, current_file)
if not job_args then
ya.sleep(0.1)
job.retry = job.retry + 1
return self:entry(job)
end
if action == const.ENTRY_ACTION.toggle_metadata or job.args.toggle_metadata then
utils.set_state(const.STATE_KEY.no_metadata, not job_args.no_metadata)
ya.emit("peek", {
force = true,
})
end
if action == const.ENTRY_ACTION.toggle_preview or job.args.toggle_preview then
utils.set_state(const.STATE_KEY.no_preview, not job_args.no_preview)
ya.emit("peek", {
force = true,
})
end
end
return M
@@ -0,0 +1,147 @@
--- @since 26.1.22
local M = {}
local const = require(".const")
local utils = require(".utils")
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = ui.Line({
ui.Span(label .. ": "):style(ui.Style():fg("reset"):bold()),
ui.Span(value):style(th.spot.tbl_col or ui.Style():fg("blue")),
})
end
elseif str ~= "General" then
line = ui.Line({ ui.Span(str):style(th.spot.title or ui.Style():fg("green")) })
end
if line then
local line_height = ui.height
and ui.height(str, { width = max_width, ansi = true, wrap = rt.preview.wrap })
or (math.max(1, is_wrap and math.ceil(ui.width(line) / max_width) or 1))
if next_str == nil and line_height == 1 then
EOF_mediainfo = true
end
if (last_line + line_height) > job.skip then
table.insert(lines, line)
end
if (last_line + line_height) >= job.skip + limit then
last_line = job.skip + limit
EOF_mediainfo = false
break
end
last_line = last_line + line_height
end
str = next_str
end
end
if EOF_mediainfo and #lines == 0 and job.skip > 0 then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
end
utils.force_render()
-- Handle preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Clear(job.area),
ui.Text(lines):area(job.area):wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, {
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = job.area.h,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,183 @@
--- @since 26.5.6
local M = {}
local const = require(".const")
local utils = require(".utils")
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
local opt = { ansi = true, tab_size = rt.preview.tab_size, wrap = rt.preview.wrap, width = max_width }
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = label .. ": " .. value
end
elseif str ~= "General" then
line = str
end
if line then
local wrapped = ui.lines(line, opt)
local line_height = #wrapped
local from = 1
local to = math.min(line_height, job.skip + limit - last_line)
local total_rendered_text_len = 1
local total_label_rendered_len = 1
local label_total_len = label and utf8.len(label .. ": ") or 0
for j = from, to do
local current_line_components = {}
local wrapped_line_len = wrapped[j]:width() or 0
local wrapped_raw =
utils.utf8_sub(line, total_rendered_text_len, total_rendered_text_len + wrapped_line_len)
wrapped_line_len = utf8.len(wrapped_raw)
total_rendered_text_len = total_rendered_text_len + wrapped_line_len
if last_line + 1 > job.skip then
local label_raw = label_total_len - total_label_rendered_len <= 0 and ""
or utils.utf8_sub(wrapped_raw, 1, label_total_len - total_label_rendered_len)
local label_raw_len = utf8.len(label_raw)
if label_raw_len > 0 then
table.insert(
current_line_components,
ui.Span(label_raw):style(ui.Style():fg("reset"):bold())
)
total_label_rendered_len = total_label_rendered_len + label_raw_len
wrapped_raw = wrapped_raw:gsub("^" .. utils.is_literal_string(label_raw), "", 1)
end
if total_label_rendered_len >= label_total_len then
local value_raw = wrapped_raw
table.insert(
current_line_components,
ui.Span(value_raw or ""):style(
label and (th.spot.tbl_col or ui.Style():fg("blue"))
or (th.spot.title or ui.Style():fg("green"))
)
)
end
table.insert(lines, ui.Line(current_line_components))
-- last_line = last_line + 1
else
total_label_rendered_len = total_label_rendered_len + total_rendered_text_len
end
last_line = last_line + 1
if next_str == nil and not wrapped[j + 1] then
EOF_mediainfo = true
end
if last_line >= job.skip + limit then
last_line = job.skip + limit
EOF_mediainfo = false
break
end
end
end
str = next_str
end
end
if EOF_mediainfo and #lines == 0 and job.skip > 0 then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
end
utils.force_render()
-- Handle preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Clear(job.area),
ui.Text(lines):area(job.area):wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, {
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = job.area.h,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,100 @@
--- @since 26.1.22
local const = require(".const")
local M = {}
local MAX_ITEMS_IN_CACHE = 10
function M.is_valid_utf8(str)
return utf8.len(str) ~= nil
end
function M.utf8_sub(str, start_char, end_char)
local start_byte = utf8.offset(str, start_char) -- Expects start_char to be a character index
local end_byte = end_char and (utf8.offset(str, end_char + 1) or (#str + 1)) - 1 -- Expects end_char
if not start_byte then
return ""
end
return str:sub(start_byte, end_byte)
end
function M.is_literal_string(str)
return str and str:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
end
function M.path_quote(path)
if not path or tostring(path) == "" then
return path
end
local result = "'" .. string.gsub(tostring(path), "'", "'\\''") .. "'"
return result
end
M.force_render = ya.sync(function(_, _)
(ui.render or ya.render)()
end)
M.set_state = ya.sync(function(state, key, value)
state[key] = value
end)
M.get_state = ya.sync(function(state, key)
return state[key]
end)
M.set_states = ya.sync(function(state, namespace, key, value, limit_cached_items)
if not limit_cached_items then
limit_cached_items = MAX_ITEMS_IN_CACHE
end
if not state[namespace] then
state[namespace] = {}
end
local storage = state[namespace]
if limit_cached_items and #storage > limit_cached_items then
table.remove(storage, 1)
end
storage[key] = value
end)
M.get_states = ya.sync(function(state, namespace, key)
local storage = state[namespace]
if not storage then
return nil
end
if storage then
return storage[key]
end
end)
function M.read_mediainfo_cached_file(file_path)
local cached = M.get_states(const.STATE_KEY.cached_mediainfo, file_path)
if cached then
return cached
end
-- Open the file in read mode
local file = io.open(file_path, "r")
if file then
-- Read the entire file content
local content = file:read("*all")
file:close()
M.set_states(const.STATE_KEY.cached_mediainfo, file_path, content)
return content
end
end
M.current_file = ya.sync(function()
local h = cx.active.current.hovered
if not h then
return
end
return tostring(h.url)
end)
function M.error(s, ...)
ya.notify({ title = "mediainfo", content = string.format(s, ...), timeout = 3, level = "error" })
end
function M.info(s, ...)
ya.notify({ title = "mediainfo", content = string.format(s, ...), timeout = 3, level = "info" })
end
return M
@@ -0,0 +1,239 @@
--- @since 26.1.22
local M = {}
local const = require(".const")
local utils = require(".utils")
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache({
skip = job.skip > 90 and 90 or job.skip,
args = job.args,
file = job.file,
area = job.area,
})
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_job_skip = job.skip
::recalc_mediainfo_job_skip::
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = ui.Line({
ui.Span(label .. ": "):style(ui.Style():fg("reset"):bold()),
ui.Span(value):style(th.spot.tbl_col or ui.Style():fg("blue")),
})
end
elseif str ~= "General" then
line = ui.Line({ ui.Span(str):style(th.spot.title or ui.Style():fg("green")) })
end
if line then
local line_height = ui.height
and ui.height(str, { width = max_width, ansi = true, wrap = rt.preview.wrap })
or (math.max(1, is_wrap and math.ceil(ui.width(line) / max_width) or 1))
if next_str == nil and line_height == 1 then
EOF_mediainfo = true
end
if (last_line + line_height) > mediainfo_job_skip then
table.insert(lines, line)
end
if (last_line + line_height) >= mediainfo_job_skip + limit then
last_line = mediainfo_job_skip + limit
EOF_mediainfo = false
break
end
last_line = last_line + line_height
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata then
if EOF_mediainfo and #lines == 0 and mediainfo_job_skip > 0 then
if job.skip > 90 then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
else
-- NOTE: Recalculate mediainfo using cached latest valid skip value when reach the end of mediainfo output
local last_valid_mediainfo_skip = utils.get_state(const.STATE_KEY.last_valid_mediainfo_skip)
mediainfo_job_skip = last_valid_mediainfo_skip
and last_valid_mediainfo_skip[tostring(cache_img_url_no_skip)]
or math.max(0, mediainfo_job_skip - (utils.get_state(const.STATE_KEY.units) or 0))
goto recalc_mediainfo_job_skip
end
else
utils.set_state(
const.STATE_KEY.last_valid_mediainfo_skip,
{ [tostring(cache_img_url_no_skip)] = mediainfo_job_skip }
)
end
end
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
utils.force_render()
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- NOTE: Workaround case video.lua doesn't doesn't generate preview image because of `skip` overflow video duration
if not rendered_img_rect then
local prev_image_height = utils.get_state(const.STATE_KEY.prev_image_height)
image_height = prev_image_height and prev_image_height[tostring(cache_img_url_no_skip)] or 0
else
utils.set_state(const.STATE_KEY.prev_image_height, { [tostring(cache_img_url_no_skip)] = image_height })
end
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
-- NOTE: Preload image from video
local cache_img_status, video_preload_err = require("video"):preload({
skip = job.skip > 90 and 90 or job.skip,
args = job.args,
file = job.file,
area = job.area,
})
if not cache_img_status and video_preload_err then
ya.dbg("mediainfo", video_preload_err)
err_msg = err_msg .. string.format("Failed to start `%s`.\n Do you have `%s` installed?\n", "ffmpeg", "ffmpeg")
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
@@ -0,0 +1,275 @@
--- @since 26.5.6
local M = {}
local const = require(".const")
local utils = require(".utils")
function M:peek(job)
local preload_status, preload_err = self:preload(job)
-- Stop if preload failed
if not preload_status then
return
end
local cache_img_url = ya.file_cache({
skip = job.skip > 90 and 90 or job.skip,
args = job.args,
file = job.file,
area = job.area,
})
local cache_img_url_no_skip = ya.file_cache({ file = job.file, skip = 0 })
local no_metadata = job.args.no_metadata
local mediainfo_job_skip = job.skip
::recalc_mediainfo_job_skip::
local mediainfo_height = 0
local lines = {}
local limit = job.area.h
local last_line = 0
local EOF_mediainfo = true
local is_wrap = rt.preview.wrap == "yes" or rt.preview.wrap == ui.Wrap.YES
if not no_metadata then
local cache_mediainfo_path = tostring(cache_img_url_no_skip) .. const.suffix
local output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
if output then
local max_width = math.max(1, job.area.w)
if output:match("^Error:") then
job.args.force_reload_mediainfo = true
preload_status, preload_err = self:preload(job)
if not preload_status or preload_err then
return
end
output = utils.read_mediainfo_cached_file(cache_mediainfo_path)
end
output = output:gsub("\n+$", "")
local iter = output:gmatch("[^\n]*")
local str = iter()
local opt = { ansi = true, tab_size = rt.preview.tab_size, wrap = rt.preview.wrap, width = max_width }
while str ~= nil do
local next_str = iter()
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not const.skip_labels[label] then
line = label .. ": " .. value
end
elseif str ~= "General" then
line = str
end
if line then
local wrapped = ui.lines(line, opt)
local line_height = #wrapped
local from = 1
local to = math.min(line_height, mediainfo_job_skip + limit - last_line)
local total_rendered_text_len = 1
local total_label_rendered_len = 1
local label_total_len = label and utf8.len(label .. ": ") or 0
for j = from, to do
local current_line_components = {}
local wrapped_line_len = wrapped[j]:width() or 0
local wrapped_raw =
utils.utf8_sub(line, total_rendered_text_len, total_rendered_text_len + wrapped_line_len)
wrapped_line_len = utf8.len(wrapped_raw)
total_rendered_text_len = total_rendered_text_len + wrapped_line_len
if last_line + 1 > mediainfo_job_skip then
local label_raw = label_total_len - total_label_rendered_len <= 0 and ""
or utils.utf8_sub(wrapped_raw, 1, label_total_len - total_label_rendered_len)
local label_raw_len = utf8.len(label_raw)
if label_raw_len > 0 then
table.insert(
current_line_components,
ui.Span(label_raw):style(ui.Style():fg("reset"):bold())
)
total_label_rendered_len = total_label_rendered_len + label_raw_len
wrapped_raw = wrapped_raw:gsub("^" .. utils.is_literal_string(label_raw), "", 1)
end
if total_label_rendered_len >= label_total_len then
local value_raw = wrapped_raw
table.insert(
current_line_components,
ui.Span(value_raw or ""):style(
label and (th.spot.tbl_col or ui.Style():fg("blue"))
or (th.spot.title or ui.Style():fg("green"))
)
)
end
table.insert(lines, ui.Line(current_line_components))
-- last_line = last_line + 1
else
total_label_rendered_len = total_label_rendered_len + total_rendered_text_len
end
last_line = last_line + 1
if next_str == nil and not wrapped[j + 1] then
EOF_mediainfo = true
end
if last_line >= mediainfo_job_skip + limit then
last_line = mediainfo_job_skip + limit
EOF_mediainfo = false
break
end
end
end
str = next_str
end
end
mediainfo_height = math.min(limit, last_line)
end
if not no_metadata then
if EOF_mediainfo and #lines == 0 and mediainfo_job_skip > 0 then
if job.skip > 90 then
ya.emit("peek", {
math.max(0, (job.skip - (utils.get_state(const.STATE_KEY.units) or 0))),
only_if = job.file.url,
upper_bound = true,
})
return
else
-- NOTE: Recalculate mediainfo using cached latest valid skip value when reach the end of mediainfo output
local last_valid_mediainfo_skip = utils.get_state(const.STATE_KEY.last_valid_mediainfo_skip)
mediainfo_job_skip = last_valid_mediainfo_skip
and last_valid_mediainfo_skip[tostring(cache_img_url_no_skip)]
or math.max(0, mediainfo_job_skip - (utils.get_state(const.STATE_KEY.units) or 0))
goto recalc_mediainfo_job_skip
end
else
utils.set_state(
const.STATE_KEY.last_valid_mediainfo_skip,
{ [tostring(cache_img_url_no_skip)] = mediainfo_job_skip }
)
end
end
-- NOTE: Hacky way to prevent image overlap with old metadata area
if utils.get_state(const.STATE_KEY.prev_metadata_area) then
local old_metadata_area = utils.get_state(const.STATE_KEY.prev_metadata_area)
if
old_metadata_area.win_x == job.area.x
and old_metadata_area.win_y == job.area.y
and old_metadata_area.win_w == job.area.w
and old_metadata_area.win_h == job.area.h
then
ya.preview_widget(job, {
ui.Clear(ui.Rect({
x = old_metadata_area.x,
y = old_metadata_area.y,
w = old_metadata_area.w,
h = old_metadata_area.h,
})),
})
end
end
utils.force_render()
local rendered_img_rect = cache_img_url
and fs.cha(cache_img_url)
and ya.image_show(
cache_img_url,
ui.Rect({
x = job.area.x,
y = job.area.y,
w = job.area.w,
h = mediainfo_height > 0 and math.max(job.area.h - mediainfo_height, job.area.h / 2) or job.area.h,
})
)
or nil
local image_height = rendered_img_rect and rendered_img_rect.h or 0
-- NOTE: Workaround case video.lua doesn't doesn't generate preview image because of `skip` overflow video duration
if not rendered_img_rect then
local prev_image_height = utils.get_state(const.STATE_KEY.prev_image_height)
image_height = prev_image_height and prev_image_height[tostring(cache_img_url_no_skip)] or 0
else
utils.set_state(const.STATE_KEY.prev_image_height, { [tostring(cache_img_url_no_skip)] = image_height })
end
-- Handle image preload error
if preload_err then
table.insert(lines, ui.Line(tostring(preload_err)):style(th.spot.title or ui.Style():fg("red")))
end
ya.preview_widget(job, {
ui.Text(lines)
:area(ui.Rect({
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
}))
:wrap(is_wrap and ui.Wrap.YES or ui.Wrap.NO),
})
-- NOTE: Hacky way to prevent image overlap with old metadata area
utils.set_state(const.STATE_KEY.prev_metadata_area, not no_metadata and {
x = job.area.x,
y = job.area.y + image_height,
w = job.area.w,
h = job.area.h - image_height,
win_x = job.area.x,
win_y = job.area.y,
win_w = job.area.w,
win_h = job.area.h,
} or nil)
end
function M:preload(job)
local cmd = "mediainfo"
local err_msg = ""
-- NOTE: Preload image from video
local cache_img_status, video_preload_err = require("video"):preload({
skip = job.skip > 90 and 90 or job.skip,
args = job.args,
file = job.file,
area = job.area,
})
if not cache_img_status and video_preload_err then
ya.dbg("mediainfo", video_preload_err)
err_msg = err_msg .. string.format("Failed to start `%s`.\n Do you have `%s` installed?\n", "ffmpeg", "ffmpeg")
end
-- NOTE: Get mediainfo and save to cache folder
local cache_mediainfo_url = Url(tostring(ya.file_cache({ file = job.file, skip = 0 })) .. const.suffix)
local cache_mediainfo_cha = fs.cha(cache_mediainfo_url)
-- Case peek function called preload to refetch mediainfo
if cache_mediainfo_cha and not job.args.force_reload_mediainfo then
return true, err_msg ~= "" and ("Error: " .. err_msg) or nil
end
local output, err
local is_valid_utf8_path = utils.is_valid_utf8(tostring(job.file.path or job.file.cache or job.file.url))
if is_valid_utf8_path then
output, err = Command(cmd)
:arg({ tostring(job.file.path or job.file.cache or job.file.url.path or job.file.url) })
:output()
else
cmd = "cd "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url.path or job.file.url).parent))
.. " && "
.. cmd
.. " "
.. utils.path_quote(tostring((job.file.path or job.file.cache or job.file.url).name))
output, err = Command(const.SHELL):arg({ "-c", cmd }):output()
end
if err then
ya.dbg("mediainfo", tostring(err))
err_msg = err_msg .. string.format("Failed to start `%s`. \n Do you have `%s` installed?\n", cmd, cmd)
end
return fs.write(
cache_mediainfo_url,
(err_msg ~= "" and ("Error: " .. err_msg) or "") .. (output and output.stdout or "")
)
end
return M
+15
View File
@@ -0,0 +1,15 @@
[plugin]
prepend_preloaders = [
{ mime = "{audio,video,image}/*", run = "mediainfo" },
{ mime = "application/subrip", run = "mediainfo" },
]
prepend_previewers = [
{ mime = "{audio,video,image}/*", run = "mediainfo" },
{ mime = "application/subrip", run = "mediainfo" },
]
[preview]
max_width = 2000
max_height = 2000
image_filter = "lanczos3"