Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ require'telescope'.setup {
extensions = {
media_files = {
-- filetypes whitelist
-- defaults to {"png", "jpg", "mp4", "webm", "pdf"}
-- defaults to {"png", "jpg", "gif", "mp4", "webm", "pdf"}
filetypes = {"png", "webp", "jpg", "jpeg"},
find_cmd = "rg" -- find command (defaults to `fd`)
-- default: copy entry's relative path to vim clipboard
on_enter = function(filepath)
vim.fn.setreg('+', filepath)
vim.notify("The image path has been copied to system clipboard!")
end
}
},
}
Expand All @@ -45,7 +50,15 @@ require'telescope'.setup {
lua require('telescope').extensions.media_files.media_files()
```

When you press `<CR>` on a selected file, it will copy its relative path to the clipboard
```lua
-- Useful for plugin developer that use telescope-media-files on their plugin
require('telescope').extensions.media_files.media_files({}, function(filepath)
-- Your custom action to do when file is selected
end)
```

When you press `<CR>`/<kbd>Enter</kbd> on a selected file, it will copy its
relative path to vim clipboard except when you modify `on_enter`.


## Prerequisites
Expand Down
14 changes: 10 additions & 4 deletions lua/telescope/_extensions/media_files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local M = {}

local filetypes = {}
local find_cmd = ""
local on_enter

M.base_directory=""
M.media_preview = defaulter(function(opts)
Expand All @@ -41,7 +42,7 @@ M.media_preview = defaulter(function(opts)
}
end, {})

function M.media_files(opts)
function M.media_files(opts, custom_on_enter)
local find_commands = {
find = {
'find',
Expand Down Expand Up @@ -92,9 +93,9 @@ function M.media_files(opts)
local entry = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if entry[1] then
local filename = entry[1]
vim.fn.setreg(vim.v.register, filename)
vim.notify("The image path has been copied!")
local filepath = entry[1]
custom_on_enter = custom_on_enter or on_enter
custom_on_enter(filepath)
end
end)
return true
Expand Down Expand Up @@ -129,6 +130,11 @@ return require('telescope').register_extension {
setup = function(ext_config)
filetypes = ext_config.filetypes or {"png", "jpg", "gif", "mp4", "webm", "pdf"}
find_cmd = ext_config.find_cmd or "fd"
on_enter = ext_config.on_enter or
function(filepath)
vim.fn.setreg(vim.v.register, filepath)
vim.notify("The image path has been copied!")
end
end,
exports = {
media_files = M.media_files
Expand Down