local modes = { ["n"] = { "NORMAL", "ColorColumn" }, ["niI"] = { "NORMAL i", "ColorColumn" }, ["niR"] = { "NORMAL r", "ColorColumn" }, ["niV"] = { "NORMAL v", "ColorColumn" }, ["no"] = { "N-PENDING", "ColorColumn" }, ["i"] = { "INSERT", "StatusInsert" }, ["ic"] = { "INSERT (completion)", "StatusInsert" }, ["ix"] = { "INSERT completion", "StatusInsert" }, ["t"] = { "TERMINAL", "StatusTerminal" }, ["nt"] = { "NTERMINAL", "StatusTerminal" }, ["v"] = { "VISUAL", "StatusVisual" }, ["V"] = { "V-LINE", "StatusVisual" }, ["Vs"] = { "V-LINE (Ctrl O)", "StatusVisual" }, [""] = { "V-BLOCK", "StatusVisual" }, ["R"] = { "REPLACE", "StatusReplace" }, ["Rv"] = { "V-REPLACE", "StatusReplace" }, ["s"] = { "SELECT", "StatusVisual" }, ["S"] = { "S-LINE", "StatusVisual" }, [""] = { "S-BLOCK", "StatusVisual" }, ["c"] = { "COMMAND", "StatusCommand" }, ["cv"] = { "COMMAND", "StatusCommand" }, ["ce"] = { "COMMAND", "StatusCommand" }, ["r"] = { "PROMPT", "StatusVisual" }, ["rm"] = { "MORE", "StatusVisual" }, ["r?"] = { "CONFIRM", "StatusVisual" }, ["!"] = { "SHELL", "StatusTerminal" }, } local function current_mode() local mode = vim.api.nvim_get_mode().mode return "%#Normal#" .. " " .. "%#" .. modes[mode][2] .. "#" .. " " .. modes[mode][1] .. " " .. "%#NormalNC#" end local function get_current_file_name() local file = vim.fn.expand("%:t") if vim.fn.empty(file) == 1 then return "" end return "%#Normal#" .. " " .. file .. " " .. "%#NormalNC#" end local function git_branch() -- Safely check if gitsigns status is available local git_status = vim.b.gitsigns_status_dict if not git_status or not git_status.head or git_status.head == "" then return "" end local branch_name = "(λ • " .. git_status.head .. ")" return "%#Normal#" .. branch_name .. "%#NormalNC#" end local function git_diff() -- Safely check if gitsigns status is available local git_status = vim.b.gitsigns_status_dict if not git_status then return "" end local added, changed, removed = "", "", "" if git_status.added and git_status.added > 0 then added = "%#DiagnosticSignInfo#" .. " +" .. git_status.added end if git_status.changed and git_status.changed > 0 then changed = "%#DiagnosticSignWarn#" .. " ~" .. git_status.changed end if git_status.removed and git_status.removed > 0 then removed = "%#DiagnosticSignError#" .. " -" .. git_status.removed end if added ~= "" or changed ~= "" or removed ~= "" then return "%#Normal#" .. added .. changed .. removed .. "%#NormalNC#" .. " " else return " " end end local function macro_status() local rec = vim.fn.reg_recording() if rec ~= "" then return "%#Normal#" .. "@" .. rec .. " " .. "%#NormalNC#" else return "%#NormalNC#" end end local function search_count() if vim.v.hlsearch == 0 then return "" end local ok, result = pcall(vim.fn.searchcount, { maxcount = 999, timeout = 500 }) if not ok or next(result) == nil then return "" end local denominator = math.min(result.total, result.maxcount) return string.format("%d/%d", result.current, denominator) end local function buffer_diagnostics() local errors = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR }) local ERROR = "%#DiagnosticSignError#" .. errors .. "%#NormalNC#" .. " " local warnings = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.WARN }) local WARNING = "%#DiagnosticSignWarn#" .. warnings .. "%#NormalNC#" .. " " local info = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.INFO }) local INFO = (info and info > 0) and ("%#DiagnosticSignInfo#" .. info .. "%#NormalNC#") or "" .. " " local hint = #vim.diagnostic.get(0, { severity = vim.diagnostic.severity.HINT }) local HINT = (hint and hint > 0) and ("%#DiagnosticSignHint#" .. hint .. "%#NormalNC#") or "" .. " " return "%#Normal#" .. HINT .. INFO .. ERROR .. WARNING .. "%#NormalNC#" .. " " end local function file_type() local type = vim.bo.filetype return "%#Normal#" .. type .. "%#NormalNC#" .. " " end local function cursor_position() local row = vim.fn.line(".") local column = vim.fn.col(".") return row .. ":" .. column .. " " end -- StatusLine Modes Status = function() return table.concat({ current_mode(), get_current_file_name(), git_branch(), git_diff(), "%=", -- right align macro_status(), search_count(), buffer_diagnostics(), file_type(), cursor_position(), }) end -- Execute statusline vim.api.nvim_create_autocmd({ "BufWinEnter" }, { pattern = "*", command = "setlocal statusline=%!v:lua.Status()", })