From 93c934a641cbefebfd601a2782814664f391a218 Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Thu, 18 Nov 2021 08:12:33 +0600 Subject: [PATCH] fixup: fix padding, fmt options in tabs and buffers Previously these options were applied to entire tabs/buffers components. Now they are applied to specific buffer/tab. Note: Now icon, color options are no longer applicable to buffers & tabs component. I can't see how they make sense here. Also cond stil applies to entire component. fixes #414 --- lua/lualine/components/buffers/buffer.lua | 23 +++++++++++++++++++---- lua/lualine/components/buffers/init.lua | 16 ++++++++++++++++ lua/lualine/components/tabs/init.lua | 16 ++++++++++++++++ lua/lualine/components/tabs/tab.lua | 23 +++++++++++++++++++---- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/lua/lualine/components/buffers/buffer.lua b/lua/lualine/components/buffers/buffer.lua index f05c866..416fe34 100644 --- a/lua/lualine/components/buffers/buffer.lua +++ b/lua/lualine/components/buffers/buffer.lua @@ -47,19 +47,23 @@ end ---returns rendered buffer ---@return string function Buffer:render() - local name + local name = self:name() + if self.options.fmt then + name = self.options.fmt(name or '') + end if self.ellipse then -- show elipsis name = '...' else if self.options.mode == 0 then - name = string.format(' %s%s%s ', self.icon, self:name(), self.modified_icon) + name = string.format('%s%s%s', self.icon, name, self.modified_icon) elseif self.options.mode == 1 then - name = string.format(' %s %s%s ', self.bufnr, self.icon, self.modified_icon) + name = string.format('%s %s%s', self.bufnr, self.icon, self.modified_icon) else - name = string.format(' %s %s%s%s ', self.bufnr, self.icon, self:name(), self.modified_icon) + name = string.format('%s %s%s%s', self.bufnr, self.icon, name, self.modified_icon) end end + name = Buffer.apply_padding(name, self.options.padding) self.len = vim.fn.strchars(name) -- setup for mouse clicks @@ -119,4 +123,15 @@ function Buffer:name() or vim.fn.pathshorten(vim.fn.fnamemodify(self.file, ':p:.')) end +---adds spaces to left and right +function Buffer.apply_padding(str, padding) + local l_padding, r_padding = 1, 1 + if type(padding) == 'number' then + l_padding, r_padding = padding, padding + elseif type(padding) == 'table' then + l_padding, r_padding = padding.left or 0, padding.right or 0 + end + return string.rep(' ', l_padding) .. str .. string.rep(' ', r_padding) +end + return Buffer diff --git a/lua/lualine/components/buffers/init.lua b/lua/lualine/components/buffers/init.lua index 1500d34..50e9170 100644 --- a/lua/lualine/components/buffers/init.lua +++ b/lua/lualine/components/buffers/init.lua @@ -177,6 +177,22 @@ function M:update_status() return table.concat(data) end +function M:draw() + self.status = '' + self.applied_separator = '' + + if self.options.cond ~= nil and self.options.cond() ~= true then + return self.status + end + local status = self:update_status() + if type(status) == 'string' and #status > 0 then + self.status = status + self:apply_section_separators() + self:apply_separator() + end + return self.status +end + vim.cmd [[ function! LualineSwitchBuffer(bufnr, mouseclicks, mousebutton, modifiers) execute ":buffer " . a:bufnr diff --git a/lua/lualine/components/tabs/init.lua b/lua/lualine/components/tabs/init.lua index 59c7f7c..92ab2bb 100644 --- a/lua/lualine/components/tabs/init.lua +++ b/lua/lualine/components/tabs/init.lua @@ -147,6 +147,22 @@ function M:update_status() return table.concat(data) end +function M:draw() + self.status = '' + self.applied_separator = '' + + if self.options.cond ~= nil and self.options.cond() ~= true then + return self.status + end + local status = self:update_status() + if type(status) == 'string' and #status > 0 then + self.status = status + self:apply_section_separators() + self:apply_separator() + end + return self.status +end + vim.cmd [[ function! LualineSwitchTab(tabnr, mouseclicks, mousebutton, modifiers) execute a:tabnr . "tabnext" diff --git a/lua/lualine/components/tabs/tab.lua b/lua/lualine/components/tabs/tab.lua index d176c4f..f7fd198 100644 --- a/lua/lualine/components/tabs/tab.lua +++ b/lua/lualine/components/tabs/tab.lua @@ -35,19 +35,23 @@ end ---returns rendered tab ---@return string function Tab:render() - local name + local name = self:label() + if self.options.fmt then + name = self.options.fmt(name or '') + end if self.ellipse then -- show elipsis name = '...' else -- different formats for different modes if self.options.mode == 0 then - name = string.format('%s%s ', (self.last or not self.first) and ' ' or '', tostring(self.tabnr)) + name = tostring(self.tabnr) elseif self.options.mode == 1 then - name = string.format('%s%s ', (self.last or not self.first) and ' ' or '', self:label()) + name = name else - name = string.format('%s%s %s ', (self.last or not self.first) and ' ' or '', tostring(self.tabnr), self:label()) + name = string.format('%s %s', tostring(self.tabnr), name) end end + name = Tab.apply_padding(name, self.options.padding) self.len = vim.fn.strchars(name) -- setup for mouse clicks @@ -88,4 +92,15 @@ function Tab:separator_after() end end +---adds spaces to left and right +function Tab.apply_padding(str, padding) + local l_padding, r_padding = 1, 1 + if type(padding) == 'number' then + l_padding, r_padding = padding, padding + elseif type(padding) == 'table' then + l_padding, r_padding = padding.left or 0, padding.right or 0 + end + return string.rep(' ', l_padding) .. str .. string.rep(' ', r_padding) +end + return Tab