From 44a0fba86721c248d907ee5227fdc617a03cdef4 Mon Sep 17 00:00:00 2001 From: Mika Raunio Date: Fri, 31 Mar 2023 11:02:12 +0300 Subject: [PATCH] feat: add name+parent path option for component(filename) (#945) Co-authored-by: Mika Raunio --- README.md | 1 + lua/lualine/components/filename.lua | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 50bbfd9..6d988d5 100644 --- a/README.md +++ b/README.md @@ -626,6 +626,7 @@ sections = { -- 1: Relative path -- 2: Absolute path -- 3: Absolute path, with tilde as the home directory + -- 4: Filename and parent dir, 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 e35b5d5..e6da77f 100644 --- a/lua/lualine/components/filename.lua +++ b/lua/lualine/components/filename.lua @@ -50,12 +50,24 @@ local function shorten_path(path, sep, max_len) return table.concat(segments, sep) end +local function filename_and_parent(path, sep) + local segments = vim.split(path, sep) + if #segments == 0 then + return path + elseif #segments == 1 then + return segments[#segments] + else + return table.concat({segments[#segments - 1], segments[#segments]}, sep) + end +end + M.init = function(self, options) M.super.init(self, options) self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options) end M.update_status = function(self) + local path_separator = package.config:sub(1, 1) local data if self.options.path == 1 then -- relative path @@ -66,6 +78,9 @@ M.update_status = function(self) elseif self.options.path == 3 then -- absolute path, with tilde data = vim.fn.expand('%:p:~') + elseif self.options.path == 4 then + -- filename and immediate parent + data = filename_and_parent(vim.fn.expand('%:p:~'), path_separator) else -- just filename data = vim.fn.expand('%:t') @@ -81,7 +96,6 @@ M.update_status = function(self) local windwidth = self.options.globalstatus and vim.go.columns or vim.fn.winwidth(0) local estimated_space_available = windwidth - self.options.shorting_target - local path_separator = package.config:sub(1, 1) data = shorten_path(data, path_separator, estimated_space_available) end