From a0d0658a16a67d1f57ceee606f3403770ad24abb Mon Sep 17 00:00:00 2001 From: David Ibia Date: Thu, 8 Feb 2024 22:51:47 +0100 Subject: [PATCH] feat(hologram.lua): add support for hologram plugin to display and handle image previews in Neovim This commit adds two new files: `hologram.lua` and `hologram.lua`. The `hologram.lua` file contains the configuration for the `hologram.nvim` plugin. It is responsible for setting up the plugin and linking it to the `absolute.after.hologram` file. The `hologram.lua` file contains the implementation for handling image previews using the `hologram` and `image` modules. It sets up autocmds to trigger the `handle_image_preview` function when entering or opening a buffer with an image file. The function creates a new image object and displays it for 5 seconds before deleting it. There is also a `handle_image_preview_close` function that is triggered when leaving or closing a buffer with an image file, but it is currently commented out. These changes were made to enhance the functionality of Neovim by allowing users to preview images within the editor using the `hologram.nvim` plugin. --- lua/absolute/after/hologram.lua | 57 +++++++++++++++++++++++++++++++ lua/absolute/plugins/hologram.lua | 6 ++++ 2 files changed, 63 insertions(+) create mode 100644 lua/absolute/after/hologram.lua create mode 100644 lua/absolute/plugins/hologram.lua diff --git a/lua/absolute/after/hologram.lua b/lua/absolute/after/hologram.lua new file mode 100644 index 0000000..54e8545 --- /dev/null +++ b/lua/absolute/after/hologram.lua @@ -0,0 +1,57 @@ +local hologram = require("hologram") +local image = require("hologram.image") + +-- Image should appear below this line, then disappear after 5 seconds +hologram.setup({ + auto_display = true, +}) + +--[[ +id: (number) autocommand id +event: (string) name of the triggered event |autocmd-events| +group: (number|nil) autocommand group id, if any +match: (string) expanded value of || +buf: (number) expanded value of || +file: (string) expanded value of || +data: (any) arbitrary data passed |nvim_exec_autocmds()| ]] + +local new_image = nil + +local function handle_image_preview(arguments) + -- get the file path + local file = arguments.file + local buf = arguments.buf + + --[[ + print("Hologram activated") + print("File: " .. file) ]] + + new_image = image:new(file, {}):display(5, 0, buf, {}) + + vim.defer_fn(function() + new_image:delete(0, { free = true }) + end, 10000) +end + +local function handle_image_preview_close() + -- Check if New Image is not nil explicitly + + -- if new_image == nil then + -- return + -- end + -- + -- new_image:delete(0, { free = true }) + -- new_image = nil +end + +local image_pattern = { "*.png", ".jpg", ".jpeg", ".gif" } + +vim.api.nvim_create_autocmd({ "BufEnter", "BufWinEnter" }, { + pattern = image_pattern, + callback = handle_image_preview, +}) + +vim.api.nvim_create_autocmd({ "BufLeave", "BufWinLeave" }, { + pattern = image_pattern, + callback = handle_image_preview_close, +}) diff --git a/lua/absolute/plugins/hologram.lua b/lua/absolute/plugins/hologram.lua new file mode 100644 index 0000000..da791db --- /dev/null +++ b/lua/absolute/plugins/hologram.lua @@ -0,0 +1,6 @@ +return { + "edluffy/hologram.nvim", + config = function() + require("absolute.after.hologram") + end, +}