diff --git a/lua/telescope/_extensions/media_files.lua b/lua/telescope/_extensions/media_files.lua index 5e0d3c8..2258ddc 100644 --- a/lua/telescope/_extensions/media_files.lua +++ b/lua/telescope/_extensions/media_files.lua @@ -16,6 +16,7 @@ local pickers = require("telescope.pickers") local previewers = require("telescope.previewers") local config = require("telescope.config") +local Job = require("plenary.job") local defaulter = utils.make_default_callable local action_state = require("telescope.actions.state") @@ -45,22 +46,30 @@ end local base_directory = "" +local PIDS = {} local media_preview = defaulter(function(options) - return previewers.new_termopen_previewer({ - get_command = function(entry) - local filepath = vim.trim(entry.value) - local preview = options.get_preview_window() - if filepath:len() == 0 then - return { "echo", "" } + return previewers.new({ + preview_fn = function(self, entry, status) + for _, PID in pairs(PIDS) do + vim.loop.kill(PID, 9) end - return { + + local preview = options.get_preview_window() + local ueberzug = Job:new({ base_directory .. "/scripts/view.py", - filepath, + vim.trim(entry.value), preview.col + options.geometry.x, preview.line + options.geometry.y, preview.width + options.geometry.width, preview.height + options.geometry.height, - } + }) + ueberzug:start() + table.insert(PIDS, ueberzug.pid) + end, + teardown = function(self) + for _, pid in pairs(PIDS) do + vim.loop.kill(pid, 9) + end end, }) end, {}) diff --git a/scripts/view.py b/scripts/view.py index 3d44581..dc63c5c 100755 --- a/scripts/view.py +++ b/scripts/view.py @@ -1,32 +1,25 @@ #!/usr/bin/env python -import sys -import time +from sys import argv +from time import sleep -import ueberzug.lib.v0 as ueberzug +from ueberzug.lib.v0 import Canvas, ScalerOption, Visibility -if __name__ == "__main__": - with ueberzug.Canvas() as canvas: - path = sys.argv[1] - x = int(sys.argv[2]) - y = int(sys.argv[3]) - width = int(sys.argv[4]) - height = int(sys.argv[5]) +with Canvas() as canv: + place = canv.create_placement( + identifier="tele.media.files", + path=argv[1], + x=argv[2], + y=argv[3], + width=argv[4], + height=argv[5], + scaler=ScalerOption.CONTAIN.value, + visibility=Visibility.VISIBLE + ) - placement = canvas.create_placement( - identifier="telescope-media-files.nvim", - width=width, - height=height, - x=x, - y=y, - scaler=ueberzug.ScalerOption.CONTAIN.value, - visibility=ueberzug.Visibility.VISIBLE, - path=path - ) - - while True: - with canvas.lazy_drawing: - pass - time.sleep(1) + while True: + with canv.lazy_drawing: + pass + sleep(1) # vim:filetype=python