Passing self as second arg to self.options.fmt (#915)

* Add `tabnr` to `fmt` function

* Passing self as second arg to `self.options.fmt`

* README updated.
This commit is contained in:
Dalius Dobravolskas 2022-12-22 15:14:34 +02:00 committed by GitHub
parent afb8bfb786
commit 3497c6c6b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 3 deletions

View File

@ -452,6 +452,11 @@ sections = {
-- padding = { left = left_padding, right = right_padding }
fmt = nil, -- Format function, formats the component's output.
-- This function receives two arguments:
-- - string that is going to be displayed and
-- that can be changed, enhanced and etc.
-- - context object with information you might
-- need. E.g. tabnr if used with tabs.
on_click = nil, -- takes a function that is called when component is clicked with mouse.
-- the function receives several arguments
-- - number of clicks incase of multiple clicks
@ -650,6 +655,16 @@ sections = {
active = 'lualine_{section}_normal', -- Color for active tab.
inactive = 'lualine_{section}_inactive', -- Color for inactive tab.
},
fmt = function(name, context)
-- Show + if buffer is modified in tab
local buflist = vim.fn.tabpagebuflist(context.tabnr)
local winnr = vim.fn.tabpagewinnr(context.tabnr)
local bufnr = buflist[winnr]
local mod = vim.fn.getbufvar(bufnr, '&mod')
return name .. (mod == 1 and ' +' or '')
end
}
}
}

View File

@ -272,7 +272,7 @@ function M:draw(default_highlight, is_focused)
self.default_hl = default_highlight
local status = self:update_status(is_focused)
if self.options.fmt then
status = self.options.fmt(status or '')
status = self.options.fmt(status or '', self)
end
if type(status) == 'string' and #status > 0 then
self.status = status

View File

@ -69,7 +69,7 @@ end
function Buffer:render()
local name = self:name()
if self.options.fmt then
name = self.options.fmt(name or '')
name = self.options.fmt(name or '', self)
end
if self.ellipse then -- show ellipsis

View File

@ -51,7 +51,7 @@ end
function Tab:render()
local name = self:label()
if self.options.fmt then
name = self.options.fmt(name or '')
name = self.options.fmt(name or '', self)
end
if self.ellipse then -- show ellipsis
name = '...'