local highlight = require("rc.highlight") local line = require("rc.line") local ui = require("rc.ui") local M = {} local normalize_path do if package.config:sub(1, 1) == "\\" then ---@param s string function normalize_path(s) s = vim.fs.normalize(s) return s:sub(2, 3) == ":/" and ("%s%s"):format(s:sub(1, 1):upper(), s:sub(2)) or s end else ---@param s string function normalize_path(s) return vim.fs.normalize(s) end end end local replace_homedir do local home = vim.uv.os_homedir() if home then home = normalize_path(home) ---@param s string function replace_homedir(s) return s:sub(1, home:len()) == home and ("~%s"):format(s:sub(home:len() + 1)) or s end else ---@param s string function replace_homedir(s) return s end end end ---@param s string ---@param buf integer local function replace_cwd(s, buf) local cwd = normalize_path(vim.fn.getcwd(vim.fn.bufwinnr(buf))) return s:sub(1, cwd:len()) == cwd and s:sub(cwd:len() + 2) or s end ---@param buf integer local function current_file(buf) return replace_homedir(replace_cwd(normalize_path(vim.api.nvim_buf_get_name(buf)), buf)) end ---@param buf number local function diagnostics(buf) local severity = vim.diagnostic.severity local hl_map = { [severity.ERROR] = "DiagnosticError", [severity.WARN] = "DiagnosticWarn", [severity.INFO] = "DiagnosticInfo", [severity.HINT] = "DiagnosticHint", } local severities = { severity.HINT, severity.WARN, severity.ERROR, } local r = " " for _, s in pairs(severities) do local count = #vim.diagnostic.get(buf, { severity = s }) if count == 0 then r = r .. line.with_hl("󱓼 ", "WinBar") else local level = math.min(20 + count * 8, 100) r = r .. line.with_hl( "󱓻 ", highlight.ensure("WinBar" .. hl_map[s] .. level, function() return highlight.get("Whitespace"):mix(hl_map[s], level) end) ) end end return r end local function get_winbar(buf) local s = " " .. current_file(buf):gsub("/", ui.winbar_sep.path) .. "%=" .. diagnostics(buf) return s end function M.setup() vim.api.nvim_create_autocmd("DiagnosticChanged", { callback = function(cx) vim.api.nvim__redraw({ winbar = true, buf = cx.buf }) end, }) vim.api.nvim_create_autocmd("BufRead", { pattern = "*", callback = function(cx) vim.opt_local.winbar = line.as_opt(get_winbar, tostring(cx.buf)) end, }) end return M