2021-05-19 00:53:42 +02:00
|
|
|
|
-- Author : swytch
|
|
|
|
|
-- Created : Friday Mar 12, 2021 21:45:21 CET
|
|
|
|
|
-- License : GPLv3
|
|
|
|
|
-- Description : neovim statusline file
|
|
|
|
|
-- based on github/nihilistkitten's work
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function gen_section(hl_string, items)
|
2022-04-27 21:58:12 +02:00
|
|
|
|
local out = ""
|
|
|
|
|
for _, item in pairs(items) do
|
|
|
|
|
if item ~= "" then
|
|
|
|
|
out = out .. " " .. item
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
2022-04-27 21:58:12 +02:00
|
|
|
|
end
|
|
|
|
|
if out ~="" then
|
|
|
|
|
return hl_string .. out .. " "
|
|
|
|
|
else
|
|
|
|
|
return out
|
|
|
|
|
end
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- sensible names for vim modes
|
|
|
|
|
local function get_mode(m)
|
2022-04-27 21:58:12 +02:00
|
|
|
|
local mode_group = {
|
|
|
|
|
["n"] = "Normal",
|
|
|
|
|
["no"] = "O-Pending",
|
|
|
|
|
["nov"] = "O-Pending",
|
|
|
|
|
["noV"] = "O-Pending",
|
|
|
|
|
["no"] = "O-Pending",
|
|
|
|
|
["niI"] = "Normal",
|
|
|
|
|
["niR"] = "Normal",
|
|
|
|
|
["niV"] = "Normal",
|
|
|
|
|
["v"] = "Visual",
|
|
|
|
|
["V"] = "V-Line",
|
|
|
|
|
[""] = "V-Block",
|
|
|
|
|
["s"] = "Select",
|
|
|
|
|
["S"] = "S-Line",
|
|
|
|
|
[""] = "S-Block",
|
|
|
|
|
["i"] = "Insert",
|
|
|
|
|
["ic"] = "Insert",
|
|
|
|
|
["ix"] = "Insert",
|
|
|
|
|
["R"] = "Replace",
|
|
|
|
|
["Rc"] = "Replace",
|
|
|
|
|
["Rv"] = "V-Replace",
|
|
|
|
|
["Rx"] = "Replace",
|
|
|
|
|
["c"] = "Command",
|
|
|
|
|
["cv"] = "Ex",
|
|
|
|
|
["ce"] = "Ex",
|
|
|
|
|
["r"] = "Prompt",
|
|
|
|
|
["rm"] = "Prompt",
|
|
|
|
|
["r?"] = "Prompt",
|
|
|
|
|
["!"] = "Shell",
|
|
|
|
|
["t"] = "Terminal",
|
|
|
|
|
}
|
|
|
|
|
return mode_group[m] or "None"
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function get_mode_display_name(m)
|
2022-04-27 21:58:12 +02:00
|
|
|
|
local mode = {
|
|
|
|
|
["Normal"] = "[ NRM ]",
|
|
|
|
|
["O-Pending"] = "[ OTR ]",
|
|
|
|
|
["Visual"] = "[ VSL ]",
|
|
|
|
|
["V-Line"] = "[ V·L ]",
|
|
|
|
|
["V-Block"] = "[ V·B ]",
|
|
|
|
|
["Select"] = "[ SEL ]",
|
|
|
|
|
["S-Line"] = "[ S·L ]",
|
|
|
|
|
["S-Block"] = "[ S·B ]",
|
|
|
|
|
["Insert"] = "[ INS ]",
|
|
|
|
|
["Replace"] = "[ RPL ]",
|
|
|
|
|
["Command"] = "[ CMD ]",
|
|
|
|
|
["Prompt"] = "[ PMT ]",
|
|
|
|
|
["Shell"] = "[ SHL ]",
|
|
|
|
|
["Terminal"] = "[ TRM ]",
|
|
|
|
|
["None"] = "[ --- ]"
|
|
|
|
|
}
|
|
|
|
|
return mode[m]
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- get the highlight group for a mode group
|
|
|
|
|
local function get_mode_color(m)
|
2022-04-27 21:58:12 +02:00
|
|
|
|
local color = {
|
|
|
|
|
["Normal"] = "%#NormalMode#",
|
|
|
|
|
["O-Pending"] = "%#NormalMode#",
|
|
|
|
|
["Visual"] = "%#VisualMode#",
|
|
|
|
|
["V-Line"] = "%#VisualMode#",
|
|
|
|
|
["V-Block"] = "%#VisualMode#",
|
|
|
|
|
["Select"] = "%#NormalMode#",
|
|
|
|
|
["S-Line"] = "%#NormalMode#",
|
|
|
|
|
["S-Block"] = "%#NormalMode#",
|
|
|
|
|
["Insert"] = "%#InsertMode#",
|
|
|
|
|
["Replace"] = "%#ReplaceMode#",
|
|
|
|
|
["Command"] = "%#CommandMode#",
|
|
|
|
|
["Prompt"] = "%#NormalMode#",
|
|
|
|
|
["Shell"] = "%#NormalMode#",
|
|
|
|
|
["Terminal"] = "%#NormalMode#",
|
|
|
|
|
["None"] = "%#NormalMode#"
|
|
|
|
|
}
|
|
|
|
|
return color[m]
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- from https://github.com/nvim-lua/lsp-status.nvim/blob/master/lua/lsp-status/diagnostics.lua
|
2022-05-02 23:32:25 +02:00
|
|
|
|
local function get_lsp_diagnostics()
|
2022-04-27 21:58:12 +02:00
|
|
|
|
local result = {}
|
|
|
|
|
local levels = {
|
2022-06-08 10:29:35 +02:00
|
|
|
|
error = "Error",
|
|
|
|
|
warn = "Warn",
|
2022-04-27 21:58:12 +02:00
|
|
|
|
info = "Info",
|
2022-06-08 10:29:35 +02:00
|
|
|
|
hint = "Hint"
|
2022-04-27 21:58:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for k, level in pairs(levels) do
|
|
|
|
|
local count = 0
|
2022-05-02 23:32:25 +02:00
|
|
|
|
local diags = vim.diagnostic.get(0, { severity = level })
|
2022-04-27 21:58:12 +02:00
|
|
|
|
for _, _ in pairs(diags) do
|
|
|
|
|
count = count + 1
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
2022-04-27 21:58:12 +02:00
|
|
|
|
result[k] = count
|
|
|
|
|
end
|
2021-05-19 00:53:42 +02:00
|
|
|
|
|
2022-04-27 21:58:12 +02:00
|
|
|
|
return result
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function process_diagnostics(prefix, n, hl)
|
2022-04-27 21:58:12 +02:00
|
|
|
|
if n > 0 then
|
|
|
|
|
return hl .. prefix .. n
|
|
|
|
|
else
|
|
|
|
|
return ""
|
|
|
|
|
end
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function spell_check()
|
2022-04-27 21:58:12 +02:00
|
|
|
|
if vim.wo.spell then
|
|
|
|
|
local lang = vim.o.spelllang
|
|
|
|
|
return "[SPELL=" .. lang .. "]"
|
|
|
|
|
else
|
|
|
|
|
return ""
|
|
|
|
|
end
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function statusline_focused()
|
2022-04-27 21:58:12 +02:00
|
|
|
|
local diagnostics = get_lsp_diagnostics()
|
|
|
|
|
local mode = vim.fn.mode()
|
|
|
|
|
local mg = get_mode(mode)
|
|
|
|
|
local accent_color = get_mode_color(mg)
|
|
|
|
|
|
|
|
|
|
local left = table.concat {
|
|
|
|
|
gen_section(accent_color, {get_mode_display_name(mg)}),
|
|
|
|
|
gen_section("%#Middle#", {"%f"}),
|
|
|
|
|
gen_section("%#Bottom#", {"%m", "%r"}),
|
|
|
|
|
gen_section(
|
|
|
|
|
"%#Alert#",
|
|
|
|
|
{
|
|
|
|
|
process_diagnostics(
|
|
|
|
|
globals.sign_error .. " ",
|
2022-06-08 10:29:35 +02:00
|
|
|
|
diagnostics.error,
|
2022-04-27 21:58:12 +02:00
|
|
|
|
"%#DiagnosticVirtualTextError#"
|
|
|
|
|
),
|
|
|
|
|
process_diagnostics(
|
2022-06-08 10:29:35 +02:00
|
|
|
|
globals.sign_warn .. " ",
|
|
|
|
|
diagnostics.warn,
|
2022-04-27 21:58:12 +02:00
|
|
|
|
"%#DiagnosticVirtualTextWarn#"
|
|
|
|
|
),
|
|
|
|
|
process_diagnostics(
|
|
|
|
|
globals.sign_info .. " ",
|
|
|
|
|
diagnostics.info,
|
|
|
|
|
"%#DiagnosticVirtualTextInfo#"
|
|
|
|
|
)
|
2021-05-19 00:53:42 +02:00
|
|
|
|
}
|
2022-04-27 21:58:12 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
local right = table.concat {
|
|
|
|
|
gen_section(
|
|
|
|
|
"%#Bottom#",
|
|
|
|
|
{
|
|
|
|
|
spell_check(),
|
|
|
|
|
vim.bo.filetype
|
2021-05-19 00:53:42 +02:00
|
|
|
|
}
|
2022-04-27 21:58:12 +02:00
|
|
|
|
),
|
|
|
|
|
gen_section("%#Middle#", {"%03.p%%"}),
|
|
|
|
|
gen_section("%#Top#", {"-%03.c-"})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return table.concat {
|
|
|
|
|
left,
|
|
|
|
|
"%#Statusline#",
|
|
|
|
|
"%=",
|
|
|
|
|
right
|
|
|
|
|
}
|
2021-05-19 00:53:42 +02:00
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function statusline_not_focused()
|
2022-04-27 21:58:12 +02:00
|
|
|
|
return table.concat {
|
|
|
|
|
gen_section("%#StatuslineNF#", {"%f", "%m"}),
|
|
|
|
|
"%=",
|
|
|
|
|
gen_section("%#StatuslineNF#", {"%03.p%%"}),
|
|
|
|
|
gen_section("%#StatuslineNF#", {"-%03.c-"})
|
|
|
|
|
}
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-05-21 21:45:36 +02:00
|
|
|
|
function _G.gen_statusline()
|
2022-04-27 21:58:12 +02:00
|
|
|
|
if vim.g.statusline_winid == vim.fn.win_getid() then
|
|
|
|
|
return statusline_focused()
|
|
|
|
|
else
|
|
|
|
|
return statusline_not_focused()
|
|
|
|
|
end
|
2021-05-19 00:53:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
vim.o.statusline = "%!luaeval(\"gen_statusline()\")"
|