refactoring

This commit is contained in:
Matt Nish-Lapidus 2025-03-21 16:50:54 -04:00
parent 74ae1150a3
commit 69400c1aa3
142 changed files with 11 additions and 70 deletions

View file

@ -0,0 +1,19 @@
Copyright (c) 2024 Lauri Niskanen
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.

View file

@ -0,0 +1,26 @@
> [!NOTE]
> Please use **boydaihungst**'s fork for the latest version of this plugin:
> https://github.com/boydaihungst/mediainfo.yazi
# mediainfo.yazi
This is a Yazi plugin for previewing media files. The preview shows thumbnail
using `ffmpegthumbnailer` if available and media metadata using `mediainfo`.
## Installation
Install the plugin:
```
ya pack -a Ape/mediainfo
```
Create `~/.config/yazi/yazi.toml` and add:
```
[plugin]
prepend_previewers = [
{ mime = "{image,audio,video}/*", run = "mediainfo"},
{ mime = "application/x-subrip", run = "mediainfo"},
]
```

View file

@ -0,0 +1,111 @@
local 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,
}
local M = {}
function M:peek()
local image_height = 0
if self:preload() == 1 then
local cache = ya.file_cache(self)
if cache and fs.cha(cache).length > 0 then
image_height = ya.image_show(cache, self.area).h
end
end
local cmd = "mediainfo"
local output, code = Command(cmd)
:args({ tostring(self.file.url) })
:stdout(Command.PIPED)
:output()
local lines = {}
if output then
local i = 0
for str in output.stdout:gmatch("[^\n]*") do
local label, value = str:match("(.*[^ ]) +: (.*)")
local line
if label then
if not skip_labels[label] then
line = ui.Line({
ui.Span(label .. ": "):bold(),
ui.Span(value),
})
end
elseif str ~= "General" then
line = ui.Line({ ui.Span(str):underline() })
end
if line then
if i >= self.skip then
table.insert(lines, line)
end
local max_width = math.max(1, self.area.w - 3)
i = i + math.max(1, math.ceil(line:width() / max_width))
end
end
else
local error = string.format("Spawn `%s` command returns %s", cmd, code)
table.insert(lines, ui.Line(error))
end
ya.preview_widgets(self, {
ui.Paragraph(
ui.Rect({
x = self.area.x,
y = self.area.y + image_height,
w = self.area.w,
h = self.area.h - image_height,
}),
lines
):wrap(ui.Paragraph.WRAP),
})
end
function M:seek(units)
local h = cx.active.current.hovered
if h and h.url == self.file.url then
local step = math.floor(units * self.area.h / 10)
ya.manager_emit("peek", {
math.max(0, cx.active.preview.skip + step),
only_if = self.file.url,
})
end
end
function M:preload()
local cache = ya.file_cache(self)
if not cache or fs.cha(cache) then
return 1
end
local cmd = "ffmpegthumbnailer"
local child, code = Command(cmd):args({
"-q", "6",
"-c", "jpeg",
"-i", tostring(self.file.url),
"-o", tostring(cache),
"-t", "5",
"-s", tostring(PREVIEW.max_width),
}):spawn()
if not child then
ya.err(string.format("spawn `%s` command returns %s", cmd, code))
return 0
end
local status = child:wait()
return status and status.success and 1 or 2
end
return M