From 7584fcff23954cb01db3782d640d7b2aa9138cc8 Mon Sep 17 00:00:00 2001 From: dharmx Date: Wed, 10 May 2023 16:41:50 +0530 Subject: [PATCH] chore: added silent option to ueberzug.lua --- lua/telescope/_extensions/media/init.lua | 17 ++++- lua/telescope/_extensions/media/preview.lua | 70 +++++++++++++++++--- lua/telescope/_extensions/media/ueberzug.lua | 19 +++--- 3 files changed, 89 insertions(+), 17 deletions(-) diff --git a/lua/telescope/_extensions/media/init.lua b/lua/telescope/_extensions/media/init.lua index 1401bac..5aebe78 100644 --- a/lua/telescope/_extensions/media/init.lua +++ b/lua/telescope/_extensions/media/init.lua @@ -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", diff --git a/lua/telescope/_extensions/media/preview.lua b/lua/telescope/_extensions/media/preview.lua index 5b3d694..8ac2580 100644 --- a/lua/telescope/_extensions/media/preview.lua +++ b/lua/telescope/_extensions/media/preview.lua @@ -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 @@ -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 @@ -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) diff --git a/lua/telescope/_extensions/media/ueberzug.lua b/lua/telescope/_extensions/media/ueberzug.lua index 1a62dc0..e30f98c 100644 --- a/lua/telescope/_extensions/media/ueberzug.lua +++ b/lua/telescope/_extensions/media/ueberzug.lua @@ -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 = table.concat(this:stderr_result(), "\n") + if errors ~= "" then + vim.notify(string.format("ueberzug exited with code %s and signal %s.\n\n%s", code, signal, errors)) + end + end), }) self.__index = self @@ -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,