Frecency using fre and fd
#2174
elanmed
started this conversation in
Show and tell
Replies: 2 comments 18 replies
-
|
An alternate version of local contents = function(fzf_cb)
local seen = {}
--- @param on_exit function
local function run_fre(on_exit)
Job:new {
command = "fre",
args = { "--sorted", },
on_stdout = function(err, abs_file)
if err then return vim.notify(err, vim.log.levels.ERROR) end
-- `fre` can have files from any directory
if not vim.startswith(abs_file, cwd) then return end
-- `fre` can have duplicates, need to check `seen`
if seen[abs_file] then return end
seen[abs_file] = true
local rel_file = vim.fs.relpath(cwd, abs_file)
local entry = fzf_lua.make_entry.file(rel_file, opts)
fzf_cb(entry)
end,
on_exit = on_exit,
}:start()
end
local function run_fd()
local fd_args = { "--hidden", "--absolute-path", "--type", "f", "--base-directory", cwd, }
Job:new {
command = "fd",
args = fd_args,
on_stdout = function(err, abs_file)
if err then return vim.notify(err, vim.log.levels.ERROR) end
-- `fd` only searches in the cwd, no need to filter out
-- `fd` has no duplicates, no need to add to `seen`
if seen[abs_file] then return end
local rel_file = vim.fs.relpath(cwd, abs_file)
local entry = fzf_lua.make_entry.file(rel_file, opts)
fzf_cb(entry)
end,
on_exit = function()
fzf_cb(nil)
end,
}:start()
end
run_fre(run_fd)
end |
Beta Was this translation helpful? Give feedback.
15 replies
-
|
I ended up making my own fzf-lua frecency plugin that implements that same algorithm as https://github.com/elanmed/fzf-lua-frecency.nvim |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey all, I was inspired by this reddit comment which suggested using the
frecommand to implement a frecency picker in fzf-lua, and I decided to give it a try.This code does a few things:
vim.systemto run thefre --sortedcommand.vim.systemreturns a newline-delimited string (when passingtext=true), which I convert to a table and iterate over. I use coroutines while iterating so I can open the fzf lua UI immediately and iteratively add each item to the list - based on the great fzf-lua wiki. I noticed thatfrefiles can have duplicates, so I filter those out.fre --sortedonly populates files that have been explicitly tracked withfre- not every file in thecwd- so I also populate the UI withfdby callingvim.system. By callingrun_fdas anon_exitcallback, I can ensure that thefrefiles will always appear at the top of the UIenteraction to callfre --addwith the selected file@ibhagwan if this is the wrong forum for these kinds of posts let me know 😅
Beta Was this translation helpful? Give feedback.
All reactions