mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-24 17:58:51 +02:00
feat(lualine): pretty_path now highlights file basename when modified
This commit is contained in:
parent
782fe0bef0
commit
8af7309c7e
4 changed files with 69 additions and 37 deletions
|
@ -131,7 +131,9 @@ return {
|
||||||
sections = {
|
sections = {
|
||||||
lualine_a = { "mode" },
|
lualine_a = { "mode" },
|
||||||
lualine_b = { "branch" },
|
lualine_b = { "branch" },
|
||||||
|
|
||||||
lualine_c = {
|
lualine_c = {
|
||||||
|
Util.lualine.root_dir(),
|
||||||
{
|
{
|
||||||
"diagnostics",
|
"diagnostics",
|
||||||
symbols = {
|
symbols = {
|
||||||
|
@ -142,11 +144,7 @@ return {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } },
|
{ "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } },
|
||||||
{
|
{ Util.lualine.pretty_path() },
|
||||||
function()
|
|
||||||
return Util.root.pretty_path()
|
|
||||||
end,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
lualine_x = {
|
lualine_x = {
|
||||||
-- stylua: ignore
|
-- stylua: ignore
|
||||||
|
|
|
@ -49,6 +49,10 @@ setmetatable(M, {
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function M.is_win()
|
||||||
|
return vim.loop.os_uname().sysname:find("Windows") ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
---@param plugin string
|
---@param plugin string
|
||||||
function M.has(plugin)
|
function M.has(plugin)
|
||||||
return require("lazy.core.config").spec.plugins[plugin] ~= nil
|
return require("lazy.core.config").spec.plugins[plugin] ~= nil
|
||||||
|
|
|
@ -43,4 +43,59 @@ function M.cmp_source(name, icon)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param component any
|
||||||
|
---@param text string
|
||||||
|
---@param hl_group? string
|
||||||
|
---@return string
|
||||||
|
function M.format(component, text, hl_group)
|
||||||
|
if not hl_group then
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
---@type table<string, string>
|
||||||
|
component.hl_cache = component.hl_cache or {}
|
||||||
|
local lualine_hl_group = component.hl_cache[hl_group]
|
||||||
|
if not lualine_hl_group then
|
||||||
|
local utils = require("lualine.utils.utils")
|
||||||
|
lualine_hl_group = component:create_hl({ fg = utils.extract_highlight_colors(hl_group, "fg") }, "LV_" .. hl_group)
|
||||||
|
component.hl_cache[hl_group] = lualine_hl_group
|
||||||
|
end
|
||||||
|
return component:format_hl(lualine_hl_group) .. text .. component:get_default_hl()
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param opts? {relative: "cwd"|"root", modified_hl: string?}
|
||||||
|
function M.pretty_path(opts)
|
||||||
|
opts = vim.tbl_extend("force", {
|
||||||
|
relative = "cwd",
|
||||||
|
modified_hl = "Constant",
|
||||||
|
}, opts or {})
|
||||||
|
|
||||||
|
return function(self)
|
||||||
|
local path = vim.fn.expand("%:p") --[[@as string]]
|
||||||
|
|
||||||
|
if path == "" then
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
local root = Util.root.get()
|
||||||
|
local cwd = Util.root.cwd()
|
||||||
|
|
||||||
|
if opts.relative == "cwd" and path:find(cwd, 1, true) == 1 then
|
||||||
|
path = path:sub(#cwd + 2)
|
||||||
|
else
|
||||||
|
path = path:sub(#root + 2)
|
||||||
|
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
|
||||||
|
|
||||||
|
if opts.modified_hl and vim.bo.modified then
|
||||||
|
parts[#parts] = M.format(self, parts[#parts], opts.modified_hl)
|
||||||
|
end
|
||||||
|
|
||||||
|
return table.concat(parts, sep)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -58,6 +58,10 @@ function M.bufpath(buf)
|
||||||
return M.realpath(vim.api.nvim_buf_get_name(assert(buf)))
|
return M.realpath(vim.api.nvim_buf_get_name(assert(buf)))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.cwd()
|
||||||
|
return M.realpath(vim.loop.cwd()) or ""
|
||||||
|
end
|
||||||
|
|
||||||
function M.realpath(path)
|
function M.realpath(path)
|
||||||
if path == "" or path == nil then
|
if path == "" or path == nil then
|
||||||
return nil
|
return nil
|
||||||
|
@ -144,38 +148,9 @@ function M.get()
|
||||||
return roots[1] and roots[1].paths[1] or vim.loop.cwd()
|
return roots[1] and roots[1].paths[1] or vim.loop.cwd()
|
||||||
end
|
end
|
||||||
|
|
||||||
M.pretty_cache = {} ---@type table<string, string>
|
---@param opts? {hl_last?: string}
|
||||||
function M.pretty_path()
|
function M.pretty_path(opts)
|
||||||
local path = vim.fn.expand("%:p") --[[@as string]]
|
return ""
|
||||||
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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue