From dffe7ff56bd9a5254452c08f3da2ddd56be531a4 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sat, 11 Sep 2021 21:26:04 -0700 Subject: [PATCH] git_icons and git_status improvements --- lua/fzf-lua/config.lua | 12 ++------ lua/fzf-lua/core.lua | 58 ++++++++++++----------------------- lua/fzf-lua/previewer/fzf.lua | 2 +- 3 files changed, 22 insertions(+), 50 deletions(-) diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 8902f9a..e7f69cf 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -126,8 +126,7 @@ M.globals.files = { file_icons = true and M._has_devicons, color_icons = true, git_icons = true, - git_diff_cmd = "git diff --name-status --relative HEAD", - git_untracked_cmd = "git ls-files --exclude-standard --others", + git_status_cmd = "git status -s", find_opts = "-type f -printf '%P\n'", fd_opts = [[--color never --type f --hidden --follow ]] .. @@ -195,6 +194,7 @@ M.globals.git = { ["M"] = { icon = "M", color = "yellow" }, ["D"] = { icon = "D", color = "red" }, ["A"] = { icon = "A", color = "green" }, + ["R"] = { icon = "R", color = "yellow" }, ["?"] = { icon = "?", color = "magenta" }, }, } @@ -206,8 +206,6 @@ M.globals.grep = { file_icons = true and M._has_devicons, color_icons = true, git_icons = true, - git_diff_cmd = M.globals.files.git_diff_cmd, - git_untracked_cmd = M.globals.files.git_untracked_cmd, grep_opts = "--line-number --recursive --color=auto --perl-regexp", rg_opts = "--column --line-number --no-heading --color=always --smart-case", actions = M.globals.files.actions, @@ -218,8 +216,6 @@ M.globals.oldfiles = { file_icons = true and M._has_devicons, color_icons = true, git_icons = false, - git_diff_cmd = M.globals.files.git_diff_cmd, - git_untracked_cmd = M.globals.files.git_untracked_cmd, actions = M.globals.files.actions, } M.globals.quickfix = { @@ -229,8 +225,6 @@ M.globals.quickfix = { file_icons = true and M._has_devicons, color_icons = true, git_icons = false, - git_diff_cmd = M.globals.files.git_diff_cmd, - git_untracked_cmd = M.globals.files.git_untracked_cmd, actions = M.globals.files.actions, } M.globals.loclist = { @@ -240,8 +234,6 @@ M.globals.loclist = { file_icons = true and M._has_devicons, color_icons = true, git_icons = false, - git_diff_cmd = M.globals.files.git_diff_cmd, - git_untracked_cmd = M.globals.files.git_untracked_cmd, actions = M.globals.files.actions, } M.globals.buffers = { diff --git a/lua/fzf-lua/core.lua b/lua/fzf-lua/core.lua index 009c6ab..834485a 100644 --- a/lua/fzf-lua/core.lua +++ b/lua/fzf-lua/core.lua @@ -156,43 +156,22 @@ end local get_diff_files = function(opts) local diff_files = {} - local status = vim.fn.systemlist(path.git_cwd( - config.globals.files.git_diff_cmd, opts.cwd)) + 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 for i = 1, #status do - local icon, file = status[i]:match("^([MUDAR])%s+(.*)") - if icon and file then diff_files[file] = icon end + local icon = status[i]:match("[MUDAR?]+") + local file = status[i]:match("[^ ]*$") + if icon and file then + diff_files[file] = icon + end end end return diff_files end -local get_untracked_files = function(opts) - local untracked_files = {} - local status = vim.fn.systemlist(path.git_cwd( - config.globals.files.git_untracked_cmd, opts.cwd)) - if vim.v.shell_error == 0 then - for i = 1, #status do - local file = status[i] - untracked_files[file] = "?" - end - end - - return untracked_files -end - -local get_git_indicator = function(file, diff_files, untracked_files) - if diff_files and diff_files[file] then - return diff_files[file] - end - if untracked_files and untracked_files[file] then - return untracked_files[file] - end - return utils.nbsp -end - - M.make_entry_lcol = function(_, entry) if not entry then return nil end local filename = entry.filename or vim.api.nvim_buf_get_name(entry.bufnr) @@ -232,16 +211,18 @@ M.make_entry_file = function(opts, x) ret[#ret+1] = utils.nbsp end if opts.git_icons then - local indicator = get_git_indicator(file, opts.diff_files, opts.untracked_files) - icon = indicator - local git_icon = config.globals.git.icons[indicator] - if git_icon then - icon = git_icon.icon - if opts.color_icons then - icon = utils.ansi_codes[git_icon.color or "dark_grey"](icon) + local indicators = opts.diff_files and opts.diff_files[file] or utils.nbsp + for i=1,#indicators do + icon = indicators:sub(i,i) + local git_icon = config.globals.git.icons[icon] + if git_icon then + icon = git_icon.icon + if opts.color_icons then + icon = utils.ansi_codes[git_icon.color or "dark_grey"](icon) + end end + ret[#ret+1] = icon end - ret[#ret+1] = icon ret[#ret+1] = utils.nbsp end ret[#ret+1] = x @@ -272,7 +253,7 @@ M.fzf_files = function(opts) if not opts then return end -- reset git tracking - opts.diff_files, opts.untracked_files = nil, nil + opts.diff_files = nil if opts.git_icons and not path.is_git_repo(opts.cwd, true) then opts.git_icons = false end coroutine.wrap(function () @@ -283,7 +264,6 @@ M.fzf_files = function(opts) if opts.git_icons then opts.diff_files = get_diff_files(opts) - opts.untracked_files = get_untracked_files(opts) end local has_prefix = opts.file_icons or opts.git_icons or opts.lsp_icons diff --git a/lua/fzf-lua/previewer/fzf.lua b/lua/fzf-lua/previewer/fzf.lua index 9727388..8592985 100644 --- a/lua/fzf-lua/previewer/fzf.lua +++ b/lua/fzf-lua/previewer/fzf.lua @@ -173,7 +173,7 @@ function Previewer.git_diff:cmdline(o) o = o or {} local act = helpers.choices_to_shell_cmd_previewer(function(items) local is_deleted = items[1]:match("D"..utils.nbsp) ~= nil - local is_untracked = items[1]:match("?"..utils.nbsp) ~= nil + local is_untracked = items[1]:match("[?RA]"..utils.nbsp) ~= nil local file = path.entry_to_file(items[1], not self.relative and self.opts.cwd) local cmd = self.cmd local args = self.args