diff options
author | ben <ben@nagy.contact> | 2025-01-10 10:41:02 -0800 |
---|---|---|
committer | ben <ben@nagy.contact> | 2025-01-10 10:41:02 -0800 |
commit | d1fd442c43d5313b916ca159fcf8fdc3cc871785 (patch) | |
tree | 774b135b8b44afb3b6f9679fed00a8ef63731c48 /nvim/lua/core/statusline.lua | |
parent | b33651ac7bd821efd4c5ec67f22cc53780da32a3 (diff) |
fixed neovim cursor disappearance during sustained motions
- synchronous git calls -> async. in statusline
- cached git branch in statusline
Diffstat (limited to 'nvim/lua/core/statusline.lua')
-rw-r--r-- | nvim/lua/core/statusline.lua | 101 |
1 files changed, 64 insertions, 37 deletions
diff --git a/nvim/lua/core/statusline.lua b/nvim/lua/core/statusline.lua index 45a2c42..cc4bd09 100644 --- a/nvim/lua/core/statusline.lua +++ b/nvim/lua/core/statusline.lua @@ -1,48 +1,75 @@ local M = {} -local function git_branch() - local git_dir = vim.fn.system("git rev-parse --git-dir 2>/dev/null") - if vim.v.shell_error ~= 0 then - return "" - end - local branch = vim.fn.system("git branch --show-current 2>/dev/null | tr -d '\n'") - return branch +local branch_cache = "" +local last_check = 0 +local check_interval = 1000 -- Check git branch every 1 second + +local function async_git_branch() + local check_time = vim.loop.now() + if check_time - last_check < check_interval then + return branch_cache + end + last_check = check_time + + local stdout = vim.loop.new_pipe(false) + local stderr = vim.loop.new_pipe(false) + + local handle + handle = vim.loop.spawn("git", { + args = { "branch", "--show-current" }, + stdio = { nil, stdout, stderr } + }, function(code, signal) + stdout:close() + stderr:close() + handle:close() + end) + + if handle then + stdout:read_start(vim.schedule_wrap(function(err, data) + if data then + branch_cache = data:gsub("\n", "") + end + end)) + end + + return branch_cache end function M.statusline_git() - local branch_name = git_branch() - return #branch_name > 0 and ' ' .. branch_name .. ' ' or '' + local branch_name = async_git_branch() + return #branch_name > 0 and ' ' .. branch_name .. ' ' or '' end +-- Rest of your statusline.lua remains exactly the same function M.setup() - vim.cmd('highlight clear StatusLine') - vim.cmd('highlight clear StatusLineNC') - - local highlights = { - StatusLineGit = { bg = "#161616", fg = "#D8DEE9" }, - StatusLineBuffer = { bg = "NONE", fg = "#D8DEE9" }, - StatusLine = { bg = "NONE", fg = "#D8DEE9" }, - StatusLineFiletype = { bg = "NONE", fg = "#D8DEE9" }, - StatusLinePath = { bg = "NONE", fg = "#D8DEE9" }, - StatusLineModified = { bg = "NONE", fg = "#D8DEE9" }, - } - - for name, colors in pairs(highlights) do - vim.api.nvim_set_hl(0, name, colors) - end - - vim.opt.statusline = table.concat({ - "%#StatusLineGit#%{v:lua.require'core.statusline'.statusline_git()}", - "%#StatusLineBuffer# %n ", - "%#StatusLine#%{&ff} ", - "%#StatusLineFiletype#%y", - "%#StatusLinePath# %<%{expand('%:p:h')}/%f", - "%#StatusLineModified#%m", - "%=", - "%#StatusLine#%5l", - "/%L:%v ", - "%{char2nr(getline('.')[col('.')-1])} " - }) + vim.cmd('highlight clear StatusLine') + vim.cmd('highlight clear StatusLineNC') + + local highlights = { + StatusLineGit = { bg = "#161616", fg = "#D8DEE9" }, + StatusLineBuffer = { bg = "NONE", fg = "#D8DEE9" }, + StatusLine = { bg = "NONE", fg = "#D8DEE9" }, + StatusLineFiletype = { bg = "NONE", fg = "#D8DEE9" }, + StatusLinePath = { bg = "NONE", fg = "#D8DEE9" }, + StatusLineModified = { bg = "NONE", fg = "#D8DEE9" }, + } + + for name, colors in pairs(highlights) do + vim.api.nvim_set_hl(0, name, colors) + end + + vim.opt.statusline = table.concat({ + "%#StatusLineGit#%{v:lua.require'core.statusline'.statusline_git()}", + "%#StatusLineBuffer# %n ", + "%#StatusLine#%{&ff} ", + "%#StatusLineFiletype#%y", + "%#StatusLinePath# %<%{expand('%:p:h')}/%f", + "%#StatusLineModified#%m", + "%=", + "%#StatusLine#%5l", + "/%L:%v ", + "%{char2nr(getline('.')[col('.')-1])} " + }) end vim.api.nvim_create_autocmd("ColorScheme", { |