| 1 |
-- Copyright (c) 2020-2021 shadmansaleh |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local lualine_require = require('lualine_require') |
| 4 |
local modules = lualine_require.lazy_require { |
| 5 |
git_diff = 'lualine.components.diff.git_diff', |
| 6 |
utils = 'lualine.utils.utils', |
| 7 |
utils_notices = 'lualine.utils.notices', |
| 8 |
highlight = 'lualine.highlight', |
| 9 |
} |
| 10 |
local M = lualine_require.require('lualine.component'):extend() |
| 11 |
|
| 12 |
local default_options = { |
| 13 |
colored = true, |
| 14 |
symbols = { added = '+', modified = '~', removed = '-' }, |
| 15 |
} |
| 16 |
|
| 17 |
local function apply_default_colors(opts) |
| 18 |
local default_diff_color = { |
| 19 |
added = { |
| 20 |
fg = modules.utils.extract_color_from_hllist( |
| 21 |
'fg', |
| 22 |
{ 'LuaLineDiffAdd', 'GitSignsAdd', 'GitGutterAdd', 'DiffAdded', 'DiffAdd' }, |
| 23 |
'#90ee90' |
| 24 |
), |
| 25 |
}, |
| 26 |
modified = { |
| 27 |
fg = modules.utils.extract_color_from_hllist( |
| 28 |
'fg', |
| 29 |
{ 'LuaLineDiffChange', 'GitSignsChange', 'GitGutterChange', 'DiffChanged', 'DiffChange' }, |
| 30 |
'#f0e130' |
| 31 |
), |
| 32 |
}, |
| 33 |
removed = { |
| 34 |
fg = modules.utils.extract_color_from_hllist( |
| 35 |
'fg', |
| 36 |
{ 'LuaLineDiffDelete', 'GitSignsDelete', 'GitGutterDelete', 'DiffRemoved', 'DiffDelete' }, |
| 37 |
'#ff0038' |
| 38 |
), |
| 39 |
}, |
| 40 |
} |
| 41 |
opts.diff_color = vim.tbl_deep_extend('keep', opts.diff_color or {}, default_diff_color) |
| 42 |
end |
| 43 |
|
| 44 |
-- Initializer |
| 45 |
function M:init(options) |
| 46 |
M.super.init(self, options) |
| 47 |
apply_default_colors(self.options) |
| 48 |
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options) |
| 49 |
-- create highlights and save highlight_name in highlights table |
| 50 |
if self.options.colored then |
| 51 |
self.highlights = { |
| 52 |
added = self:create_hl(self.options.diff_color.added, 'added'), |
| 53 |
modified = self:create_hl(self.options.diff_color.modified, 'modified'), |
| 54 |
removed = self:create_hl(self.options.diff_color.removed, 'removed'), |
| 55 |
} |
| 56 |
end |
| 57 |
modules.git_diff.init(self.options) |
| 58 |
end |
| 59 |
|
| 60 |
-- Function that runs every time statusline is updated |
| 61 |
function M:update_status(is_focused) |
| 62 |
local git_diff = modules.git_diff.get_sign_count((not is_focused and vim.api.nvim_get_current_buf())) |
| 63 |
if git_diff == nil then |
| 64 |
return '' |
| 65 |
end |
| 66 |
|
| 67 |
local colors = {} |
| 68 |
if self.options.colored then |
| 69 |
-- load the highlights and store them in colors table |
| 70 |
for name, highlight_name in pairs(self.highlights) do |
| 71 |
colors[name] = self:format_hl(highlight_name) |
| 72 |
end |
| 73 |
end |
| 74 |
|
| 75 |
local result = {} |
| 76 |
-- loop though data and load available sections in result table |
| 77 |
for _, name in ipairs { 'added', 'modified', 'removed' } do |
| 78 |
if git_diff[name] and git_diff[name] > 0 then |
| 79 |
if self.options.colored then |
| 80 |
table.insert(result, colors[name] .. self.options.symbols[name] .. git_diff[name]) |
| 81 |
else |
| 82 |
table.insert(result, self.options.symbols[name] .. git_diff[name]) |
| 83 |
end |
| 84 |
end |
| 85 |
end |
| 86 |
if #result > 0 then |
| 87 |
return table.concat(result, ' ') |
| 88 |
else |
| 89 |
return '' |
| 90 |
end |
| 91 |
end |
| 92 |
|
| 93 |
return M |