mirror of
https://github.com/anotherhadi/sttr.nvim.git
synced 2026-10-05 16:58:24 +02:00
init
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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
|
||||
Reference in New Issue
Block a user