style(hologram.lua): fix indentation for consistency and readability

feat(hologram.lua): change image_pattern to start with a dot for file extensions
feat(hologram.lua): add support for handling image previews on buffer enter and leave events
This commit is contained in:
David Ibia
2024-02-26 14:37:14 +01:00
parent ba39d9bfe4
commit c4e74ff5e1

View File

@@ -3,7 +3,7 @@ local image = require("hologram.image")
-- Image should appear below this line, then disappear after 5 seconds -- Image should appear below this line, then disappear after 5 seconds
hologram.setup({ hologram.setup({
auto_display = true, auto_display = true,
}) })
--[[ --[[
@@ -18,40 +18,40 @@ data: (any) arbitrary data passed |nvim_exec_autocmds()| ]]
local new_image = nil local new_image = nil
local function handle_image_preview(arguments) local function handle_image_preview(arguments)
-- get the file path -- get the file path
local file = arguments.file local file = arguments.file
local buf = arguments.buf local buf = arguments.buf
--[[ --[[
print("Hologram activated") print("Hologram activated")
print("File: " .. file) ]] print("File: " .. file) ]]
new_image = image:new(file, {}):display(5, 0, buf, {}) new_image = image:new(file, {}):display(5, 0, buf, {})
vim.defer_fn(function() vim.defer_fn(function()
new_image:delete(0, { free = true }) new_image:delete(0, { free = true })
end, 10000) end, 10000)
end end
local function handle_image_preview_close() local function handle_image_preview_close()
-- Check if New Image is not nil explicitly -- Check if New Image is not nil explicitly
-- if new_image == nil then -- if new_image == nil then
-- return -- return
-- end -- end
-- --
-- new_image:delete(0, { free = true }) -- new_image:delete(0, { free = true })
-- new_image = nil -- new_image = nil
end end
local image_pattern = { "*.png", ".jpg", ".jpeg", ".gif" } local image_pattern = { ".png", ".jpg", ".jpeg", ".gif" }
vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, { vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, {
pattern = image_pattern, pattern = image_pattern,
callback = handle_image_preview, callback = handle_image_preview,
}) })
vim.api.nvim_create_autocmd({ "BufLeave", "BufWinLeave" }, { vim.api.nvim_create_autocmd({ "BufLeave", "BufWinLeave" }, {
pattern = image_pattern, pattern = image_pattern,
callback = handle_image_preview_close, callback = handle_image_preview_close,
}) })