Files
dotfiles/.config/nvim/lua/utils/telescope_extra/init.lua
2025-09-05 10:32:09 -07:00

93 lines
2.7 KiB
Lua

local telescope = require("telescope")
local telescopeConfig = require("telescope.config")
local M = {}
function M.find_and_paste_image()
local telescope = require("telescope.builtin")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
telescope.find_files({
attach_mappings = function(_, map)
local function embed_image(prompt_bufnr)
local entry = action_state.get_selected_entry()
local filepath = entry[1]
actions.close(prompt_bufnr)
local img_clip = require("img-clip")
img_clip.paste_image(nil, filepath)
end
map("i", "<CR>", embed_image)
map("n", "<CR>", embed_image)
return true
end,
})
end
function M.setup()
-- Clone the default Telescope configuration
local vimgrep_arguments = { unpack(telescopeConfig.values.vimgrep_arguments) }
-- I want to search in hidden/dot files.
table.insert(vimgrep_arguments, "--hidden")
-- I don't want to search in the `.git` directory.
table.insert(vimgrep_arguments, "--glob")
table.insert(vimgrep_arguments, "!**/.git/*")
telescope.setup({
defaults = {
-- `hidden = true` is not supported in text grep commands.
vimgrep_arguments = vimgrep_arguments,
},
pickers = {
find_files = {
-- `hidden = true` will still show the inside of `.git/` as it's not `.gitignore`d.
find_command = { "rg", "--files", "--hidden", "--glob", "!**/.git/*" },
mappings = {
n = {
["cd"] = function(prompt_bufnr)
local selection = require("telescope.actions.state").get_selected_entry()
local dir = vim.fn.fnamemodify(selection.path, ":p:h")
require("telescope.actions").close(prompt_bufnr)
-- Depending on what you want put `cd`, `lcd`, `tcd`
vim.cmd(string.format("silent lcd %s", dir))
end,
},
},
},
},
preview = {
-- show images in telescope using kitty
mime_hook = function(filepath, bufnr, opts)
local is_image = function(filepath)
local image_extensions = { "png", "jpg" } -- Supported image formats
local split_path = vim.split(filepath:lower(), ".", { plain = true })
local extension = split_path[#split_path]
return vim.tbl_contains(image_extensions, extension)
end
if is_image(filepath) then
local term = vim.api.nvim_open_term(bufnr, {})
local function send_output(_, data, _)
for _, d in ipairs(data) do
vim.api.nvim_chan_send(term, d .. "\r\n")
end
end
vim.fn.jobstart({
"kitty +icat " .. filepath, -- Terminal image viewer command
}, { on_stdout = send_output, stdout_buffered = true, pty = true })
else
require("telescope.previewers.utils").set_preview_message(
bufnr,
opts.winid,
"Binary cannot be previewed"
)
end
end,
},
})
end
return M