From cc963eaee2c0fe366336d97ec2b94db4e0b311d2 Mon Sep 17 00:00:00 2001 From: ptdewey Date: Tue, 30 Jul 2024 19:54:12 -0400 Subject: [PATCH] feat: added filename path option to dynamically show parent for duplicate filenames --- README.md | 1 + lua/lualine/components/filename.lua | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 238b283b8..2326393eb 100644 --- a/README.md +++ b/README.md @@ -639,6 +639,7 @@ sections = { -- 2: Absolute path -- 3: Absolute path, with tilde as the home directory -- 4: Filename and parent dir, with tilde as the home directory + -- 5: Filename or filename and parent dir when there are duplicate filenames open, with tilde as the home directory shorting_target = 40, -- Shortens path to leave 40 spaces in the window -- for other components. (terrible name, any suggestions?) diff --git a/lua/lualine/components/filename.lua b/lua/lualine/components/filename.lua index 22fd964a6..8762d8ba8 100644 --- a/lua/lualine/components/filename.lua +++ b/lua/lualine/components/filename.lua @@ -81,6 +81,26 @@ M.update_status = function(self) elseif self.options.path == 4 then -- filename and immediate parent data = filename_and_parent(vim.fn.expand('%:p:~'), path_separator) + elseif self.options.path == 5 then + -- check for duplicate filenames in current buffers and add parent dir if a duplicate is found + local current_filename = vim.fn.expand('%:t') + local buffer_list = vim.fn.getbufinfo { buflisted = 1 } + local duplicate_found = false + + for _, buf in ipairs(buffer_list) do + if buf.name ~= vim.fn.expand('%:p') and vim.fn.fnamemodify(buf.name, ':t') == current_filename then + duplicate_found = true + break + end + end + + if duplicate_found then + -- add parent directory to distinguish duplicate filenames + local parent_dir = vim.fn.fnamemodify(vim.fn.expand('%:p:~'), ':h:t:~') + data = parent_dir .. path_separator .. current_filename + else + data = current_filename + end else -- just filename data = vim.fn.expand('%:t')