From d481aeb5097694031f52fe430bf1448877806219 Mon Sep 17 00:00:00 2001 From: arthur Date: Sat, 15 Jun 2024 22:43:49 +0800 Subject: [PATCH] move the telecope media supported to extra.editor.telescope --- lua/lazyvim/plugins/extras/editor/medias.lua | 109 ------------------ .../plugins/extras/editor/telescope.lua | 103 +++++++++++++++++ 2 files changed, 103 insertions(+), 109 deletions(-) delete mode 100644 lua/lazyvim/plugins/extras/editor/medias.lua diff --git a/lua/lazyvim/plugins/extras/editor/medias.lua b/lua/lazyvim/plugins/extras/editor/medias.lua deleted file mode 100644 index 72de101a..00000000 --- a/lua/lazyvim/plugins/extras/editor/medias.lua +++ /dev/null @@ -1,109 +0,0 @@ -return { - { - "nvim-telescope/telescope.nvim", - optional = true, - dependencies = { - { - "nvim-telescope/telescope-media-files.nvim", - enabled = function() - if not vim.tbl_contains(LazyVim.config.json.data.extras, "lazyvim.plugins.extras.editor.medias") then - return false - end - - local function disableWithWarnMessage(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)." - ) - return false - end - - -- Check that the required prerequisites are installed. - if vim.fn.executable("chafa") == 0 then - return disableWithWarnMessage("The required prerequisite not installed: `Chafa`") - end - - -- Check that at least one prerequisite is installed. - local hasFindCmd = vim.tbl_contains({ "find", "fd", "rg" }, function(cmd) - return vim.fn.executable(cmd) == 1 - end, { predicate = true }) - - if not hasFindCmd then - return disableWithWarnMessage( - "None of the required 'find cmd' prerequisites is installed.\n" - .. "Please install at least one of the following: `rd`, `rg`, or `find`." - ) - end - - return true - end, - config = function() - local findCmd = "" - --- @class Set A simple Set implementation using a table to avoid duplicates; it only contains the methods add and toList - --- @field _data table Table to hold the set data. - local supportedFiletypeSet = { _data = {} } - - function supportedFiletypeSet:add(values) - values = type(values) == "string" and { values } or values - - 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 - - -- Find the installed 'find' command - for _, cmd in ipairs({ "find", "fd", "rg" }) do - if vim.fn.executable(cmd) == 1 then - findCmd = cmd - break - end - end - - -- Add the supported file types if the tool is installed - local tools = { - { cmd = "chafa", name = "Chafa", filetypes = { "png", "jpg", "jpeg", "webp" } }, - { cmd = "convert", name = "ImageMagick", filetypes = { "svg" } }, - { cmd = "ffmpegthumbnailer", name = "ffmpegthumbnailer", filetypes = { "mp4", "webm" } }, - { cmd = "pdftoppm", name = "pdftoppm", filetypes = { "pdf" } }, - { cmd = "epub-thumbnailer", name = "epub-thumbnailer", filetypes = { "epub" } }, - { cmd = "fontpreview", name = "fontpreview", filetypes = { "ttf", "otf" } }, - } - for _, tool in ipairs(tools) do - if vim.fn.executable(tool.cmd) == 1 then - supportedFiletypeSet:add(tool.filetypes) - end - end - - -- We checked that all required prerequisites were installed in the enabled function, but still ensure this again before setup. - -- Set up the extension for media files when the telescope is loaded. - local mediaFilesExtension = { - filetypes = supportedFiletypeSet:toList(), - find_cmd = findCmd, - } - if mediaFilesExtension.find_cmd and #mediaFilesExtension.filetypes > 0 then - LazyVim.on_load("telescope.nvim", function() - local ok, telescope = pcall(require, "telescope") - if ok then - telescope.extensions["media_files"] = - vim.tbl_extend("force", telescope.extensions["media_files"] or {}, mediaFilesExtension) - else - LazyVim.error("Failed to load telescope.nvim") - end - end) - end - end, - keys = { - { "fP", "Telescope media_files", desc = "Find Media Files" }, - }, - }, - }, - }, -} diff --git a/lua/lazyvim/plugins/extras/editor/telescope.lua b/lua/lazyvim/plugins/extras/editor/telescope.lua index 11cfef1b..2215bb93 100644 --- a/lua/lazyvim/plugins/extras/editor/telescope.lua +++ b/lua/lazyvim/plugins/extras/editor/telescope.lua @@ -222,6 +222,109 @@ return { end, }, + -- Media files for Telescope + -- Ensure the required prerequisites are installed. + { + "nvim-telescope/telescope.nvim", + optional = true, + dependencies = { + { + "nvim-telescope/telescope-media-files.nvim", + enabled = function() + -- Check that the required prerequisites are installed. + if vim.fn.executable("chafa") == 0 then + return false + end + + -- Check that at least one prerequisite is installed. + local hasFindCmd = vim.tbl_contains({ "find", "fd", "rg" }, function(cmd) + return vim.fn.executable(cmd) == 1 + end, { predicate = true }) + + if not hasFindCmd then + return false + end + + return true + end, + config = function() + local findCmd = "" + --- @class Set A simple Set implementation using a table to avoid duplicates; it only contains the methods add and toList + --- @field _data table Table to hold the set data. + local supportedFiletypeSet = { _data = {} } + + function supportedFiletypeSet:add(values) + values = type(values) == "string" and { values } or values + + 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 + + -- Find the installed 'find' command + for _, cmd in ipairs({ "find", "fd", "rg" }) do + if vim.fn.executable(cmd) == 1 then + findCmd = cmd + break + end + end + + -- Add the supported file types if the tool is installed + local tools = { + -- required + { cmd = "chafa", name = "Chafa", filetypes = { "png", "jpg", "jpeg", "webp", "gif", "bmp", "tiff" } }, + -- optional + { + cmd = "convert", + name = "ImageMagick", + filetypes = { "svg", "png", "jpg", "jpeg", "gif", "tiff", "bmp", "webp", "pdf", "ico", "eps" }, + }, + { + cmd = "ffmpegthumbnailer", + name = "ffmpegthumbnailer", + filetypes = { "mp4", "webm", "mkv", "avi", "mov", "flv", "mpeg", "mpg" }, + }, + { cmd = "pdftoppm", name = "pdftoppm", filetypes = { "pdf" } }, + { cmd = "epub-thumbnailer", name = "epub-thumbnailer", filetypes = { "epub" } }, + { cmd = "fontpreview", name = "fontpreview", filetypes = { "ttf", "otf", "woff", "woff2" } }, + } + for _, tool in ipairs(tools) do + if vim.fn.executable(tool.cmd) == 1 then + supportedFiletypeSet:add(tool.filetypes) + end + end + + -- We checked that all required prerequisites were installed in the enabled function, but still ensure this again before setup. + -- Set up the extension for media files when the telescope is loaded. + local mediaFilesExtension = { + filetypes = supportedFiletypeSet:toList(), + find_cmd = findCmd, + } + if mediaFilesExtension.find_cmd and #mediaFilesExtension.filetypes > 0 then + LazyVim.on_load("telescope.nvim", function() + local ok, telescope = pcall(require, "telescope") + if ok then + telescope.extensions["media_files"] = + vim.tbl_extend("force", telescope.extensions["media_files"] or {}, mediaFilesExtension) + else + LazyVim.error("Failed to load telescope.nvim") + end + end) + end + end, + keys = { + { "fP", "Telescope media_files", desc = "Find Media Files" }, + }, + }, + }, + }, + -- Flash Telescope config { "nvim-telescope/telescope.nvim",