From 22d7f4b6c5a56c31f1ca0f074c8ffc96b6dbd7c0 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sat, 12 Feb 2022 11:54:19 -0800 Subject: [PATCH] args refactor and resume support --- README.md | 2 +- doc/fzf-lua.txt | 2 +- lua/fzf-lua/config.lua | 2 +- lua/fzf-lua/providers/files.lua | 68 +++++++++++++++++++++------------ 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 31b3aaf..14f0f95 100644 --- a/README.md +++ b/README.md @@ -546,7 +546,7 @@ require'fzf-lua'.setup { prompt = 'Args❯ ', files_only = true, -- actions inherit from 'actions.files' and merge - actions = { ["ctrl-x"] = actions.arg_del } + actions = { ["ctrl-x"] = { actions.arg_del, actions.resume } } }, oldfiles = { prompt = 'History❯ ', diff --git a/doc/fzf-lua.txt b/doc/fzf-lua.txt index aeb24cd..570eae7 100644 --- a/doc/fzf-lua.txt +++ b/doc/fzf-lua.txt @@ -588,7 +588,7 @@ Consult the list below for available settings: prompt = 'Args❯ ', files_only = true, -- actions inherit from 'actions.files' and merge - actions = { ["ctrl-x"] = actions.arg_del } + actions = { ["ctrl-x"] = { actions.arg_del, actions.resume } } }, oldfiles = { prompt = 'History❯ ', diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 433a224..27b6d50 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -283,7 +283,7 @@ M.globals.args = { git_icons = true, _actions = function() return M.globals.actions.files end, actions = { - ["ctrl-x"] = actions.arg_del + ["ctrl-x"] = { actions.arg_del, actions.resume } }, } M.globals.oldfiles = { diff --git a/lua/fzf-lua/providers/files.lua b/lua/fzf-lua/providers/files.lua index 953feca..f0458eb 100644 --- a/lua/fzf-lua/providers/files.lua +++ b/lua/fzf-lua/providers/files.lua @@ -46,38 +46,56 @@ M.args = function(opts) opts = config.normalize_opts(opts, config.globals.args) if not opts then return end - local entries = vim.fn.execute("args") - entries = utils.strsplit(entries, "%s\n") - -- remove the current file indicator - -- remove all non-files - local args = {} - for _, s in ipairs(entries) do - if s:match('^%[') then - s = s:gsub('^%[', ''):gsub('%]$', '') - end - local st = vim.loop.fs_stat(s) - if opts.files_only == false or - st and st.type == 'file' then - table.insert(args, s) - end + if opts.fzf_opts['--header'] == nil then + opts.fzf_opts['--header'] = vim.fn.shellescape((':: %s to delete') + :format(utils.ansi_codes.yellow(""))) end - entries = nil local contents = function (cb) - for _, x in ipairs(args) do + + local function add_entry(x, co) x = core.make_entry_file(opts, x) - if x then - cb(x, function(err) - if err then return end - -- close the pipe to fzf, this - -- removes the loading indicator in fzf - cb(nil, function() end) - end) - end + if not x then return end + cb(x, function(err) + coroutine.resume(co) + if err then + -- close the pipe to fzf, this + -- removes the loading indicator in fzf + cb(nil, function() end) + end + end) + coroutine.yield() end - utils.delayed_cb(cb) + + -- run in a coroutine for async progress indication + coroutine.wrap(function() + local co = coroutine.running() + + local entries = vim.fn.execute("args") + entries = utils.strsplit(entries, "%s\n") + -- remove the current file indicator + -- remove all non-files + -- local start = os.time(); for _ = 1,10000,1 do + for _, s in ipairs(entries) do + if s:match('^%[') then + s = s:gsub('^%[', ''):gsub('%]$', '') + end + local st = vim.loop.fs_stat(s) + if opts.files_only == false or + st and st.type == 'file' then + add_entry(s, co) + end + end + -- end; print("took", os.time()-start, "seconds.") + + -- done + cb(nil, function() coroutine.resume(co) end) + coroutine.yield() + end)() + end + opts = core.set_header(opts, 2) return core.fzf_files(opts, contents) end