Files
sttr.nvim/lua/sttr/picker.lua
T
2026-05-21 13:56:31 +02:00

65 lines
1.8 KiB
Lua

local M = {}
local function fmt(item)
return string.format('%-28s %s', item.cmd, item.desc)
end
local function with_telescope(items, on_choice)
local pickers = require('telescope.pickers')
local finders = require('telescope.finders')
local conf = require('telescope.config').values
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
pickers.new({}, {
prompt_title = 'sttr',
finder = finders.new_table({
results = items,
entry_maker = function(item)
local display = fmt(item)
return { value = item, display = display, ordinal = display }
end,
}),
sorter = conf.generic_sorter({}),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local entry = action_state.get_selected_entry()
on_choice(entry and entry.value)
end)
return true
end,
}):find()
end
local function with_snacks(snacks, items, on_choice)
snacks.picker.select(items, {
prompt = 'sttr',
format_item = fmt,
}, on_choice)
end
local function with_native(items, on_choice)
vim.ui.select(items, {
prompt = 'sttr',
format_item = fmt,
}, on_choice)
end
function M.pick(items, on_choice)
if pcall(require, 'telescope') then
return with_telescope(items, on_choice)
end
-- Explicit snacks check: covers users who have picker but not select override
local ok, snacks = pcall(require, 'snacks')
if ok and type(snacks.picker) == 'table' and type(snacks.picker.select) == 'function' then
return with_snacks(snacks, items, on_choice)
end
-- vim.ui.select: snacks may have overridden it via { select = { enabled = true } }
with_native(items, on_choice)
end
return M