Skip to content

Commit

Permalink
chore: added silent option to ueberzug.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
dharmx committed May 10, 2023
1 parent 567237d commit a50804f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
17 changes: 16 additions & 1 deletion lua/telescope/_extensions/media/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@ local V = vim.fn
local _TelescopeMediaConfig = {
backend = "none",
move = false,
backend_options = {
chafa = {
move = false
},
catimg = {
move = false
},
viu = {
move = false
},
ueberzug = {
xmove = -1,
ymove = -2,
}
},
on_confirm = canned.single.copy_path,
on_confirm_muliple = canned.multiple.bulk_copy,
cache_path = "/tmp/tele.media.cache",
cache_path = "/tmp/media",
preview_title = "",
results_title = "",
prompt_title = "Media",
Expand Down
70 changes: 62 additions & 8 deletions lua/telescope/_extensions/media/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ local function _run(command, buffer, options, extension)
if code == 0 then
pcall(A.nvim_buf_set_lines, buffer, 0, -1, false, result)
A.nvim_buf_set_option(buffer, "filetype", if_nil(extension, "text"))
else _dial(buffer, options.preview.winid, "PREVIEWER ERROR", options.preview.fill.error) end
else _dial(buffer, options.preview.winid, "PREVIEWER TIMED OUT", options.preview.fill.timeout) end
else
_dial(buffer, options.preview.winid, "PREVIEWER ERROR", options.preview.fill.error)
end
else
_dial(buffer, options.preview.winid, "PREVIEWER TIMED OUT", options.preview.fill.timeout)
end
return false
end

Expand All @@ -43,7 +47,45 @@ local function redirect(buffer, extension, absolute, options)
-- TODO: This looks vile. Cleanup is required.
if B.readelf and vim.tbl_contains({ "x-executable", "x-pie-executable", "x-sharedlib" }, _mime[2]) then
return _run(B.readelf + absolute, buffer, options)
elseif vim.tbl_contains({ "a", "ace", "alz", "arc", "arj", "bz", "bz2", "cab", "cpio", "deb", "gz", "jar", "lha", "lz", "lzh", "lzma", "lzo", "rpm", "rz", "t7z", "tar", "tbz", "tbz2", "tgz", "tlz", "txz", "tZ", "tzo", "war", "xpi", "xz", "Z", "zip" }, extension) then
elseif
-- Huge list of archive filetypes/extensions. {{{
vim.tbl_contains({
"a",
"ace",
"alz",
"arc",
"arj",
"bz",
"bz2",
"cab",
"cpio",
"deb",
"gz",
"jar",
"lha",
"lz",
"lzh",
"lzma",
"lzo",
"rpm",
"rz",
"t7z",
"tar",
"tbz",
"tbz2",
"tgz",
"tlz",
"txz",
"tZ",
"tzo",
"war",
"xpi",
"xz",
"Z",
"zip",
}, extension)
-- }}}
then
local command = rifle.orders(absolute, "bsdtar", "atool")
if command then _run(command, buffer, options) end
elseif extension == "rar" and B.unrar then
Expand Down Expand Up @@ -114,15 +156,27 @@ local function _filetype_hook(filepath, buffer, options)

if handler then
local _cache
-- stylua: ignore start
if options.move and extension == "gif" and vim.tbl_contains({ "chafa", "viu", "ueberzug" }, options.backend) then _cache = absolute
else _cache = handler(absolute, options.cache_path, options) end
if
extension == "gif"
and vim.tbl_contains({ "chafa", "viu", "catimg" }, options.backend)
and options.backend_options[options.backend]
and options.backend_options[options.backend].move
then
_cache = absolute
else
_cache = handler(absolute, options.cache_path, options)
end
if _cache == NULL then return redirect(buffer, extension, absolute, options) end
-- stylua: ignore end

local win = options.get_preview_window()
if options.backend == "ueberzug" then
options._ueberzug:send({ path = _cache, x = win.col - 1, y = win.line, width = win.width, height = win.height })
options._ueberzug:send({
path = _cache,
x = win.col + options.backend_options.ueberzug.xmove,
y = win.line + options.backend_options.ueberzug.ymove,
width = win.width,
height = win.height,
})
elseif options.backend == "viu" then
if not B.viu then error("viu isn't in PATH.", ERROR) end
mutil.termopen(buffer, B.viu + _cache)
Expand Down
23 changes: 14 additions & 9 deletions lua/telescope/_extensions/media/ueberzug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ local function _tail(fifo, options)
})
end

function Ueberzug:new(fifo)
function Ueberzug:new(fifo, silent)
fifo = Path:new(fifo)
fifo:touch({ parents = true })

local args = { "layer", "--parser", "json" }
if silent then table.insert(args, 1, "--silent") end
local ueberzug_task = Job:new({
command = "ueberzug",
args = {
"--silent",
"layer",
"--parser",
"json",
},
args = args,
writer = _tail(fifo.filename),
on_exit = vim.schedule_wrap(function(this, code, signal)
local errors = "```\n" .. table.concat(this:stderr_result(), "\n") .. "```"
if errors ~= "" then
vim.notify(string.format("# ueberzug exited with code `%s` and signal `%s`.\n%s", code, signal, errors))
end
end),
})

self.__index = self
Expand All @@ -53,7 +56,7 @@ end
function Ueberzug:send(message)
local defaults = {
action = "add",
identifier = "tele.media.fifo",
identifier = "media",
x = 0,
y = 0,
width = 100,
Expand All @@ -68,6 +71,8 @@ function Ueberzug:send(message)
self.fifo:write((vim.json.encode(message):gsub("\\", "")) .. "\n", "a")
end

function Ueberzug:hide() self:send({ path = vim.NIL, x = 1, y = 1, width = 1, height = 1 }) end
function Ueberzug:hide()
-- self:send({ path = vim.NIL, x = 1, y = 1, width = 1, height = 1 })
end

return Ueberzug

0 comments on commit a50804f

Please sign in to comment.