Shadman 37a314b9e3
feat: add support for dynamic color with functions in color options (#566)
* feat: allow functions in color options.

* update_evilline

* docs: document color functions

* remove unnecesery stuff

* add dynamic color supoort for themes

* chore: autogen (vimdocs+formating)

* fix dynamic colors not working as color fallback

* fix transitional separators not updating for dynamic colors dynamic colors

* fix failing tests

* apply format

* Allow cases where theme doesn't even define nornal color for some mode

* allow color function to return nil

* some enhancements

* more enhancements

* code cleanup

* if we don't have even normal in theme we should just nvim highlight it with it's ususal stl colors

* not sure how it get here . It should be in different pr

* keep only c of lualine_c in component section name

* use sh to run docgen

* fix filetype component not respecting color option properly

* fix section x,y,z not falling back to correct colors

* auto format

* actually fix xyz not falling back to correct mode

* fix comp sep not correctly removed properly on function hl

* pass only section in color fn

* more enhancements

* update docs

* update create_comp_hl call locations

* enhancements+fixes

* fix broken hls in tabline

* Fix function color options not inheriting right colors

* some enhancements

* fix tests

* tweek docs

Co-authored-by: shadmansaleh <shadmansaleh@users.noreply.github.com>
2022-03-02 19:37:08 +06:00

104 lines
2.9 KiB
Lua

-- Copyright (c) 2020-2021 shadmansaleh
-- MIT license, see LICENSE for more details.
local lualine_require = require('lualine_require')
local modules = lualine_require.lazy_require {
git_diff = 'lualine.components.diff.git_diff',
utils = 'lualine.utils.utils',
utils_notices = 'lualine.utils.notices',
highlight = 'lualine.highlight',
}
local M = lualine_require.require('lualine.component'):extend()
local default_options = {
colored = true,
symbols = { added = '+', modified = '~', removed = '-' },
diff_color = {
added = {
fg = modules.utils.extract_color_from_hllist(
'fg',
{ 'GitSignsAdd', 'GitGutterAdd', 'DiffAdded', 'DiffAdd' },
'#90ee90'
),
},
modified = {
fg = modules.utils.extract_color_from_hllist(
'fg',
{ 'GitSignsChange', 'GitGutterChange', 'DiffChanged', 'DiffChange' },
'#f0e130'
),
},
removed = {
fg = modules.utils.extract_color_from_hllist(
'fg',
{ 'GitSignsDelete', 'GitGutterDelete', 'DiffRemoved', 'DiffDelete' },
'#ff0038'
),
},
},
}
-- Initializer
function M:init(options)
M.super.init(self, options)
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 = {
added = modules.highlight.create_component_highlight_group(
self.options.diff_color.added,
'diff_added',
self.options,
false
),
modified = modules.highlight.create_component_highlight_group(
self.options.diff_color.modified,
'diff_modified',
self.options,
false
),
removed = modules.highlight.create_component_highlight_group(
self.options.diff_color.removed,
'diff_removed',
self.options,
false
),
}
end
modules.git_diff.init(self.options)
end
-- Function that runs everytime statusline is updated
function M:update_status(is_focused)
local git_diff = modules.git_diff.get_sign_count((not is_focused and vim.api.nvim_get_current_buf()))
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
colors[name] = modules.highlight.component_format_highlight(highlight_name)
end
end
local result = {}
-- loop though data and load available sections in result table
for _, name in ipairs { 'added', 'modified', 'removed' } do
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