2023-12-26 14:48:33 +03:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M:peek()
|
2024-01-11 20:35:57 +03:00
|
|
|
local cache = ya.file_cache(self)
|
|
|
|
if not cache then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2023-12-26 14:48:33 +03:00
|
|
|
if self:preload() == 1 then
|
2024-01-11 20:35:57 +03:00
|
|
|
ya.image_show(cache, self.area)
|
2023-12-26 14:48:33 +03:00
|
|
|
ya.preview_widgets(self, {})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:seek(units)
|
|
|
|
local h = cx.active.current.hovered
|
|
|
|
if h and h.url == self.file.url then
|
|
|
|
local step = ya.clamp(-1, units, 1)
|
2024-04-22 09:18:00 +03:00
|
|
|
ya.manager_emit("peek", { math.max(0, cx.active.preview.skip + step), only_if = self.file.url })
|
2023-12-26 14:48:33 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:preload()
|
2024-01-11 20:35:57 +03:00
|
|
|
local cache = ya.file_cache(self)
|
2024-01-15 13:30:50 +03:00
|
|
|
if not cache or fs.cha(cache) then
|
2023-12-26 14:48:33 +03:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2023-12-30 18:41:41 +03:00
|
|
|
local output = Command("pdftoppm")
|
2023-12-26 14:48:33 +03:00
|
|
|
:args({ "-singlefile", "-jpeg", "-jpegopt", "quality=75", "-f", tostring(self.skip + 1), tostring(self.file.url) })
|
|
|
|
:stdout(Command.PIPED)
|
|
|
|
:stderr(Command.PIPED)
|
|
|
|
:output()
|
|
|
|
|
2024-01-01 13:28:38 +03:00
|
|
|
if not output then
|
|
|
|
return 0
|
|
|
|
elseif not output.status:success() then
|
2023-12-26 14:48:33 +03:00
|
|
|
local pages = tonumber(output.stderr:match("the last page %((%d+)%)")) or 0
|
|
|
|
if self.skip > 0 and pages > 0 then
|
2024-04-22 09:18:00 +03:00
|
|
|
ya.manager_emit("peek", { math.max(0, pages - 1), only_if = self.file.url, upper_bound = true })
|
2023-12-26 14:48:33 +03:00
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return fs.write(cache, output.stdout) and 1 or 2
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|