perf improvement for system commands (issue #126)

main
bhagwan 3 years ago
parent dffe7ff56b
commit 8871e7485a

@ -68,7 +68,7 @@ end
local function run_command(args)
local user_opts = args or {}
if next(user_opts) == nil and not user_opts.cmd then
utils.info("[Fzf-lua] missing command args")
utils.info("missing command args")
return
end

@ -158,8 +158,8 @@ local get_diff_files = function(opts)
local diff_files = {}
local cmd = opts.git_status_cmd or config.globals.files.git_status_cmd
if not cmd then return {} end
local status = vim.fn.systemlist(path.git_cwd(cmd, opts.cwd))
if not utils.shell_error() then
local status, err = utils.io_systemlist(path.git_cwd(cmd, opts.cwd))
if err == 0 then
for i = 1, #status do
local icon = status[i]:match("[MUDAR?]+")
local file = status[i]:match("[^ ]*$")

@ -32,6 +32,8 @@ function M.setup(opts)
if bat_theme and #bat_theme > 0 then
vim.env.BAT_THEME = bat_theme
end
-- set lua_io if caller requested
utils.set_lua_io(globals.lua_io)
-- reset our globals based on user opts
-- this doesn't happen automatically
config.globals = globals

@ -172,8 +172,8 @@ end
function M.git_root(cwd, noerr)
local cmd = M.git_cwd("git rev-parse --show-toplevel", cwd)
local output = vim.fn.systemlist(cmd)
if utils.shell_error() then
local output, err = utils.io_systemlist(cmd)
if err ~= 0 then
if not noerr then utils.info(unpack(output)) end
return nil
end

@ -327,4 +327,48 @@ function M.ft_detect(ext)
return ft
end
-- speed up exteral commands (issue #126)
local _use_lua_io = false
function M.set_lua_io(b)
_use_lua_io = b
if _use_lua_io then
M.warn("using experimental feature 'lua_io'")
end
end
function M.io_systemlist(cmd, use_lua_io)
if not use_lua_io then use_lua_io = _use_lua_io end
if use_lua_io then
local rc = 0
local stdout = ''
local handle = io.popen(cmd .. " 2>&1; echo $?", "r")
if handle then
stdout = {}
for h in handle:lines() do
stdout[#stdout + 1] = h
end
-- last line contains the exit status
rc = tonumber(stdout[#stdout])
stdout[#stdout] = nil
end
handle:close()
return stdout, rc
else
return vim.fn.systemlist(cmd), vim.v.shell_error
end
end
function M.io_system(cmd, use_lua_io)
if not use_lua_io then use_lua_io = _use_lua_io end
if use_lua_io then
local stdout, rc = M.io_systemlist(cmd, true)
if type(stdout) == 'table' then
stdout = table.concat(stdout, "\n")
end
return stdout, rc
else
return vim.fn.system(cmd), vim.v.shell_error
end
end
return M

Loading…
Cancel
Save