From 9d3101784c522ebf5791d0260f636f53f0a8ec71 Mon Sep 17 00:00:00 2001 From: Pete Kazmier Date: Sat, 9 Mar 2024 13:34:53 -0500 Subject: [PATCH] feat(lualine): add more hl options to pretty_path Adds two additional options to pretty_path: filename_hl and dirpath_hl. This allows users to customize the highlight group of both the directory component of the path name and the filenname independently. modified_hl is still used when the buffer has been modified. Thanks to dpetka2001 (Iordanis Petkakis) for the changes to the format function. --- lua/lazyvim/util/lualine.lua | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lua/lazyvim/util/lualine.lua b/lua/lazyvim/util/lualine.lua index 3e3cdb9a..c693b00d 100644 --- a/lua/lazyvim/util/lualine.lua +++ b/lua/lazyvim/util/lualine.lua @@ -56,17 +56,36 @@ function M.format(component, text, hl_group) 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) + local mygui = function() + local mybold = utils.extract_highlight_colors(hl_group, "bold") and "bold" + local myitalic = utils.extract_highlight_colors(hl_group, "italic") and "italic" + if mybold and myitalic then + return mybold .. "," .. myitalic + elseif mybold then + return mybold + elseif myitalic then + return myitalic + else + return "" + end + end + + lualine_hl_group = component:create_hl({ + fg = utils.extract_highlight_colors(hl_group, "fg"), + gui = mygui(), + }, "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?} +---@param opts? {relative: "cwd"|"root", modified_hl: string?, dirpath_hl: string?, filename_hl: string?} function M.pretty_path(opts) opts = vim.tbl_extend("force", { relative = "cwd", modified_hl = "Constant", + dirpath_hl = "", + filename_hl = "", }, opts or {}) return function(self) @@ -75,6 +94,7 @@ function M.pretty_path(opts) if path == "" then return "" end + local root = Util.root.get({ normalize = true }) local cwd = Util.root.cwd() @@ -86,15 +106,23 @@ function M.pretty_path(opts) 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) + else + parts[#parts] = M.format(self, parts[#parts], opts.filename_hl) end - return table.concat(parts, sep) + local dirpath = "" + if #parts > 1 then + dirpath = table.concat({ unpack(parts, 1, #parts - 1) }, sep) + dirpath = M.format(self, dirpath .. sep, opts.dirpath_hl) + end + return dirpath .. parts[#parts] end end