Skip to content

Commit

Permalink
perf(heirline.nvim): async query git branch name, remove calling 'git…
Browse files Browse the repository at this point in the history
…-prompt-string' (#544)
  • Loading branch information
linrongbin16 authored Apr 26, 2024
1 parent 6b55dba commit 1271988
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 145 deletions.
193 changes: 52 additions & 141 deletions lua/configs/rebelot/heirline-nvim/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,15 @@ local FileName = {
},
}

local git_branch_name_cache = nil
local GitBranch = {
hl = { fg = "normal_fg3", bg = "normal_bg3" },
update = { "FocusGained", "TermLeave", "TermClose" },
update = { "User", pattern = "HeirlineGitBranchUpdated" },

{
provider = function(self)
if vim.fn.exists("*gitbranch#name") > 0 then
local branch = vim.fn["gitbranch#name"]()
if str.not_empty(branch) then
return "" .. branch .. " "
end
if str.not_empty(git_branch_name_cache) then
return "" .. git_branch_name_cache .. " "
end
return ""
end,
Expand All @@ -241,59 +239,6 @@ local GitBranch = {
},
}

local git_prompt_string_colors = {
black = "diagnostic_hint",
white = "text_fg",
red = "git_delete",
green = "git_add",
blue = "replace_bg",
cyan = "diagnostic_info",
grey = "diagnostic_hint",
orange = "git_change",
yellow = "git_change",
purple = "normal_fg3",
magenta = "normal_fg3",
bright_black = "diagnostic_hint",
bright_red = "text_fg",
bright_green = "git_add",
bright_yellow = "git_change",
bright_blue = "replace_bg",
bright_magenta = "normal_fg3",
bright_cyan = "diagnostic_info",
bright_white = "text_fg",
}

local git_prompt_string_value_cache = nil --[[@as string?]]
local git_prompt_string_color_cache = nil
local GitPromptString = {
hl = { fg = "normal_fg3", bg = "normal_bg3" },
update = { "User", pattern = "HeirlineGitPromptStringUpdated" },

{
provider = function(self)
if str.not_empty(git_prompt_string_value_cache) then
---@diagnostic disable-next-line
local result = git_prompt_string_value_cache:gsub("%%", "%%%%")
return result .. " "
end
return ""
end,
hl = function(self)
if str.not_empty(git_prompt_string_color_cache) then
local c = str.replace(git_prompt_string_color_cache, "-", "_")
if git_prompt_string_colors[c] then
return { fg = git_prompt_string_colors[c], bg = "normal_bg3" }
end
end
return { fg = "normal_fg3", bg = "normal_bg3" }
end,
},
{
provider = right_slant,
hl = { fg = "normal_bg3", bg = "normal_bg4" },
},
}

local GitDiff = {
init = function(self)
self.summary = {}
Expand Down Expand Up @@ -581,7 +526,7 @@ local Progress = {
local StatusLine = {
Mode,
FileName,
vim.fn.executable("git-prompt-string") > 0 and GitPromptString or GitBranch,
GitBranch,
GitDiff,
LspStatus,
{ provider = "%=", hl = { fg = "normal_fg4", bg = "normal_bg4" } },
Expand Down Expand Up @@ -1050,92 +995,58 @@ vim.api.nvim_create_autocmd("VimEnter", {
end,
})

local running_git_prompt_string = false
if vim.fn.executable("git-prompt-string") > 0 then
local function update_git_branch_info()
if running_git_prompt_string then
return
end
running_git_prompt_string = true

local cwd
local bufname
local bufnr = vim.api.nvim_get_current_buf()
if type(bufnr) == "number" and bufnr > 0 then
bufname = vim.api.nvim_buf_get_name(bufnr)
if type(bufname) == "string" and string.len(bufname) > 0 then
local bufdir = vim.fn.fnamemodify(bufname, ":h")
if
type(bufdir) == "string"
and string.len(bufdir) > 0
and vim.fn.isdirectory(bufdir) > 0
then
cwd = bufdir
end
local function get_buffer_dir()
local bufnr = vim.api.nvim_get_current_buf()
if type(bufnr) == "number" and bufnr > 0 then
local bufname = vim.api.nvim_buf_get_name(bufnr)
if type(bufname) == "string" and string.len(bufname) > 0 then
local bufdir = vim.fn.fnamemodify(bufname, ":h")
if type(bufdir) == "string" and string.len(bufdir) > 0 and vim.fn.isdirectory(bufdir) > 0 then
return bufdir
end
end
end

local branch = nil
local failed_get_branch = false
spawn.run({ "git-prompt-string", "-color-disabled" }, {
cwd = cwd,
on_stdout = function(line)
if type(line) == "string" then
branch = line
end
end,
on_stderr = function()
failed_get_branch = true
end,
}, function()
if failed_get_branch then
vim.schedule(function()
running_git_prompt_string = false
end)
return
end
return nil
end

if type(branch) == "string" then
git_prompt_string_value_cache = branch
end
local running_git_branch_info = false
local function update_git_branch()
if running_git_branch_info then
return
end
running_git_branch_info = true

local branch_info = ""
local failed_get_branch_info = false
spawn.run({ "git-prompt-string", "-json" }, {
cwd = cwd,
on_stdout = function(line)
if type(line) == "string" then
branch_info = branch_info .. line
end
end,
on_stderr = function()
failed_get_branch_info = true
end,
}, function()
if not failed_get_branch_info and str.not_empty(branch_info) then
local ok, j = pcall(vim.json.decode, branch_info)
if ok and str.not_empty(tbl.tbl_get(j, "color")) then
git_prompt_string_color_cache = j["color"]
end
end
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "HeirlineGitPromptStringUpdated",
modeline = false,
})
vim.schedule(function()
running_git_prompt_string = false
end)
end)
local cwd = get_buffer_dir()
local branch_name = nil
local failed_get_branch_name = false
spawn.run({ "git", "branch", "--show-current" }, {
cwd = cwd,
on_stdout = function(line)
if type(line) == "string" then
branch_name = line
end
end,
on_stderr = function()
branch_name = nil
end,
}, function(git_completed)
if not failed_get_branch_name and tbl.tbl_get(git_completed, "code") == 0 then
git_branch_name_cache = branch_name
end
vim.schedule(function()
vim.api.nvim_exec_autocmds("User", {
pattern = "HeirlineGitBranchUpdated",
modeline = false,
})
vim.schedule(function()
running_git_branch_info = false
end)
end)
end

vim.api.nvim_create_autocmd(
{ "FocusGained", "TermLeave", "TermClose", "BufEnter", "CursorHold", "CursorMoved" },
{
group = "heirline_augroup",
callback = update_git_branch_info,
}
)
end)
end

vim.api.nvim_create_autocmd({ "FocusGained", "TermLeave", "TermClose", "BufEnter", "CursorHold" }, {
group = "heirline_augroup",
callback = update_git_branch,
})
4 changes: 0 additions & 4 deletions lua/plugins/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ local M = {
keys = lua_keys("airblade/vim-gitgutter"),
},
-- Statusline
{
"itchyny/vim-gitbranch",
event = { VeryLazy },
},
{
"linrongbin16/lsp-progress.nvim",
lazy = true,
Expand Down

0 comments on commit 1271988

Please sign in to comment.