Skip to content

Commit

Permalink
feat(root): cached pretty path function for statuslines
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Oct 12, 2023
1 parent 305e82f commit 8d7361c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lua/lazyvim/plugins/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ return {
},
},
{ "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } },
{ "filename", path = 1, symbols = { modified = "", readonly = "", unnamed = "" } },
-- stylua: ignore
{
function()
return Util.root.pretty_path()
end,
},
},
lualine_x = {
Expand Down
34 changes: 34 additions & 0 deletions lua/lazyvim/util/root.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,38 @@ 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
end

return M

0 comments on commit 8d7361c

Please sign in to comment.