feat: Add source option to diff

It allows you to easily integrate external source for diff count.
If user has some other plugin keeping track of diff counts. There's
no reason for lualine to make same calculations again . It can just
reuse them. But lualine knows nothing about how other plugin exposes
their api so user will have to glue them together with this option.
This commit is contained in:
shadmansaleh 2021-08-03 17:41:37 +06:00
parent e7b8b42089
commit ea2432cc1e
3 changed files with 19 additions and 4 deletions

View File

@ -346,6 +346,10 @@ sections = {
color_modified = nil, -- changes diff's modified foreground color
color_removed = nil, -- changes diff's removed foreground color
symbols = {added = '+', modified = '~', removed = '-'} -- changes diff symbols
source = nil, -- A function that works as a data source for diff.
-- it must return a table like
{added = add_count, modified = modified_count, removed = removed_count }
-- Or nil on failure. Count <= 0 won't be displayed.
}
}
}

View File

@ -276,6 +276,10 @@ Component specific local options~
color_modified = nil, -- changes diff's modified foreground color
color_removed = nil, -- changes diff's removed foreground color
symbols = {added = '+', modified = '~', removed = '-'} -- changes diff symbols
source = nil, -- A function that works as a data source for diff.
-- it must return a table like
{added = add_count, modified = modified_count, removed = removed_count }
-- Or nil on failure. Count <= 0 won't be displayed.
}
}
}

View File

@ -62,16 +62,23 @@ Diff.new = function(self, options, child)
}
end
vim.api.nvim_exec([[
autocmd lualine BufEnter * lua require'lualine.components.diff'.update_git_diff_getter()
autocmd lualine BufWritePost * lua require'lualine.components.diff'.update_git_diff()
]], false)
if type(new_instance.options.source) ~= 'function' then
-- setup internal source
vim.cmd [[
autocmd lualine BufEnter * lua require'lualine.components.diff'.update_git_diff_getter()
autocmd lualine BufWritePost * lua require'lualine.components.diff'.update_git_diff()
]]
end
return new_instance
end
-- Function that runs everytime statusline is updated
Diff.update_status = function(self)
if self.options.source then
Diff.git_diff = self.options.source()
end
if Diff.git_diff == nil then return '' end
local colors = {}