vijaymarupudi / nvim-fzf

A Lua API for using fzf in neovim.
MIT License
340 stars 13 forks source link

Add logic to use a list of dictionaries #11

Closed baldore closed 3 years ago

baldore commented 3 years ago

First at all, thanks for this great plugin. I'm trying to play with it and I see some "limitations" (maybe I need to figure it out). Here's the issue:

The examples shown are great, but they have some limitations by just being strings. It should be great to have an additional option to receive a list of dictionaries, where one of the fields is what fzf shows, for example, label. This can be an amazing addtion since will provide more flexibility to write custom methods.

Example: I'm writing a function that shows the all the lines in a buffer, but I need to use the line numbers of the selected values. If I could just use objects as the list, then I'll receive the objects with the line numbers added.

Let me know what do you think. I could help with a PR if needed.

baldore commented 3 years ago

Forget about this. I think I need to study a little bit Fzf. Thanks again for this.

vijaymarupudi commented 3 years ago

Here's a snippet that will help you, glad to answer any questions

local fzf = require('fzf').fzf

local function pick_object(list_of_objects, key)
  local fzf_input = {}
  for idx, v in ipairs(list_of_objects) do
    table.insert(fzf_input, tostring(idx) .. "\t" .. tostring(v[key]))
  end
  local choices =  fzf(fzf_input, "--with-nth=2..")
  if not choices then return nil end
  local idx = tonumber(string.match(choices[1], "^(%d)\t"))
  return list_of_objects[idx]
end

coroutine.wrap(function ()
  local choice = pick_object({
    {name = "Test1", val = 343434},
    {name = "AnotherTest", val =34343}
  }, "name")
  pp(choice)
end)()

If you think this should be a more general utility, open to API suggestions.

baldore commented 3 years ago

Yeah, I just didn't know how to pass data without affecting the filtering. I checked the documentation and it was there. Thanks a lot for taking the time to write the example.

vijaymarupudi commented 3 years ago

No problem, always glad to hear from users.