Picker: Is there a simple example for creating your own picker source? #498
-
In telescope, I created my own project selector. It looks like the screenshot attached. The |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is how I got it working. No idea if I did any of this right. local snacks = require("snacks")
local config = require("ao.config")
local layout = require("snacks.picker.config.layouts")
local projects = require("ao.modules.projects").list({ cwd = config.options.projects.directory.path })
local width = 0
local height = math.min(#projects + 5, 25)
local max_name_length = 0
for _, entry in ipairs(projects) do
entry.text = entry.name .. entry.path
max_name_length = math.max(max_name_length, #entry.name)
local total_length = #entry.text + 20
if total_length > width then
width = total_length
end
end
width = math.min(width, 150)
local padding = max_name_length + 15
local format = function(item, _)
local ret = {}
ret[#ret + 1] = { item.name, "SnacksPickerLabel" }
ret[#ret + 1] = { string.rep(" ", padding - #item.name), virtual = true }
ret[#ret + 1] = { item.path, "SnacksPickerComment" }
return ret
end
layout = vim.tbl_deep_extend("force", layout.telescope, {
layout = {
width = width,
height = height,
min_width = width,
min_height = height,
{
box = "vertical",
{ win = "list", title = "Results", title_pos = "center", border = "rounded" },
{ win = "input", height = 1, border = "rounded", title = "Projects", title_pos = "center" },
},
},
preview = false,
})
snacks.picker.pick({ items = projects, layout = layout, format = format }) Looks like this: |
Beta Was this translation helpful? Give feedback.
-
This is my custom picker for workspaces.nvim. Putting it here if anyone needs it: vim.keymap.set('n', '<Leader>w', function()
local items = {}
local longest_name = 0
for i, workspace in ipairs(require('workspaces').get()) do
table.insert(items, {
idx = i,
score = i,
text = workspace.path,
name = workspace.name,
})
longest_name = math.max(longest_name, #workspace.name)
end
longest_name = longest_name + 2
return Snacks.picker({
items = items,
format = function(item)
local ret = {}
ret[#ret + 1] = { ('%-' .. longest_name .. 's'):format(item.name), 'SnacksPickerLabel' }
ret[#ret + 1] = { item.text, 'SnacksPickerComment' }
return ret
end,
confirm = function(picker, item)
picker:close()
vim.cmd(('WorkspacesOpen %s'):format(item.name))
end,
})
end)
end,
|
Beta Was this translation helpful? Give feedback.
This is my custom picker for workspaces.nvim. Putting it here if anyone needs it: