2021-10-11 12:37:09 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2022-01-02 11:38:39 +00:00
|
|
|
local lualine_require = require('lualine_require')
|
2022-02-01 08:04:03 +00:00
|
|
|
local modules = lualine_require.lazy_require {
|
2021-10-11 12:37:09 +00:00
|
|
|
git_diff = 'lualine.components.diff.git_diff',
|
|
|
|
utils = 'lualine.utils.utils',
|
|
|
|
utils_notices = 'lualine.utils.notices',
|
|
|
|
highlight = 'lualine.highlight',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-10-11 12:37:09 +00:00
|
|
|
local M = lualine_require.require('lualine.component'):extend()
|
|
|
|
|
|
|
|
local default_options = {
|
|
|
|
colored = true,
|
|
|
|
symbols = { added = '+', modified = '~', removed = '-' },
|
2022-04-13 05:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local function apply_default_colors(opts)
|
|
|
|
local default_diff_color = {
|
2021-10-11 12:37:09 +00:00
|
|
|
added = {
|
2021-11-22 04:12:19 +00:00
|
|
|
fg = modules.utils.extract_color_from_hllist(
|
|
|
|
'fg',
|
|
|
|
{ 'GitSignsAdd', 'GitGutterAdd', 'DiffAdded', 'DiffAdd' },
|
|
|
|
'#90ee90'
|
|
|
|
),
|
2021-10-11 12:37:09 +00:00
|
|
|
},
|
|
|
|
modified = {
|
2021-11-22 04:12:19 +00:00
|
|
|
fg = modules.utils.extract_color_from_hllist(
|
|
|
|
'fg',
|
|
|
|
{ 'GitSignsChange', 'GitGutterChange', 'DiffChanged', 'DiffChange' },
|
|
|
|
'#f0e130'
|
|
|
|
),
|
2021-10-11 12:37:09 +00:00
|
|
|
},
|
|
|
|
removed = {
|
2021-11-22 04:12:19 +00:00
|
|
|
fg = modules.utils.extract_color_from_hllist(
|
|
|
|
'fg',
|
|
|
|
{ 'GitSignsDelete', 'GitGutterDelete', 'DiffRemoved', 'DiffDelete' },
|
|
|
|
'#ff0038'
|
|
|
|
),
|
2021-10-11 12:37:09 +00:00
|
|
|
},
|
2022-04-13 05:26:31 +00:00
|
|
|
}
|
|
|
|
opts.diff_color = vim.tbl_deep_extend('keep', opts.diff_color or {}, default_diff_color)
|
|
|
|
end
|
2021-10-11 12:37:09 +00:00
|
|
|
|
|
|
|
-- Initializer
|
|
|
|
function M:init(options)
|
|
|
|
M.super.init(self, options)
|
2022-04-13 05:26:31 +00:00
|
|
|
apply_default_colors(self.options)
|
2021-10-11 12:37:09 +00:00
|
|
|
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
|
|
|
|
-- create highlights and save highlight_name in highlights table
|
|
|
|
if self.options.colored then
|
|
|
|
self.highlights = {
|
2022-04-14 18:30:47 +00:00
|
|
|
added = self:create_hl(self.options.diff_color.added, 'added'),
|
|
|
|
modified = self:create_hl(self.options.diff_color.modified, 'modified'),
|
|
|
|
removed = self:create_hl(self.options.diff_color.removed, 'removed'),
|
2021-10-11 12:37:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
modules.git_diff.init(self.options)
|
|
|
|
end
|
|
|
|
|
2022-05-30 14:25:05 +00:00
|
|
|
-- Function that runs every time statusline is updated
|
2021-10-11 12:37:09 +00:00
|
|
|
function M:update_status(is_focused)
|
2022-01-04 13:17:16 +00:00
|
|
|
local git_diff = modules.git_diff.get_sign_count((not is_focused and vim.api.nvim_get_current_buf()))
|
2021-10-11 12:37:09 +00:00
|
|
|
if git_diff == nil then
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
|
|
|
local colors = {}
|
|
|
|
if self.options.colored then
|
|
|
|
-- load the highlights and store them in colors table
|
|
|
|
for name, highlight_name in pairs(self.highlights) do
|
2022-04-14 18:30:47 +00:00
|
|
|
colors[name] = self:format_hl(highlight_name)
|
2021-10-11 12:37:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = {}
|
|
|
|
-- loop though data and load available sections in result table
|
2022-02-01 08:04:03 +00:00
|
|
|
for _, name in ipairs { 'added', 'modified', 'removed' } do
|
2021-10-11 12:37:09 +00:00
|
|
|
if git_diff[name] and git_diff[name] > 0 then
|
|
|
|
if self.options.colored then
|
|
|
|
table.insert(result, colors[name] .. self.options.symbols[name] .. git_diff[name])
|
|
|
|
else
|
|
|
|
table.insert(result, self.options.symbols[name] .. git_diff[name])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #result > 0 then
|
|
|
|
return table.concat(result, ' ')
|
|
|
|
else
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|