enhance: Reload lualine for ColorScheme so auto can self update
This commit is contained in:
parent
9eda025073
commit
b2fcf50742
|
@ -179,7 +179,7 @@ local function setup_theme()
|
||||||
end
|
end
|
||||||
local theme = get_theme_from_config()
|
local theme = get_theme_from_config()
|
||||||
modules.highlight.create_highlight_groups(theme)
|
modules.highlight.create_highlight_groups(theme)
|
||||||
vim.cmd [[autocmd lualine ColorScheme * lua require'lualine.utils.utils'.reload_highlights()
|
vim.cmd [[autocmd lualine ColorScheme * lua require'lualine'.setup()
|
||||||
autocmd lualine OptionSet background lua require'lualine'.setup()]]
|
autocmd lualine OptionSet background lua require'lualine'.setup()]]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ function FileType:apply_icon()
|
||||||
.section)
|
.section)
|
||||||
local icon_highlight = self.options.self.section .. '_' ..
|
local icon_highlight = self.options.self.section .. '_' ..
|
||||||
icon_highlight_group
|
icon_highlight_group
|
||||||
if not modules.utils.highlight_exists(icon_highlight .. '_normal') then
|
if not modules.highlight.highlight_exists(icon_highlight .. '_normal') then
|
||||||
icon_highlight = modules.highlight.create_component_highlight_group(
|
icon_highlight = modules.highlight.create_component_highlight_group(
|
||||||
{fg = highlight_color}, icon_highlight_group,
|
{fg = highlight_color}, icon_highlight_group,
|
||||||
self.options)
|
self.options)
|
||||||
|
|
|
@ -7,10 +7,27 @@ local modules = lualine_require.lazy_require{
|
||||||
utils = 'lualine.utils.utils',
|
utils = 'lualine.utils.utils',
|
||||||
color_utils = 'lualine.utils.color_utils',
|
color_utils = 'lualine.utils.color_utils',
|
||||||
}
|
}
|
||||||
|
|
||||||
local section_highlight_map = {x = 'c', y = 'b', z = 'a'}
|
local section_highlight_map = {x = 'c', y = 'b', z = 'a'}
|
||||||
local active_theme = nil
|
local active_theme = nil
|
||||||
local create_cterm_colors = false
|
local create_cterm_colors = false
|
||||||
|
|
||||||
|
-- table to store the highlight names created by lualine
|
||||||
|
local loaded_highlights = {}
|
||||||
|
|
||||||
|
-- determine if an highlight exist and isn't cleared
|
||||||
|
function M.highlight_exists(highlight_name)
|
||||||
|
return loaded_highlights[highlight_name] or false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- clears loaded_highlights table and highlights
|
||||||
|
local function clear_highlights()
|
||||||
|
for highlight_name, _ in pairs(loaded_highlights) do
|
||||||
|
vim.cmd('highlight clear ' .. highlight_name)
|
||||||
|
loaded_highlights[highlight_name] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function sanitize_color(color)
|
local function sanitize_color(color)
|
||||||
if type(color) == 'string' then
|
if type(color) == 'string' then
|
||||||
if color:sub(1,1) == '#' then return color end -- RGB value
|
if color:sub(1,1) == '#' then return color end -- RGB value
|
||||||
|
@ -23,7 +40,7 @@ local function sanitize_color(color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.highlight(name, foreground, background, gui, link, reload)
|
function M.highlight(name, foreground, background, gui, link)
|
||||||
local command = {'highlight!'}
|
local command = {'highlight!'}
|
||||||
if link and #link > 0 then
|
if link and #link > 0 then
|
||||||
vim.list_extend(command, {'link', name, link})
|
vim.list_extend(command, {'link', name, link})
|
||||||
|
@ -51,13 +68,11 @@ function M.highlight(name, foreground, background, gui, link, reload)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
vim.cmd(table.concat(command, ' '))
|
vim.cmd(table.concat(command, ' '))
|
||||||
if not reload then
|
loaded_highlights[name] = true
|
||||||
modules.utils.save_highlight(name, {name, foreground, background, gui, link, true})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.create_highlight_groups(theme)
|
function M.create_highlight_groups(theme)
|
||||||
modules.utils.clear_highlights()
|
clear_highlights()
|
||||||
active_theme = theme
|
active_theme = theme
|
||||||
create_cterm_colors = not vim.go.termguicolors
|
create_cterm_colors = not vim.go.termguicolors
|
||||||
for mode, sections in pairs(theme) do
|
for mode, sections in pairs(theme) do
|
||||||
|
@ -131,9 +146,9 @@ end
|
||||||
-- to retrive highlight group
|
-- to retrive highlight group
|
||||||
function M.create_component_highlight_group(color, highlight_tag, options)
|
function M.create_component_highlight_group(color, highlight_tag, options)
|
||||||
local tag_id = 0
|
local tag_id = 0
|
||||||
while (modules.utils.highlight_exists(table.concat(
|
while (M.highlight_exists(table.concat(
|
||||||
{'lualine', highlight_tag, 'no_mode'}, '_'))
|
{'lualine', highlight_tag, 'no_mode'}, '_'))
|
||||||
or (options.self.section and modules.utils.highlight_exists(table.concat(
|
or (options.self.section and M.highlight_exists(table.concat(
|
||||||
{options.self.section, highlight_tag, 'normal'}, '_')))
|
{options.self.section, highlight_tag, 'normal'}, '_')))
|
||||||
) do
|
) do
|
||||||
highlight_tag = highlight_tag .. '_' .. tostring(tag_id)
|
highlight_tag = highlight_tag .. '_' .. tostring(tag_id)
|
||||||
|
@ -199,7 +214,7 @@ function M.component_format_highlight(highlight_name)
|
||||||
else
|
else
|
||||||
highlight_group = highlight_group .. '_inactive'
|
highlight_group = highlight_group .. '_inactive'
|
||||||
end
|
end
|
||||||
if modules.utils.highlight_exists(highlight_group) then
|
if M.highlight_exists(highlight_group) then
|
||||||
return '%#' .. highlight_group .. '#'
|
return '%#' .. highlight_group .. '#'
|
||||||
else
|
else
|
||||||
return '%#' .. highlight_name .. '_normal#'
|
return '%#' .. highlight_name .. '_normal#'
|
||||||
|
@ -208,7 +223,7 @@ end
|
||||||
|
|
||||||
function M.format_highlight(is_focused, highlight_group)
|
function M.format_highlight(is_focused, highlight_group)
|
||||||
if highlight_group > 'lualine_c'
|
if highlight_group > 'lualine_c'
|
||||||
and not modules.utils.highlight_exists(highlight_group .. '_normal') then
|
and not M.highlight_exists(highlight_group .. '_normal') then
|
||||||
highlight_group = 'lualine_' ..
|
highlight_group = 'lualine_' ..
|
||||||
section_highlight_map[highlight_group:match(
|
section_highlight_map[highlight_group:match(
|
||||||
'lualine_(.)')]
|
'lualine_(.)')]
|
||||||
|
@ -219,7 +234,7 @@ function M.format_highlight(is_focused, highlight_group)
|
||||||
else
|
else
|
||||||
highlight_name = append_mode(highlight_group)
|
highlight_name = append_mode(highlight_group)
|
||||||
end
|
end
|
||||||
if modules.utils.highlight_exists(highlight_name) then
|
if M.highlight_exists(highlight_name) then
|
||||||
return '%#' .. highlight_name .. '#'
|
return '%#' .. highlight_name .. '#'
|
||||||
end
|
end
|
||||||
return '%#' .. highlight_group .. '_normal#'
|
return '%#' .. highlight_group .. '_normal#'
|
||||||
|
@ -237,7 +252,7 @@ function M.get_transitional_highlights(left_hl, right_hl)
|
||||||
|
|
||||||
-- construct the name of hightlight group
|
-- construct the name of hightlight group
|
||||||
local highlight_name = table.concat({'lualine_transitional',left_hl,'to',right_hl}, '_')
|
local highlight_name = table.concat({'lualine_transitional',left_hl,'to',right_hl}, '_')
|
||||||
if not modules.utils.highlight_exists(highlight_name) then
|
if not M.highlight_exists(highlight_name) then
|
||||||
-- Create the highlight_group if needed
|
-- Create the highlight_group if needed
|
||||||
-- Get colors from highlights
|
-- Get colors from highlights
|
||||||
local fg = modules.utils.extract_highlight_colors(left_hl, 'bg')
|
local fg = modules.utils.extract_highlight_colors(left_hl, 'bg')
|
||||||
|
|
|
@ -19,34 +19,6 @@ function M.extract_highlight_colors(color_group, scope)
|
||||||
return color
|
return color
|
||||||
end
|
end
|
||||||
|
|
||||||
-- table to store the highlight names created by lualine
|
|
||||||
M.loaded_highlights = {}
|
|
||||||
|
|
||||||
-- sets loaded_highlights table
|
|
||||||
function M.save_highlight(highlight_name, highlight_args)
|
|
||||||
M.loaded_highlights[highlight_name] = highlight_args
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.reload_highlights()
|
|
||||||
local highlight = require('lualine.highlight')
|
|
||||||
for _, highlight_args in pairs(M.loaded_highlights) do
|
|
||||||
highlight.highlight(unpack(highlight_args))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- determine if an highlight exist and isn't cleared
|
|
||||||
function M.highlight_exists(highlight_name)
|
|
||||||
return M.loaded_highlights[highlight_name] and true or false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- clears loaded_highlights table and highlights
|
|
||||||
function M.clear_highlights()
|
|
||||||
for highlight_name, _ in pairs(M.loaded_highlights) do
|
|
||||||
vim.cmd('highlight clear ' .. highlight_name)
|
|
||||||
M.loaded_highlights[highlight_name] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- remove empty strings from list
|
-- remove empty strings from list
|
||||||
function M.list_shrink(list)
|
function M.list_shrink(list)
|
||||||
local new_list = {}
|
local new_list = {}
|
||||||
|
|
|
@ -1,38 +1,11 @@
|
||||||
local helpers = require 'tests.helpers'
|
local helpers = require 'tests.helpers'
|
||||||
|
|
||||||
local eq = assert.are.same
|
local eq = assert.are.same
|
||||||
local meths = helpers.meths
|
|
||||||
local build_component_opts = helpers.build_component_opts
|
local build_component_opts = helpers.build_component_opts
|
||||||
|
|
||||||
describe('Utils', function()
|
describe('Utils', function()
|
||||||
local utils = require('lualine.utils.utils')
|
local utils = require('lualine.utils.utils')
|
||||||
|
|
||||||
it('can save and restore highlights', function()
|
|
||||||
local hl1 = {'hl1', '#122233', '#445566', 'italic', nil, true}
|
|
||||||
utils.save_highlight(hl1[1], hl1)
|
|
||||||
-- highlight loaded in loaded_highlights table
|
|
||||||
eq(utils.loaded_highlights[hl1[1]], hl1)
|
|
||||||
-- highlight exists works properly
|
|
||||||
eq(utils.highlight_exists('hl1'), true)
|
|
||||||
eq(utils.highlight_exists('hl2'), false)
|
|
||||||
-- highlights can be restored
|
|
||||||
-- hl doesn't exist
|
|
||||||
assert.has_error(function() meths.get_hl_by_name('hl1', true) end,
|
|
||||||
'Invalid highlight name: hl1')
|
|
||||||
utils.reload_highlights()
|
|
||||||
-- Now hl1 is created
|
|
||||||
eq(meths.get_hl_by_name('hl1', true), {
|
|
||||||
foreground = tonumber(hl1[2]:sub(2, #hl1[2]), 16), -- convert rgb -> int
|
|
||||||
background = tonumber(hl1[3]:sub(2, #hl1[3]), 16), -- convert rgb -> int
|
|
||||||
italic = true
|
|
||||||
})
|
|
||||||
-- highlights can be cleared
|
|
||||||
utils.clear_highlights()
|
|
||||||
eq(utils.highlight_exists('hl1'), false)
|
|
||||||
-- highlight group has been cleared
|
|
||||||
eq(meths.get_hl_by_name('hl1', true), {[true] = 6})
|
|
||||||
end)
|
|
||||||
|
|
||||||
it('can retrive highlight groups', function()
|
it('can retrive highlight groups', function()
|
||||||
local hl2 = {fg = '#aabbcc', bg = '#889977', reverse = true}
|
local hl2 = {fg = '#aabbcc', bg = '#889977', reverse = true}
|
||||||
-- handles non existing hl groups
|
-- handles non existing hl groups
|
||||||
|
|
Loading…
Reference in New Issue