Menu: how to return items selected? #350
Unanswered
Otterpatsch
asked this question in
Q&A
Replies: 1 comment
-
You can kinda hack it together by doing something like this: local function create_menu()
local items = {
{ type = "item", text = "Item1", values = { id = 1 } },
{ type = "item", text = "Item2", values = { id = 2 } },
{ type = "item", text = "Item3", values = { id = 3 } },
{ type = "item", text = "Item4", values = { id = 4 } },
{ type = "item", text = "Item5", values = { id = 5 } },
}
local on_submit = function(item)
return item.id
end
local on_close = function()
return
end
local lines = {}
for _, item in ipairs(items) do
if item.type == "item" then
table.insert(lines, Menu.item(item.text, item.values))
elseif item.type == "separator" then
table.insert(lines, Menu.separator(item.text))
else
print("Error")
end
end
local selected_ids = {}
local menu = Menu({
position = "50%",
size = {
width = 25,
height = 5,
},
border = {
style = "single",
text = {
top = "Choose",
top_align = "center",
},
},
win_options = {
winhighlight = "Normal:Normal,FloatBorder:Normal",
},
}, {
lines = lines,
max_width = 20,
keymap = {
focus_next = { "j", "<Down>", "<Tab>" },
focus_prev = { "k", "<Up>", "<S-Tab>" },
close = { "<Esc>", "<C-c>" },
submit = { "<CR>" },
},
on_close = function()
return on_close()
end,
on_submit = function()
return on_submit(vim.tbl_filter(function(id)
return selected_ids[id]
end, vim.tbl_keys(selected_ids)))
end,
})
menu:map("n", "<Space>", function()
local node = menu.tree:get_node()
if node then
selected_ids[node.id] = not selected_ids[node.id]
end
end, { nowait = true })
menu:mount()
end You can show the selected items in UI by using the Or you can use |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello there,
I want to use the item which got selected for further processing. How to do so?
I tried to simply print(menu) but this as expected didnt yield the expected value
Beta Was this translation helpful? Give feedback.
All reactions