1
0
Fork 0
numbers/nvim/plugin/statusline.lua

166 lines
3.9 KiB
Lua

-- setup truncation limits
local truncation_limit = 120
-- is buffer horizontally truncated
Is_htruncated = function(width)
local current_width = vim.api.nvim_win_get_width(0)
return current_width < width
end
-- diagnostics symbol config
local symbol_config = {
indicator_hint = "",
indicator_info = "",
indicator_warning = "",
indicator_error = "",
}
-- statusline colors
local statusline_colors = {
active = "%#StatusLine#",
inactive = "%#StatusLineNC#",
git = "%#diffChanged#",
err = "%#DiagnosticSignError#",
warn = "%#DiagnosticSignWarn#",
nfo = "%#DiagnosticSignInfo#",
hnt = "%#DiagnosticSignHint#",
line_col = "%#Delimiter#",
percentage = "%#Delimiter#",
}
-- get git information of current file
local get_git_status = function()
local meta = {}
local gitsigns_summary = vim.b.gitsigns_status_dict
if not gitsigns_summary then
return ""
end
meta["branch"] = gitsigns_summary["head"]
meta["added"] = gitsigns_summary["added"]
meta["modified"] = gitsigns_summary["changed"]
meta["removed"] = gitsigns_summary["removed"]
if Is_htruncated(truncation_limit) then
return string.format(" %s", meta["branch"])
else
return string.format("  %s +%s ~%s -%s", meta["branch"], meta["added"], meta["modified"], meta["removed"])
end
end
-- get current file name
local get_filename = function()
if Is_htruncated(truncation_limit) then
return "%t "
end
return "%<%F "
end
-- get current line/col
local get_line_col = function()
return " %l:%c"
end
-- get current percentage through file
local get_percentage = function()
if Is_htruncated(truncation_limit) then
return ""
else
return " %p%%"
end
end
local get_flags = function()
return "%h%w%m "
end
-- get current file diagnostics
local get_diagnostics = function()
if #vim.lsp.buf_get_clients(0) == 0 then
return ""
end
local status_parts = {}
if not Is_htruncated(truncation_limit) then
local warnings = vim.diagnostic.get(0, { severity = vim.diagnostic.severity.WARN })
local hints = vim.diagnostic.get(0, { severity = vim.diagnostic.severity.HINT })
local infos = vim.diagnostic.get(0, { severity = vim.diagnostic.severity.INFO })
local errors = vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR })
if errors > 0 then
table.insert(status_parts, statusline_colors.err .. symbol_config.indicator_error .. errors)
end
if warnings > 0 then
table.insert(status_parts, statusline_colors.warn .. symbol_config.indicator_warning .. warnings)
end
if infos > 0 then
table.insert(status_parts, statusline_colors.nfo .. symbol_config.indicator_info .. infos)
end
if hints > 0 then
table.insert(status_parts, statusline_colors.hnt .. symbol_config.indicator_hint .. hints)
end
end
local status_diagnostics = vim.trim(table.concat(status_parts, " "))
if status_diagnostics ~= "" then
return status_diagnostics .. " "
end
return ""
end
-- inactive statusline
local statusline_inactive = function()
local flags = get_flags()
local filename = get_filename()
local line_col = get_line_col()
local percentage = get_percentage()
return table.concat({
statusline_colors.inactive,
flags,
filename,
"%=% ",
line_col,
percentage,
})
end
-- active statusline
local statusline_normal = function()
-- local diagnostics = get_diagnostics()
local flags = statusline_colors.active .. get_flags()
local filename = statusline_colors.active .. get_filename()
local git = statusline_colors.git .. get_git_status()
local line_col = statusline_colors.line_col .. get_line_col()
local percentage = statusline_colors.percentage .. get_percentage()
return table.concat({
-- diagnostics,
flags,
"%=% ",
filename,
"%=% ",
git,
line_col,
percentage,
})
end
-- module
M = {}
-- generate statusline
StatusLine = function()
return statusline_normal()
end
M.StatusLine = StatusLine
-- generate inactive statusline
StatusLineInactive = function()
return statusline_inactive()
end
M.StatusLineInactive = StatusLineInactive
return M