Skip to content

Commit

Permalink
♻ Refactor: Move telescope hologram to module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyen-Hoang-Nam committed Jul 9, 2022
1 parent 52dc2b6 commit f717eba
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 566 deletions.
80 changes: 80 additions & 0 deletions lua/setup/telescope/hologram.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
local previewers = require("telescope.previewers")
local utils = require("utils.core")

local M = {}

local is_hologram_preview = false
local support_filetype = { "png" } -- TODO: Support jpg, jpeg, webp, avif
local hologram_image = nil
local last_file_path = "" -- TODO: Cache file path to stop hologram reload image

local cache_win_position = false
local hologram_row = 0
local hologram_col = 0
local preview_win_position = 0
local current_win_position = 0
-- local preview_win_height = 0
-- local preview_win_width = 0

local is_support_filetype = function(filetype)
return vim.tbl_contains(support_filetype, filetype)
end

local delete_hologram = function()
hologram_image:delete({ free = true })
is_hologram_preview = false
end

local set_hologram_row_col = function(opts)
if not cache_win_position then
preview_win_position = vim.api.nvim_win_get_position(opts.winid)
current_win_position = vim.api.nvim_win_get_position(0)
-- preview_win_height = vim.api.nvim_win_get_height(opts.winid)
-- preview_win_width = vim.api.nvim_win_get_width(opts.winid)

cache_win_position = true
end

local cursor_win_position = vim.api.nvim_win_get_cursor(0)
hologram_row = preview_win_position[1] - current_win_position[1] - cursor_win_position[1] + 1
hologram_col = preview_win_position[2] - current_win_position[2] - cursor_win_position[2] + 2
end

local create_hologram = function(filepath)
hologram_image = require("hologram.image"):new({
buf = 0,
source = filepath,
row = hologram_row,
col = hologram_col,
})
hologram_image:transmit()

is_hologram_preview = true
end

-- NOTE: Use Hologram to show image
M.buffer_previewer_maker = function(filepath, bufnr, opts)
-- NOTE: Clear image when preview other file
if is_hologram_preview and last_file_path ~= filepath then
delete_hologram()
end

last_file_path = filepath

local filetype = utils.file_extension(filepath)
if is_support_filetype(filetype) then
set_hologram_row_col(opts)

create_hologram(filepath)
else
previewers.buffer_previewer_maker(filepath, bufnr, opts)
end
end

M.teardown = function(_)
if is_hologram_preview then
delete_hologram()
end
end

return M
63 changes: 3 additions & 60 deletions lua/setup/telescope/init.lua
Original file line number Diff line number Diff line change
@@ -1,66 +1,9 @@
local telescope = require("telescope")
local previewers = require("telescope.previewers")
local actions = require("telescope.actions")
local utils = require("utils.core")
local buffer_previewer = require("setup.telescope.buffer_previewer")
local hologram = require("setup.telescope.hologram")

local is_hologram_preview = false
local support_filetype = { "png" } -- TODO: Support jpg, jpeg, webp, avif
local hologram_image

local is_support_filetype = function(filetype)
return vim.tbl_contains(support_filetype, filetype)
end

-- NOTE: Use Hologram to show image
local buffer_previewer_maker = function(filepath, bufnr, opts)
-- NOTE: Clear image when preview other file
if is_hologram_preview then
hologram_image:delete({ free = true })
is_hologram_preview = false
end

local filetype = utils.file_extension(filepath)
if is_support_filetype(filetype) then
local preview_win_position = vim.api.nvim_win_get_position(opts.winid)
local current_win_position = vim.api.nvim_win_get_position(0)
local cursor_win_position = vim.api.nvim_win_get_cursor(0)
local preview_win_height = vim.api.nvim_win_get_height(opts.winid)
local preview_win_width = vim.api.nvim_win_get_width(opts.winid)

-- if filetype == "jpg" or filetype == "jpeg" or filetype == "webp" or filetype == "avif" then
-- local filename = "/tmp/" .. require("utils.md5").sumhexa(filepath) .. ".png"

-- vim.loop.spawn("convert", {
-- args = { filepath, filename },
-- }, function()
-- -- print(filename)
-- end)

-- filepath = filename
-- end

hologram_image = require("hologram.image"):new({
buf = 0,
source = filepath,
row = preview_win_position[1] - current_win_position[1] - cursor_win_position[1] + 1,
col = preview_win_position[2] - current_win_position[2] - cursor_win_position[2] + 2,
})

hologram_image:transmit({ column = preview_win_width, row = preview_win_height })

is_hologram_preview = true
else
previewers.buffer_previewer_maker(filepath, bufnr, opts)
end
end

buffer_previewer.teardown = function(_)
if is_hologram_preview then
hologram_image:delete({ free = true })
is_hologram_preview = false
end
end
buffer_previewer.teardown = hologram.teardown

telescope.setup({
defaults = {
Expand All @@ -70,7 +13,7 @@ telescope.setup({
horizontal = { mirror = false, preview_width = 0.5 },
},

buffer_previewer_maker = buffer_previewer_maker,
buffer_previewer_maker = hologram.buffer_previewer_maker,

mappings = {
i = {
Expand Down
Loading

0 comments on commit f717eba

Please sign in to comment.