diff --git a/lua/lazyvim/plugins/extras/editor/medias.lua b/lua/lazyvim/plugins/extras/editor/medias.lua index 6830a932..17431fdf 100644 --- a/lua/lazyvim/plugins/extras/editor/medias.lua +++ b/lua/lazyvim/plugins/extras/editor/medias.lua @@ -1,3 +1,23 @@ +local findCmd = "" +-- Use the simple Set that implemented by a table to avoid duplicates; it only contains the methods add and toList +local supportedFiletypeSet = { _data = {} } + +function supportedFiletypeSet:add(values) + if type(values) == "string" then + values = { values } + end + + for _, value in ipairs(values) do + if not self._data[value] then + self._data[value] = 1 + end + end +end + +function supportedFiletypeSet:toList() + return vim.tbl_keys(self._data) +end + return { { "nvim-telescope/telescope.nvim", @@ -5,12 +25,19 @@ return { { "nvim-telescope/telescope-media-files.nvim", event = "VeryLazy", - config = function(_, opts) + enabled = function() if not LazyVim.has("telescope.nvim") then - return + return false end - -- https://github.com/nvim-telescope/telescope-media-files.nvim?tab=readme-ov-file#prerequisites + local function sendPrerequisitesError(msg) + LazyVim.warn( + "You have enabled extras.editor.medias, see (:`LazyExtras`). \n\n" + .. msg + .. "\n\nMore information can be found at: " + .. "[nvim-telescope/telescope-media-files.nvim](https://github.com/nvim-telescope/telescope-media-files.nvim?tab=readme-ov-file#prerequisites)." + ) + end local prerequisites = { required = { { cmd = "chafa", name = "Chafa", filetypes = { "png", "jpg", "jpeg", "webp" } }, @@ -28,28 +55,18 @@ return { { cmd = "fontpreview", name = "fontpreview", filetypes = { "ttf", "otf" } }, }, } - local function sendPrerequisitesError(msg) - LazyVim.error( - msg - .. "\nMore information can be found at: " - .. "https://github.com/nvim-telescope/telescope-media-files.nvim?tab=readme-ov-file#prerequisites" - ) - end - - local supportedFiletypes = {} -- Ensure all required prerequisites are installed for _, tool in ipairs(prerequisites.required) do if vim.fn.executable(tool.cmd) == 0 then - sendPrerequisitesError("Required prerequisite not installed: " .. tool.name) - return + sendPrerequisitesError(string.format("The required prerequisite not installed: `%s`", tool.name)) + return false else - supportedFiletypes = vim.tbl_extend("force", supportedFiletypes, tool.filetypes) + supportedFiletypeSet:add(tool.filetypes) end end -- Verify that at least one of the required find commands is installed - local findCmd = "" for _, tool in ipairs(prerequisites.findCmd) do if vim.fn.executable(tool.cmd) == 1 then findCmd = tool.cmd @@ -62,22 +79,24 @@ return { "None of the required 'find cmd' prerequisites is installed.\n" .. "Please install at least one of the following: `rd`, `rg`, or `find`." ) - return + return false end -- Check optional prerequisites and include their filetypes if installed for _, tool in ipairs(prerequisites.optional) do if vim.fn.executable(tool.cmd) == 1 then - supportedFiletypes = vim.tbl_extend("force", supportedFiletypes, tool.filetypes) + supportedFiletypeSet:add(tool.filetypes) end end - -- Setup media_files extension for telescope + return true + end, + config = function(_, opts) LazyVim.on_load("telescope.nvim", function() local ok, err = pcall(require("telescope").load_extension, "media_files") if ok then opts.extensions = vim.tbl_extend("force", opts.extensions or {}, { - media_files = { filetypes = supportedFiletypes, find_cmd = findCmd }, + media_files = { filetypes = supportedFiletypeSet:toList(), find_cmd = findCmd }, }) else LazyVim.error("Failed to load the telescope extension for `telescope-media-files.nvim`:\n" .. err)