bugfix: git icon overrides breaks 'git_status' (closes #283)

This commit is contained in:
bhagwan 2021-12-28 13:13:03 -08:00
parent 782b8599fd
commit 3eb5baa6e9
3 changed files with 38 additions and 4 deletions

View File

@ -146,6 +146,7 @@ M.globals = {
cmd_deleted = "git diff --color HEAD --", cmd_deleted = "git diff --color HEAD --",
cmd_modified = "git diff --color HEAD", cmd_modified = "git diff --color HEAD",
cmd_untracked = "git diff --color --no-index /dev/null", cmd_untracked = "git diff --color --no-index /dev/null",
_fn_git_icons = function() return M.globals.git.icons end,
_ctor = previewers.fzf.git_diff, _ctor = previewers.fzf.git_diff,
}, },
man = { man = {

View File

@ -146,15 +146,36 @@ function Previewer.git_diff:new(o, opts)
self.cmd_modified = path.git_cwd(o.cmd_modified, opts.cwd) self.cmd_modified = path.git_cwd(o.cmd_modified, opts.cwd)
self.cmd_untracked = path.git_cwd(o.cmd_untracked, opts.cwd) self.cmd_untracked = path.git_cwd(o.cmd_untracked, opts.cwd)
self.pager = o.pager self.pager = o.pager
do
-- populate the icon mappings
local icons_overrides = o._fn_git_icons and o._fn_git_icons()
self.git_icons = {}
for _, i in ipairs({ "D", "M", "R", "A", "C", "?" }) do
self.git_icons[i] =
icons_overrides and icons_overrides[i] and
utils.lua_regex_escape(icons_overrides[i].icon) or i
end
end
return self return self
end end
function Previewer.git_diff:cmdline(o) function Previewer.git_diff:cmdline(o)
o = o or {} o = o or {}
local act = shell.preview_action_cmd(function(items) local act = shell.preview_action_cmd(function(items)
local is_deleted = items[1]:match("D"..utils.nbsp) ~= nil if not items or vim.tbl_isempty(items) then
local is_modified = items[1]:match("[MRA]"..utils.nbsp) ~= nil utils.warn("shell error while running preview action.")
local is_untracked = items[1]:match("[%?C]"..utils.nbsp) ~= nil return
end
local is_deleted = items[1]:match(self.git_icons['D']..utils.nbsp) ~= nil
local is_modified = items[1]:match("[" ..
self.git_icons['M'] ..
self.git_icons['R'] ..
self.git_icons['A'] ..
"]" ..utils.nbsp) ~= nil
local is_untracked = items[1]:match("[" ..
self.git_icons['?'] ..
self.git_icons['C'] ..
"]"..utils.nbsp) ~= nil
local file = path.entry_to_file(items[1], not self.relative and self.opts.cwd) local file = path.entry_to_file(items[1], not self.relative and self.opts.cwd)
local cmd = nil local cmd = nil
if is_modified then cmd = self.cmd_modified if is_modified then cmd = self.cmd_modified
@ -172,7 +193,10 @@ function Previewer.git_diff:cmdline(o)
-- uncomment to see the command in the preview window -- uncomment to see the command in the preview window
-- cmd = vim.fn.shellescape(cmd) -- cmd = vim.fn.shellescape(cmd)
return cmd return cmd
end, "{}") -- we need to add '--' to mark the end of command options
-- as git icon customization may contain special shell chars
-- which will otherwise choke our preview cmd ('+', '-', etc)
end, "-- {}")
return act return act
end end

View File

@ -123,6 +123,15 @@ function M.lua_escape(str)
end) end)
end end
function M.lua_regex_escape(str)
-- escape all lua special chars
-- ( ) % . + - * [ ? ^ $
if not str then return nil end
return str:gsub('[%(%)%.%+%-%*%[%?%^%$%%]', function(x)
return '%' .. x
end)
end
-- TODO: why does `file --dereference --mime` return -- TODO: why does `file --dereference --mime` return
-- wrong result for some lua files ('charset=binary')? -- wrong result for some lua files ('charset=binary')?
M.file_is_binary = function(filepath) M.file_is_binary = function(filepath)