@@ -26,6 +26,16 @@ function M.pick_one(items, prompt, label_fn)
26
26
end
27
27
28
28
29
+ local function index_of (xs , term )
30
+ for i , x in pairs (xs ) do
31
+ if x == term then
32
+ return i
33
+ end
34
+ end
35
+ return - 1
36
+ end
37
+
38
+
29
39
function M .pick_many (items , prompt , label_f )
30
40
if not items or # items == 0 then
31
41
return {}
@@ -36,11 +46,11 @@ function M.pick_many(items, prompt, label_f)
36
46
end
37
47
38
48
local choices = {}
49
+ local selected = {}
39
50
for i , item in pairs (items ) do
40
51
table.insert (choices , string.format (" %d. %s" , i , label_f (item )))
41
52
end
42
53
43
- local selected = {}
44
54
while true do
45
55
local answer = vim .fn .input (string.format (" \n %s\n %s (Esc to finish): " , table.concat (choices , " \n " ), prompt ))
46
56
if answer == " " then
@@ -49,9 +59,15 @@ function M.pick_many(items, prompt, label_f)
49
59
50
60
local index = tonumber (answer )
51
61
if index ~= nil then
52
- if string.find (choices [index ], " *" ) == nil then
53
- table.insert (selected , items [index ])
54
- choices [index ] = choices [index ] .. " *"
62
+ local choice = choices [index ]
63
+ local item = items [index ]
64
+ if string.find (choice , " *" ) == nil then
65
+ table.insert (selected , item )
66
+ choices [index ] = choice .. " *"
67
+ else
68
+ choices [index ] = string.gsub (choice , " %*$" , " " )
69
+ local idx = index_of (selected , item )
70
+ table.remove (selected , idx )
55
71
end
56
72
end
57
73
end
0 commit comments