@@ -78,10 +78,13 @@ M.ui_select = function(items, ui_opts, on_choice)
7878 local entries = {}
7979 local num_width = math.ceil (math.log10 (# items ))
8080 local num_format_str = " %" .. num_width .. " d"
81+ local reverse_lookup = {}
8182 for i , e in ipairs (items ) do
82- table.insert (entries ,
83- (" %s. %s" ):format (utils .ansi_codes .magenta (num_format_str :format (i )),
84- ui_opts .format_item and ui_opts .format_item (e ) or tostring (e )))
83+ local entry = (" %s. %s" ):format (utils .ansi_codes .magenta (num_format_str :format (i )),
84+ ui_opts .format_item and ui_opts .format_item (e ) or tostring (e ))
85+ entries [# entries + 1 ] = entry
86+ -- fzf will to strip all ansi (even in item str), so we store stripped key
87+ reverse_lookup [utils .strip_ansi_coloring (entry )] = i
8588 end
8689
8790 local opts = _OPTS or {}
@@ -182,7 +185,8 @@ M.ui_select = function(items, ui_opts, on_choice)
182185 local previewer = require (" fzf-lua.previewer.builtin" ).buffer_or_file :extend ()
183186 --- @diagnostic disable-next-line : unused
184187 function previewer :parse_entry (entry_str , cb )
185- local res = assert (ui_opts .preview_item (entry_str , cb ))
188+ local str = utils .strip_ansi_coloring (entry_str )
189+ local res = assert (ui_opts .preview_item (items [reverse_lookup [str ]], cb ))
186190 local pos_start , pos_end = res .pos , res .pos_end
187191 return {
188192 _scratch_buf = res .buf ,
0 commit comments