feat(lualine): pretty_path now highlights file basename when modified

This commit is contained in:
Folke Lemaitre 2023-10-15 22:37:49 +02:00
parent 782fe0bef0
commit 8af7309c7e
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 69 additions and 37 deletions

View file

@ -58,6 +58,10 @@ function M.bufpath(buf)
return M.realpath(vim.api.nvim_buf_get_name(assert(buf)))
end
function M.cwd()
return M.realpath(vim.loop.cwd()) or ""
end
function M.realpath(path)
if path == "" or path == nil then
return nil
@ -144,38 +148,9 @@ function M.get()
return roots[1] and roots[1].paths[1] or vim.loop.cwd()
end
M.pretty_cache = {} ---@type table<string, string>
function M.pretty_path()
local path = vim.fn.expand("%:p") --[[@as string]]
if path == "" then
return ""
end
path = Util.norm(path)
if M.pretty_cache[path] then
return M.pretty_cache[path]
end
local cache_key = path
local cwd = M.realpath(vim.loop.cwd()) or ""
if path:find(cwd, 1, true) == 1 then
path = path:sub(#cwd + 2)
else
local roots = M.detect({ spec = { ".git" } })
local root = roots[1] and roots[1].paths[1] or nil
if root then
path = path:sub(#vim.fs.dirname(root) + 2)
end
end
local sep = package.config:sub(1, 1)
local parts = vim.split(path, "[\\/]")
if #parts > 3 then
parts = { parts[1], "", parts[#parts - 1], parts[#parts] }
end
local ret = table.concat(parts, sep)
M.pretty_cache[cache_key] = ret
return ret
---@param opts? {hl_last?: string}
function M.pretty_path(opts)
return ""
end
return M