From dfdfcad1aab0ee39ac3876e47cbeb727eb4f1e95 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 15 Oct 2023 22:38:44 +0200 Subject: [PATCH] feat(lualine): new root dir component that only shows when cwd != root_dir --- lua/lazyvim/util/lualine.lua | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lua/lazyvim/util/lualine.lua b/lua/lazyvim/util/lualine.lua index 7eb353bf..1ec2cd19 100644 --- a/lua/lazyvim/util/lualine.lua +++ b/lua/lazyvim/util/lualine.lua @@ -98,4 +98,46 @@ function M.pretty_path(opts) end end +---@param opts? {cwd:false, subdirectory: true, parent: true, other: true, icon?:string} +function M.root_dir(opts) + opts = vim.tbl_extend("force", { + cwd = false, + subdirectory = true, + parent = true, + other = true, + icon = "󱉭 ", + color = Util.ui.fg("Special"), + }, opts or {}) + + local function get() + local cwd = Util.root.cwd() + local root = Util.root.get() + local name = vim.fs.basename(root) + + if root == cwd then + -- root is cwd + return opts.cwd and name + elseif root:find(cwd, 1, true) == 1 then + -- root is subdirectory of cwd + return opts.subdirectory and name + elseif cwd:find(root, 1, true) == 1 then + -- root is parent directory of cwd + return opts.parent and name + else + -- root and cwd are not related + return opts.other and name + end + end + + return { + function() + return (opts.icon and opts.icon .. " ") .. get() + end, + cond = function() + return type(get()) == "string" + end, + color = opts.color, + } +end + return M