]> git.rmz.io Git - dotfiles.git/commitdiff
lazyvim: absorb lualine.pretty_path
authorSamir Benmendil <me@rmz.io>
Sun, 16 Feb 2025 00:01:34 +0000 (00:01 +0000)
committerSamir Benmendil <me@rmz.io>
Sun, 2 Mar 2025 16:05:27 +0000 (16:05 +0000)
nvim/lua/plugins/ui.lua

index f317aaf44a3e5ce6e575a7a251d1f1245ded3417..272c027b787ddcf5a450c3c2efcb2e50fcc252f5 100644 (file)
@@ -1,3 +1,89 @@
+local M = {}
+
+---@param component any -- actually a lualine.component
+---@param text string
+---@param hl_group? string
+---@return string
+function M.format(component, text, hl_group)
+  text = text:gsub("%%", "%%%%")
+  if not hl_group or hl_group == "" then
+    return text
+  end
+  --- hl_cache is added here, not modified
+  ---@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")
+    ---@type string[]
+    local gui = vim.tbl_filter(function(x) return x end, {
+      utils.extract_highlight_colors(hl_group, "bold") and "bold",
+      utils.extract_highlight_colors(hl_group, "italic") and "italic",
+    })
+
+    lualine_hl_group = component:create_hl({
+      fg = utils.extract_highlight_colors(hl_group, "fg"),
+      gui = #gui > 0 and table.concat(gui, ",") or nil,
+    }, "LV_" .. hl_group) --[[@as string]]
+    component.hl_cache[hl_group] = lualine_hl_group
+  end
+  return component:format_hl(lualine_hl_group) .. text .. component:get_default_hl()
+end
+
+function M.pretty_path()
+  return function(self)
+    local opts = {
+      directory_hl = "",
+      filename_hl = "Bold",
+      length = 3,
+      modified_hl = "MatchParen",  -- hl group when file is modified
+      modified_sign = "",
+      readonly_icon = " 󰌾 ",
+    }
+
+    local path = vim.fn.expand("%:p") --[[@as string]]
+
+    if path == "" then
+      return ""
+    end
+
+    path = require("lazy.core.util").norm(path)
+    local cwd = vim.uv.cwd()
+
+    if path:find(cwd, 1, true) == 1 then
+      path = path:sub(#cwd + 2)
+    end
+
+    local sep = package.config:sub(1, 1)
+    local parts = vim.split(path, "[\\/]")
+
+    if opts.length == 0 then
+      parts = parts
+    elseif #parts > opts.length then
+      parts = { parts[1], "…", unpack(parts, #parts - opts.length + 2, #parts) }
+    end
+
+    if vim.bo.modified then
+      parts[#parts] = parts[#parts] .. opts.modified_sign
+      parts[#parts] = M.format(self, parts[#parts], opts.modified_hl)
+    else
+      parts[#parts] = M.format(self, parts[#parts], opts.filename_hl)
+    end
+
+    local dir = ""
+    if #parts > 1 then
+      dir = table.concat({ unpack(parts, 1, #parts - 1) }, sep)
+      dir = M.format(self, dir .. sep, opts.directory_hl)
+    end
+
+    local readonly = ""
+    if vim.bo.readonly then
+      readonly = M.format(self, opts.readonly_icon, opts.modified_hl)
+    end
+    return dir .. parts[#parts] .. readonly
+  end
+end
+
 ---@type LazySpec
 return {
   { 'echasnovski/mini.clue',
 ---@type LazySpec
 return {
   { 'echasnovski/mini.clue',
@@ -120,7 +206,7 @@ return {
               },
             },
             { "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } },
               },
             },
             { "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } },
-            { LazyVim.lualine.pretty_path() },
+            { M.pretty_path() },
           },
           lualine_x = {
             Snacks.profiler.status(),
           },
           lualine_x = {
             Snacks.profiler.status(),