From ea2432cc1e2f9f20d316d3b0f52c7246955bcc63 Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Tue, 3 Aug 2021 17:41:37 +0600 Subject: [PATCH] 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. --- README.md | 4 ++++ doc/lualine.txt | 4 ++++ lua/lualine/components/diff.lua | 15 +++++++++++---- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 37e6af4..6f564db 100644 --- a/README.md +++ b/README.md @@ -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. } } } diff --git a/doc/lualine.txt b/doc/lualine.txt index 0a51159..63e7933 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -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. } } } diff --git a/lua/lualine/components/diff.lua b/lua/lualine/components/diff.lua index 9be96c2..91bb6b6 100644 --- a/lua/lualine/components/diff.lua +++ b/lua/lualine/components/diff.lua @@ -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 = {}