nvim/lua/ui/statusbar.lua

114 lines
2.8 KiB
Lua
Raw Normal View History

-- see `:help` for any questions
-- use `&<var>` to show value of vimscript variable
-- API --
local o = vim.o -- options
local go = vim.go -- only-global options
local bo = vim.bo -- buffer local options
local wo = vim.wo -- window local options
local cmd = vim.cmd -- vim commands
local fn = vim.fn -- vim functions
local opt = vim.opt -- vim option object
local g = vim.g -- global variables
local b = vim.b -- buffer local variables
local w = vim.w -- window local variables
local t = vim.t -- tab local variables
local v = vim.v -- variables
local env = vim.env -- environment variables
--Later generalize into plugin
-- read from bottom up to understand `status_bar()`
local function file_state()
if vim.bo.modified then
return "+"
elseif vim.bo.readonly then
return "-"
end
return ""
end
local function diagnostics()
local buffer_number = vim.fn.bufnr('%')
local severity_levels = {
-- level,prefix
errors = {'Error', 'E:'},
warnings = {'Warning', 'W:'},
info = {'Information', 'I:'},
hints = {'Hint', 'H:'}
}
local out = ''
for _,v in pairs(severity_levels) do
local d = vim.lsp.diagnostic.get_count(
buffer_number,
v[1] -- level
)
if d > 0 then
out = out .. v[2] .. d .. ' '
end
end
return out
end
local function highlight(group, color)
cmd('highlight ' .. group .. ' cterm='..color .. ' gui='..color)
end
highlight('StatusLine', 'bold,reverse')
highlight('StatusLineDark', 'bold')
local light_highlight = '%#StatusLine#'
local dark_highlight = '%#StatusLineDark#'
function section(items)
local out = ''
for _,item in pairs(items) do
if item ~= '' and item ~= nil then
if out == '' then
out = item
else
out = out .. ' | ' .. item
end
end
end
return ' ' .. out .. ' '
end
function sections(items)
local out = {}
local n = 1
for _,item in pairs(items) do
if item == '%=' then
table.insert(out, item)
n = 1 -- reset coloring at different parts
else
if (n % 2) == 1 then
table.insert(out, light_highlight..section(item))
else
table.insert(out, dark_highlight..section(item))
end
n = n + 1
end
end
return out
end
function status_bar()
return table.concat(sections({
-- Stage Left
{'%f', file_state()},
{diagnostics()},
'%=',
-- Stage Right
{
vim.b.gitsigns_status,
vim.bo.filetype
},
{'%p%%'},
{'%c,%l'}
}))
end