Added git diff to statusline
This commit is contained in:
@@ -28,7 +28,7 @@ local function setup_statusline_highlights()
|
|||||||
derive_mode_hl('StatusModeCommand', 'PreProc')
|
derive_mode_hl('StatusModeCommand', 'PreProc')
|
||||||
derive_mode_hl('StatusModeReplace', 'WarningMsg')
|
derive_mode_hl('StatusModeReplace', 'WarningMsg')
|
||||||
derive_mode_hl('StatusModeTerminal', 'Special')
|
derive_mode_hl('StatusModeTerminal', 'Special')
|
||||||
derive_hl('StatusGit', { base = 'StatusLine', fg = 'LineNr' })
|
derive_hl('StatusGit', { base = 'CursorLine', fg = 'LineNr' })
|
||||||
derive_hl('StatusLsp', { base = 'StatusLine', fg = 'DiagnosticInfo' })
|
derive_hl('StatusLsp', { base = 'StatusLine', fg = 'DiagnosticInfo' })
|
||||||
derive_hl('StatusLspSpinner', { base = 'StatusLine', fg = 'Constant' })
|
derive_hl('StatusLspSpinner', { base = 'StatusLine', fg = 'Constant' })
|
||||||
derive_hl(
|
derive_hl(
|
||||||
@@ -37,7 +37,12 @@ local function setup_statusline_highlights()
|
|||||||
)
|
)
|
||||||
derive_hl('StatusDiagWarn', { base = 'StatusLine', fg = 'DiagnosticWarn' })
|
derive_hl('StatusDiagWarn', { base = 'StatusLine', fg = 'DiagnosticWarn' })
|
||||||
derive_hl('StatusDiagHint', { base = 'StatusLine', fg = 'DiagnosticHint' })
|
derive_hl('StatusDiagHint', { base = 'StatusLine', fg = 'DiagnosticHint' })
|
||||||
derive_hl('StatusCursor', { base = 'StatusLine', fg = 'Normal' })
|
derive_hl('StatusCursor', { base = 'CursorLine', fg = 'Normal' })
|
||||||
|
derive_hl('StatusGitAdded', { base = 'StatusLine', fg = 'OkMsg' })
|
||||||
|
derive_hl(
|
||||||
|
'StatusGitRemoved',
|
||||||
|
{ base = 'StatusLine', fg = 'DiagnosticError' }
|
||||||
|
)
|
||||||
end
|
end
|
||||||
vim.api.nvim_create_autocmd('ColorScheme', {
|
vim.api.nvim_create_autocmd('ColorScheme', {
|
||||||
callback = setup_statusline_highlights,
|
callback = setup_statusline_highlights,
|
||||||
@@ -81,7 +86,7 @@ local function update_git_branch()
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
vim.uv.new_timer():start(5000, 5000, update_git_branch)
|
vim.uv.new_timer():start(5000, 5000, vim.schedule_wrap(update_git_branch))
|
||||||
vim.api.nvim_create_autocmd({ 'BufEnter', 'FocusGained' }, {
|
vim.api.nvim_create_autocmd({ 'BufEnter', 'FocusGained' }, {
|
||||||
callback = function()
|
callback = function()
|
||||||
update_git_branch()
|
update_git_branch()
|
||||||
@@ -217,23 +222,66 @@ vim.api.nvim_create_autocmd(
|
|||||||
{ callback = update_diag_cache }
|
{ callback = update_diag_cache }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
local function update_git_diff()
|
||||||
|
if vim.bo.buftype ~= '' then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local file = vim.fn.expand('%:p')
|
||||||
|
if file == '' then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
vim.system(
|
||||||
|
{ 'git', 'diff', '--numstat', 'HEAD', '--', file },
|
||||||
|
{ text = true },
|
||||||
|
function(out)
|
||||||
|
if out.code ~= 0 or out.stdout == '' then
|
||||||
|
vim.b.git_diff = nil
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local added, removed = out.stdout:match('(%d+)%s+(%d+)')
|
||||||
|
vim.b.git_diff = {
|
||||||
|
added = tonumber(added) or 0,
|
||||||
|
removed = tonumber(removed) or 0,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
)
|
||||||
|
end
|
||||||
|
vim.api.nvim_create_autocmd({ 'BufEnter', 'FocusGained', 'BufWritePost' }, {
|
||||||
|
callback = update_git_diff,
|
||||||
|
})
|
||||||
|
|
||||||
function _G.__statusline()
|
function _G.__statusline()
|
||||||
local m = vim.fn.mode()
|
local m = vim.fn.mode()
|
||||||
local mode_hl = mode_hl_map[m] or 'StatusModeNormal'
|
local mode_hl = mode_hl_map[m] or 'StatusModeNormal'
|
||||||
local git = vim.b.git_branch or ''
|
local git = vim.b.git_branch or ''
|
||||||
local cur_buf = vim.api.nvim_get_current_buf()
|
local cur_buf = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local parts = {
|
local parts = {
|
||||||
'%#' .. mode_hl .. '#' .. (mode_map[m] or ' ?? ') .. '%*',
|
'%#' .. mode_hl .. '#' .. (mode_map[m] or ' ?? ') .. '%*',
|
||||||
git ~= '' and ('%#StatusGit# ' .. git .. ' %*') or '',
|
git ~= '' and ('%#StatusGit# ' .. git .. ' %*') or '',
|
||||||
lsp_section(),
|
|
||||||
'%=',
|
|
||||||
diag_cache[cur_buf] or build_diag_string(vim.diagnostic.count(cur_buf)),
|
|
||||||
'%#StatusCursor# %l:%-2c %*',
|
|
||||||
'%#' .. mode_hl .. '# %p%% %*',
|
|
||||||
}
|
}
|
||||||
|
local diff = vim.b.git_diff
|
||||||
|
if diff and (diff.added > 0 or diff.removed > 0) then
|
||||||
|
if diff.added > 0 then
|
||||||
|
table.insert(parts, '%#StatusGitAdded# +' .. diff.added .. ' %*')
|
||||||
|
end
|
||||||
|
if diff.removed > 0 then
|
||||||
|
table.insert(
|
||||||
|
parts,
|
||||||
|
'%#StatusGitRemoved# -' .. diff.removed .. ' %*'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(parts, lsp_section())
|
||||||
|
table.insert(parts, '%=')
|
||||||
|
table.insert(
|
||||||
|
parts,
|
||||||
|
diag_cache[cur_buf] or build_diag_string(vim.diagnostic.count(cur_buf))
|
||||||
|
)
|
||||||
|
table.insert(parts, '%#StatusCursor# %l:%-2c %*')
|
||||||
|
table.insert(parts, '%#' .. mode_hl .. '# %p%% %*')
|
||||||
return table.concat(parts)
|
return table.concat(parts)
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.o.statusline = '%!v:lua.__statusline()'
|
vim.o.statusline = '%!v:lua.__statusline()'
|
||||||
|
|
||||||
require('incline').setup({
|
require('incline').setup({
|
||||||
|
|||||||
Reference in New Issue
Block a user