better git format and parsing

This commit is contained in:
kyazdani42
2020-02-19 16:24:49 +01:00
parent 1c4fb795fb
commit 05b5117f75
5 changed files with 72 additions and 50 deletions

View File

@@ -71,11 +71,12 @@ local function format_tree(tree)
for i, node in pairs(tree) do
local padding = get_padding(node.depth)
local git = node.git
local icon = ""
if node.icon == true then
icon = get_icon(node.path .. node.name, node.dir, node.open)
end
dirs[i] = padding .. icon .. node.name
dirs[i] = padding .. icon .. git .. node.name
end
return dirs
@@ -109,48 +110,60 @@ local HIGHLIGHT_GROUPS = {
['^.*%.html?$'] = 'HtmlFile';
}
local function highlight_git(highlight, node, line, col_start)
if node.git ~= '' then
highlight('LuaTreeGit'..node.git, line, col_start, -1)
return true
end
return false
end
local function highlight_line(buffer)
local function highlight(group, line, from, to)
vim.api.nvim_buf_add_highlight(buffer, -1, group, line, from, to)
end
return function(line, node)
local text_start = node.depth * 2
if node.dir then
if node.name ~= '..' then
highlight('LuaTreeFolderIcon', line, 0, text_start + 4)
local text_start = node.depth * 2 + 4
local gitlen = string.len(node.git)
if node.name == '..' then
highlight('LuaTreeFolderName', line, 0, -1)
elseif node.dir == true then
highlight('LuaTreeFolderIcon', line, 0, text_start)
highlight('LuaTreeFolderName', line, text_start + gitlen, -1)
if highlight_git(highlight, node, line, text_start + 4) == false then
highlight('LuaTreeFolderName', line, text_start + 4, -1)
end
else
highlight('LuaTreeFolderName', line, 0, -1)
end
return
elseif is_special(node.name) == true then
highlight('LuaTreeSpecialFile', line, text_start, -1)
text_start = text_start - 4
highlight('LuaTreeSpecialFile', line, text_start + gitlen, -1)
elseif is_executable(node.path .. node.name) then
highlight('LuaTreeExecFile', line, text_start, -1)
text_start = text_start - 4
highlight('LuaTreeExecFile', line, text_start + gitlen, -1)
elseif is_pic(node.path .. node.name) then
highlight('LuaTreeImageFile', line, text_start, -1)
text_start = text_start - 4
highlight('LuaTreeImageFile', line, text_start + gitlen, -1)
else
for k, v in pairs(HIGHLIGHT_GROUPS) do
if string.match(node.name, k) ~= nil then
highlight('LuaTree' .. v, line, 0, text_start + 4)
highlight_git(highlight, node, line, text_start + 4)
return
highlight('LuaTree' .. v, line, 0, text_start)
break
end
end
end
highlight_git(highlight, node, line, text_start)
if node.git == '' then return end
if node.git == '' then
highlight('LuaTreeGitDirty', line, text_start, text_start + gitlen)
elseif node.git == '' then
highlight('LuaTreeGitStaged', line, text_start, text_start + gitlen)
elseif node.git == '✓★ ' then
highlight('LuaTreeGitStaged', line, text_start, text_start + 3)
highlight('LuaTreeGitNew', line, text_start + 3, text_start + gitlen)
elseif node.git == '✓✗ ' then
highlight('LuaTreeGitStaged', line, text_start, text_start + 3)
highlight('LuaTreeGitDirty', line, text_start + 3, text_start + gitlen)
elseif node.git == '' then
highlight('LuaTreeGitMerge', line, text_start, text_start + gitlen)
elseif node.git == '' then
highlight('LuaTreeGitRenamed', line, text_start, text_start + gitlen)
elseif node.git == '' then
highlight('LuaTreeGitNew', line, text_start, text_start + gitlen)
end
end
end

View File

@@ -1,4 +1,5 @@
local function system(v) return vim.api.nvim_call_function('system', { v }) end
local function systemlist(v) return vim.api.nvim_call_function('systemlist', { v }) end
local function is_git_repo()
local is_git = system('git rev-parse')
@@ -9,7 +10,7 @@ local IS_GIT_REPO = is_git_repo()
local function set_git_status()
if IS_GIT_REPO == false then return '' end
return system('git status --porcelain=v1')
return systemlist('git status --porcelain=v1')
end
local GIT_STATUS = set_git_status()
@@ -26,31 +27,41 @@ local function force_refresh_git()
end
local function is_folder_dirty(relpath)
return string.match(GIT_STATUS, relpath) ~= nil
for _, status in pairs(GIT_STATUS) do
if string.match(status, relpath) ~= nil then return true end
end
end
local function create_git_checker(pattern)
return function(relpath)
local ret = string.match(GIT_STATUS, '^.*' .. relpath)
if ret == nil then return false end
return string.match(ret, pattern) ~= nil
for _, status in pairs(GIT_STATUS) do
local ret = string.match(status, '^.. ' .. relpath)
if ret ~= nil and string.match(ret, pattern) ~= nil then return true end
end
return false
end
end
local is_modified = create_git_checker('^ ?MM?')
local is_staged = create_git_checker('^ ?A')
local is_unmerged = create_git_checker('^ ?UU')
local is_untracked = create_git_checker('^%?%?')
local unstaged = create_git_checker('^ ')
local staged = create_git_checker('^M ')
local staged_new = create_git_checker('^A ')
local staged_mod = create_git_checker('^MM')
local unmerged = create_git_checker('^[U ][U ]')
local renamed = create_git_checker('^R')
local untracked = create_git_checker('^%?%?')
local function get_git_attr(path, is_dir)
if IS_GIT_REPO == false then return '' end
if is_dir then
if is_folder_dirty(path) == true then return 'Dirty' end
if is_folder_dirty(path) == true then return '' end
else
if is_modified(path) then return 'Modified'
elseif is_staged(path) then return 'Staged'
elseif is_unmerged(path) then return 'Unmerged'
elseif is_untracked(path) then return 'Untracked'
if unstaged(path) then return ''
elseif staged(path) then return ''
elseif staged_new(path) then return '✓★ '
elseif staged_mod(path) then return '✓✗ '
elseif unmerged(path) then return ''
elseif renamed(path) then return ''
elseif untracked(path) then return ''
end
end