2021-02-13 00:03:57 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-03-15 23:37:46 +00:00
|
|
|
local M = {}
|
2022-01-02 11:38:39 +00:00
|
|
|
local lualine_require = require('lualine_require')
|
2021-08-30 11:54:23 +00:00
|
|
|
local require = lualine_require.require
|
2022-02-01 08:04:03 +00:00
|
|
|
local modules = lualine_require.lazy_require {
|
2021-08-30 11:54:23 +00:00
|
|
|
utils = 'lualine.utils.utils',
|
2021-08-30 12:07:24 +00:00
|
|
|
color_utils = 'lualine.utils.color_utils',
|
2022-02-01 08:04:03 +00:00
|
|
|
}
|
2021-09-03 14:16:54 +00:00
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
local section_highlight_map = { x = 'c', y = 'b', z = 'a' }
|
2021-04-07 00:34:36 +00:00
|
|
|
local active_theme = nil
|
2022-03-02 13:37:08 +00:00
|
|
|
local theme_hls = {}
|
2021-08-30 11:54:23 +00:00
|
|
|
local create_cterm_colors = false
|
2020-12-30 14:48:51 +00:00
|
|
|
|
2021-09-03 14:16:54 +00:00
|
|
|
-- table to store the highlight names created by lualine
|
|
|
|
local loaded_highlights = {}
|
|
|
|
|
2021-09-26 00:15:22 +00:00
|
|
|
-- table to map mode to highlight suffixes
|
|
|
|
local mode_to_highlight = {
|
|
|
|
['VISUAL'] = '_visual',
|
|
|
|
['V-BLOCK'] = '_visual',
|
|
|
|
['V-LINE'] = '_visual',
|
|
|
|
['SELECT'] = '_visual',
|
|
|
|
['S-LINE'] = '_visual',
|
|
|
|
['S-BLOCK'] = '_visual',
|
|
|
|
['REPLACE'] = '_replace',
|
|
|
|
['V-REPLACE'] = '_replace',
|
|
|
|
['INSERT'] = '_insert',
|
|
|
|
['COMMAND'] = '_command',
|
|
|
|
['EX'] = '_command',
|
|
|
|
['MORE'] = '_command',
|
|
|
|
['CONFIRM'] = '_command',
|
|
|
|
['TERMINAL'] = '_terminal',
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
--- determine if an highlight exist and isn't cleared
|
|
|
|
---@param highlight_name string
|
|
|
|
---@return boolean whether hl_group was defined with highlight_name
|
2021-09-03 14:16:54 +00:00
|
|
|
function M.highlight_exists(highlight_name)
|
|
|
|
return loaded_highlights[highlight_name] or false
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
--- clears loaded_highlights table and highlights
|
2021-09-03 14:16:54 +00:00
|
|
|
local function clear_highlights()
|
|
|
|
for highlight_name, _ in pairs(loaded_highlights) do
|
|
|
|
vim.cmd('highlight clear ' .. highlight_name)
|
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
loaded_highlights = {}
|
2021-09-03 14:16:54 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---converts cterm, color_name type colors to #rrggbb format
|
|
|
|
---@param color string|number
|
|
|
|
---@return string
|
2021-08-02 14:25:34 +00:00
|
|
|
local function sanitize_color(color)
|
2022-03-02 13:37:08 +00:00
|
|
|
if color == nil or color == '' then
|
|
|
|
return 'None'
|
|
|
|
end
|
2021-08-02 14:25:34 +00:00
|
|
|
if type(color) == 'string' then
|
2021-09-03 18:28:20 +00:00
|
|
|
if color:sub(1, 1) == '#' then
|
|
|
|
return color
|
|
|
|
end -- RGB value
|
2021-08-30 11:54:23 +00:00
|
|
|
return modules.color_utils.color_name2rgb(color)
|
2021-08-02 14:25:34 +00:00
|
|
|
elseif type(color) == 'number' then
|
|
|
|
if color > 255 then
|
2021-09-03 18:28:20 +00:00
|
|
|
error("What's this it can't be higher then 255 and you've given " .. color)
|
2021-08-02 14:25:34 +00:00
|
|
|
end
|
2021-08-30 11:54:23 +00:00
|
|
|
return modules.color_utils.cterm2rgb(color)
|
2021-08-02 14:25:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-02 13:37:08 +00:00
|
|
|
function M.get_lualine_hl(name)
|
|
|
|
local hl = loaded_highlights[name]
|
|
|
|
if hl and not hl.empty then
|
|
|
|
if hl.link then
|
|
|
|
return modules.utils.extract_highlight_colors(hl.link)
|
|
|
|
end
|
|
|
|
local hl_def = {
|
|
|
|
fg = hl.fg ~= 'None' and vim.deepcopy(hl.fg),
|
|
|
|
bg = hl.bg ~= 'None' and vim.deepcopy(hl.bg),
|
|
|
|
}
|
|
|
|
if hl.gui then
|
|
|
|
for _, flag in ipairs(vim.split(hl.gui, ',')) do
|
|
|
|
if flag ~= 'None' then
|
|
|
|
hl_def[flag] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return hl_def
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
--- Define a hl_group
|
|
|
|
---@param name string
|
|
|
|
---@param foreground string|number: color
|
|
|
|
---@param background string|number: color
|
|
|
|
---@param gui table cterm/gui options like bold/italic ect
|
|
|
|
---@param link string hl_group name to link new hl to
|
2021-09-03 14:16:54 +00:00
|
|
|
function M.highlight(name, foreground, background, gui, link)
|
2021-09-03 18:28:20 +00:00
|
|
|
local command = { 'highlight!' }
|
2021-08-09 07:53:42 +00:00
|
|
|
if link and #link > 0 then
|
2022-03-02 13:37:08 +00:00
|
|
|
if loaded_highlights[name] and loaded_highlights[name].link == link then
|
|
|
|
return
|
|
|
|
end
|
2021-09-03 18:28:20 +00:00
|
|
|
vim.list_extend(command, { 'link', name, link })
|
2021-08-09 07:53:42 +00:00
|
|
|
else
|
|
|
|
foreground = sanitize_color(foreground)
|
|
|
|
background = sanitize_color(background)
|
2022-03-02 13:37:08 +00:00
|
|
|
gui = (gui ~= nil and gui ~= '') and gui or 'None'
|
|
|
|
if
|
|
|
|
loaded_highlights[name]
|
|
|
|
and loaded_highlights[name].fg == foreground
|
|
|
|
and loaded_highlights[name].bg == background
|
|
|
|
and loaded_highlights[name].gui == gui
|
|
|
|
then
|
|
|
|
return -- color is already defined why are we doing this anyway ?
|
2021-04-08 02:43:06 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
table.insert(command, name)
|
|
|
|
table.insert(command, 'guifg=' .. foreground)
|
|
|
|
table.insert(command, 'guibg=' .. background)
|
|
|
|
table.insert(command, 'gui=' .. gui)
|
|
|
|
if create_cterm_colors then
|
|
|
|
table.insert(command, 'ctermfg=' .. modules.color_utils.rgb2cterm(foreground))
|
|
|
|
table.insert(command, 'ctermbg=' .. modules.color_utils.rgb2cterm(background))
|
2021-08-09 07:53:42 +00:00
|
|
|
table.insert(command, 'cterm=' .. gui)
|
2021-04-08 02:43:06 +00:00
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
vim.cmd(table.concat(command, ' '))
|
2022-03-02 13:37:08 +00:00
|
|
|
|
|
|
|
-- update attached hl groups
|
|
|
|
local old_hl_def = loaded_highlights[name]
|
|
|
|
if old_hl_def and next(old_hl_def.attached) then
|
|
|
|
-- Update attached hl groups as they announced to depend on hl_group 'name'
|
|
|
|
-- 'hl' being in 'name'a attached table means 'hl'
|
|
|
|
-- depends of 'name'.
|
|
|
|
-- 'hl' key in attached table will contain a table that
|
|
|
|
-- defines the reletaion between 'hl' & 'name'.
|
|
|
|
-- name.attached.hl = { bg = 'fg' } means
|
|
|
|
-- hl's fg is same as 'names' bg . So 'hl's fg should
|
|
|
|
-- be updated when ever 'name' changes it's 'bg'
|
|
|
|
local bg_changed = old_hl_def.bg ~= background
|
|
|
|
local fg_changed = old_hl_def.bg ~= foreground
|
|
|
|
local gui_changed = old_hl_def.gui ~= gui
|
|
|
|
for attach_name, attach_desc in pairs(old_hl_def.attached) do
|
|
|
|
if bg_changed and attach_desc.bg and loaded_highlights[attach_name] then
|
|
|
|
M.highlight(
|
|
|
|
attach_name,
|
|
|
|
attach_desc.bg == 'fg' and background or loaded_highlights[attach_name].fg,
|
|
|
|
attach_desc.bg == 'bg' and background or loaded_highlights[attach_name].bg,
|
|
|
|
loaded_highlights[attach_name].gui,
|
|
|
|
loaded_highlights[attach_name].link
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if fg_changed and attach_desc.fg and loaded_highlights[attach_name] then
|
|
|
|
M.highlight(
|
|
|
|
attach_name,
|
|
|
|
attach_desc.fg == 'fg' and foreground or loaded_highlights[attach_name].fg,
|
|
|
|
attach_desc.fg == 'bg' and foreground or loaded_highlights[attach_name].bg,
|
|
|
|
loaded_highlights[attach_name].gui,
|
|
|
|
loaded_highlights[attach_name].link
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if gui_changed and attach_desc.gui and loaded_highlights[attach_name] then
|
|
|
|
M.highlight(
|
|
|
|
attach_name,
|
|
|
|
loaded_highlights[attach_name].fg,
|
|
|
|
loaded_highlights[attach_name].bg,
|
|
|
|
gui,
|
|
|
|
loaded_highlights[attach_name].link
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- store current hl state
|
|
|
|
loaded_highlights[name] = {
|
|
|
|
fg = foreground,
|
|
|
|
bg = background,
|
|
|
|
gui = gui,
|
|
|
|
link = link,
|
|
|
|
attached = old_hl_def and old_hl_def.attached or {},
|
|
|
|
}
|
2021-01-05 15:08:56 +00:00
|
|
|
end
|
|
|
|
|
2022-03-02 13:37:08 +00:00
|
|
|
---Attach an hl to another so attachee auto updated on change to hl it's attached too.
|
|
|
|
---@param provider string the hl receiver is getting attached to
|
|
|
|
---@param receiver string the hl that will be auto updated upon change to provider
|
|
|
|
---@param provider_el_type string (fg/bg) what element receiver relates to of provider
|
|
|
|
---@param receiver_el_type string (fg/bg) what element provider relates to of receiver
|
|
|
|
local function attach_hl(provider, receiver, provider_el_type, receiver_el_type)
|
|
|
|
if loaded_highlights[provider] == nil then
|
|
|
|
loaded_highlights[provider] = { empty = true, attached = {} }
|
|
|
|
end
|
|
|
|
loaded_highlights[provider].attached[receiver] = { [provider_el_type] = receiver_el_type }
|
|
|
|
end
|
2021-10-12 14:04:47 +00:00
|
|
|
---define hl_groups for a theme
|
|
|
|
---@param theme table
|
2021-01-04 01:14:29 +00:00
|
|
|
function M.create_highlight_groups(theme)
|
2021-09-03 14:16:54 +00:00
|
|
|
clear_highlights()
|
2021-04-07 00:34:36 +00:00
|
|
|
active_theme = theme
|
2022-03-02 13:37:08 +00:00
|
|
|
theme_hls = {}
|
|
|
|
local psudo_options = { self = { section = 'a' } }
|
2021-08-30 12:07:24 +00:00
|
|
|
create_cterm_colors = not vim.go.termguicolors
|
2020-12-30 14:48:51 +00:00
|
|
|
for mode, sections in pairs(theme) do
|
2022-03-02 13:37:08 +00:00
|
|
|
theme_hls[mode] = {}
|
2021-08-09 07:53:42 +00:00
|
|
|
for section, color in pairs(sections) do
|
2022-03-02 13:37:08 +00:00
|
|
|
local hl_tag = mode
|
|
|
|
psudo_options.self.section = section
|
|
|
|
theme_hls[mode][section] = M.create_component_highlight_group(color, hl_tag, psudo_options, true)
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---@description: adds '_mode' at end of highlight_group
|
|
|
|
---@param highlight_group string name of highlight group
|
|
|
|
---@return string highlight group name with mode
|
2022-03-02 13:37:08 +00:00
|
|
|
local function append_mode(highlight_group, is_focused)
|
2021-09-19 06:20:07 +00:00
|
|
|
if is_focused == nil then
|
|
|
|
is_focused = modules.utils.is_focused()
|
|
|
|
end
|
|
|
|
if is_focused == false then
|
|
|
|
return highlight_group .. '_inactive'
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
local mode = require('lualine.utils.mode').get_mode()
|
2021-09-26 00:15:22 +00:00
|
|
|
return highlight_group .. (mode_to_highlight[mode] or '_normal')
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
|
2021-08-19 19:23:44 +00:00
|
|
|
-- Helper function for create component highlight
|
2021-10-12 14:04:47 +00:00
|
|
|
---Handles fall back of colors when creating highlight group
|
2022-03-02 13:37:08 +00:00
|
|
|
---@param hl_name string name of highlight that we are setting default values for
|
|
|
|
---@param mode string mode which default component color should be given.
|
|
|
|
---@param section string the lualine section component is in.
|
2021-10-12 14:04:47 +00:00
|
|
|
---@param color table color passed for creating component highlight
|
2022-03-02 13:37:08 +00:00
|
|
|
---@param options table Options table of component this is first fall back
|
|
|
|
local function get_default_component_color(hl_name, mode, section, color, options)
|
|
|
|
local default_theme_color
|
|
|
|
if active_theme[mode] and active_theme[mode][section] then
|
|
|
|
default_theme_color = active_theme[mode][section]
|
|
|
|
elseif section >= 'c' and active_theme[mode] and active_theme[mode][section_highlight_map[section]] then
|
|
|
|
default_theme_color = active_theme[mode][section_highlight_map[section]]
|
|
|
|
elseif section >= 'c' and active_theme.normal[section_highlight_map[section]] then
|
|
|
|
default_theme_color = active_theme.normal[section_highlight_map[section]]
|
|
|
|
else
|
|
|
|
default_theme_color = active_theme.normal[section]
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
|
|
|
|
local ret = { fg = color.fg, bg = color.bg, gui = color.gui }
|
|
|
|
if ret.fg and ret.bg then
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
local function apply_default(def_color, def_name)
|
|
|
|
if type(def_color) == 'function' and loaded_highlights[def_name] and not loaded_highlights[def_name].empty then
|
|
|
|
if loaded_highlights[def_name].link then
|
|
|
|
def_color = loaded_highlights[def_name].link
|
|
|
|
else
|
|
|
|
def_color = loaded_highlights[def_name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if type(def_color) == 'function' then
|
|
|
|
def_color = def_color { section = section }
|
|
|
|
end
|
|
|
|
if type(def_color) == 'string' then
|
|
|
|
def_color = modules.utils.extract_highlight_colors(def_color)
|
|
|
|
end
|
|
|
|
if type(def_color) == 'table' then
|
|
|
|
if not ret.fg then
|
|
|
|
ret.fg = def_color.fg
|
|
|
|
attach_hl(def_name, hl_name, 'fg', 'fg')
|
|
|
|
end
|
|
|
|
if not ret.bg then
|
|
|
|
ret.bg = def_color.bg
|
|
|
|
attach_hl(def_name, hl_name, 'bg', 'bg')
|
|
|
|
end
|
2021-08-19 19:23:44 +00:00
|
|
|
end
|
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
|
|
|
|
if
|
|
|
|
options.color
|
|
|
|
and options.color_highlight
|
|
|
|
and options.color_highlight.name
|
|
|
|
and options.color_highlight.name .. '_' .. mode ~= hl_name
|
|
|
|
then
|
|
|
|
apply_default(options.color, options.color_highlight.name .. '_' .. mode)
|
|
|
|
else
|
|
|
|
apply_default(default_theme_color, string.format('lualine_%s_%s', section, mode))
|
2021-08-19 19:23:44 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
ret.fg = sanitize_color(ret.fg)
|
|
|
|
ret.bg = sanitize_color(ret.bg)
|
|
|
|
return ret
|
2021-08-19 19:23:44 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---Create highlight group with fg bg and gui from theme
|
|
|
|
---@param color table has to be { fg = "#rrggbb", bg="#rrggbb" gui = "effect" }
|
|
|
|
--- all the color elements are optional if fg or bg is not given options
|
|
|
|
--- must be provided So fg and bg can default the themes colors
|
|
|
|
---@param highlight_tag string is unique tag for highlight group
|
|
|
|
---returns the name of highlight group
|
|
|
|
---@param options table is parameter of component.init() function
|
2022-03-02 13:37:08 +00:00
|
|
|
---@return table that can be used by component_format_highlight
|
2021-10-12 14:04:47 +00:00
|
|
|
--- to retrieve highlight group
|
2022-03-02 13:37:08 +00:00
|
|
|
function M.create_component_highlight_group(color, highlight_tag, options, apply_no_default)
|
|
|
|
local section = options.self.section
|
2021-07-27 06:57:51 +00:00
|
|
|
local tag_id = 0
|
2021-09-03 18:28:20 +00:00
|
|
|
while
|
2022-03-02 13:37:08 +00:00
|
|
|
M.highlight_exists(table.concat({ 'lualine', section, highlight_tag }, '_'))
|
|
|
|
or (section and M.highlight_exists(table.concat({ 'lualine', section, highlight_tag, 'normal' }, '_')))
|
2021-09-03 18:28:20 +00:00
|
|
|
do
|
2021-07-27 06:57:51 +00:00
|
|
|
highlight_tag = highlight_tag .. '_' .. tostring(tag_id)
|
|
|
|
tag_id = tag_id + 1
|
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
|
2021-08-09 07:53:42 +00:00
|
|
|
if type(color) == 'string' then
|
2022-03-02 13:37:08 +00:00
|
|
|
local highlight_group_name = table.concat({ 'lualine', section, highlight_tag }, '_')
|
2021-08-09 07:53:42 +00:00
|
|
|
M.highlight(highlight_group_name, nil, nil, nil, color) -- l8nk to group
|
2022-03-02 13:37:08 +00:00
|
|
|
return {
|
|
|
|
name = highlight_group_name,
|
|
|
|
fn = nil,
|
|
|
|
no_mode = true,
|
|
|
|
link = true,
|
|
|
|
section = section,
|
|
|
|
options = options,
|
|
|
|
no_default = apply_no_default,
|
|
|
|
}
|
2021-08-09 07:53:42 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
|
|
|
|
if type(color) ~= 'function' and (apply_no_default or (color.bg and color.fg)) then
|
2021-02-15 18:09:12 +00:00
|
|
|
-- When bg and fg are both present we donn't need to set highlighs for
|
2021-02-20 03:21:05 +00:00
|
|
|
-- each mode as they will surely look the same. So we can work without options
|
2022-03-02 13:37:08 +00:00
|
|
|
local highlight_group_name = table.concat({ 'lualine', section, highlight_tag }, '_')
|
2021-08-09 07:53:42 +00:00
|
|
|
M.highlight(highlight_group_name, color.fg, color.bg, color.gui, nil)
|
2022-03-02 13:37:08 +00:00
|
|
|
return {
|
|
|
|
name = highlight_group_name,
|
|
|
|
fn = nil,
|
|
|
|
no_mode = true,
|
|
|
|
section = section,
|
|
|
|
options = options,
|
|
|
|
no_default = apply_no_default,
|
|
|
|
}
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
|
2021-03-15 23:37:46 +00:00
|
|
|
local modes = {
|
2021-09-03 18:28:20 +00:00
|
|
|
'normal',
|
|
|
|
'insert',
|
|
|
|
'visual',
|
|
|
|
'replace',
|
|
|
|
'command',
|
|
|
|
'terminal',
|
|
|
|
'inactive',
|
2021-03-15 23:37:46 +00:00
|
|
|
}
|
2021-02-15 18:09:12 +00:00
|
|
|
for _, mode in ipairs(modes) do
|
2022-03-02 13:37:08 +00:00
|
|
|
local hl_name = table.concat({ 'lualine', section, highlight_tag, mode }, '_')
|
|
|
|
local cl = color
|
|
|
|
if type(color) == 'function' then
|
|
|
|
cl = color { section = section } or {}
|
|
|
|
end
|
|
|
|
if type(cl) == 'string' then
|
|
|
|
cl = { link = cl }
|
2021-02-20 03:21:05 +00:00
|
|
|
else
|
2022-03-02 13:37:08 +00:00
|
|
|
cl = get_default_component_color(hl_name, mode, section, cl, options)
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
M.highlight(hl_name, cl.fg, cl.bg, cl.gui, cl.link)
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
return {
|
|
|
|
name = table.concat({ 'lualine', section, highlight_tag }, '_'),
|
|
|
|
fn = type(color) == 'function' and color,
|
|
|
|
no_mode = false,
|
|
|
|
link = false,
|
|
|
|
section = section,
|
|
|
|
options = options,
|
|
|
|
no_default = apply_no_default,
|
|
|
|
}
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---@description: retrieve highlight_groups for components
|
2022-03-02 13:37:08 +00:00
|
|
|
---@param highlight table return value of create_component_highlight_group
|
2021-10-12 14:04:47 +00:00
|
|
|
--- return value of create_component_highlight_group is to be passed in
|
|
|
|
--- this parameter to receive highlight that was created
|
|
|
|
---@return string formatted highlight group name
|
2022-03-02 13:37:08 +00:00
|
|
|
function M.component_format_highlight(highlight, is_focused)
|
|
|
|
if not highlight.fn then
|
|
|
|
local highlight_group = highlight.name
|
|
|
|
if highlight.no_mode then
|
|
|
|
return '%#' .. highlight_group .. '#'
|
|
|
|
end
|
|
|
|
highlight_group = append_mode(highlight_group, is_focused)
|
2021-03-15 23:37:46 +00:00
|
|
|
return '%#' .. highlight_group .. '#'
|
2021-02-20 03:21:05 +00:00
|
|
|
else
|
2022-03-02 13:37:08 +00:00
|
|
|
local color = highlight.fn { section = highlight.section } or {}
|
|
|
|
local hl_name = highlight.name
|
|
|
|
if type(color) == 'string' then
|
|
|
|
M.highlight(hl_name, nil, nil, nil, color)
|
|
|
|
return '%#' .. hl_name .. '#'
|
|
|
|
elseif type(color) == 'table' then
|
|
|
|
if not highlight.no_default and not (color.fg and color.bg) then
|
|
|
|
hl_name = append_mode(highlight.name, is_focused)
|
|
|
|
color = get_default_component_color(
|
|
|
|
hl_name,
|
|
|
|
append_mode(''):sub(2),
|
|
|
|
highlight.section,
|
|
|
|
color,
|
|
|
|
highlight.options
|
|
|
|
)
|
|
|
|
end
|
|
|
|
M.highlight(hl_name, color.fg, color.bg, color.gui, color.link)
|
|
|
|
return '%#' .. hl_name .. '#', color
|
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---@description: retrieve highlight_groups for section
|
2022-03-02 13:37:08 +00:00
|
|
|
---@param section string highlight group name without mode
|
2021-10-12 14:04:47 +00:00
|
|
|
--- return value of create_component_highlight_group is to be passed in
|
|
|
|
--- this parameter to receive highlight that was created
|
|
|
|
---@param is_focused boolean
|
|
|
|
---@return string formatted highlight group name
|
2022-03-02 13:37:08 +00:00
|
|
|
function M.format_highlight(section, is_focused)
|
|
|
|
local mode = append_mode('', is_focused):sub(2)
|
|
|
|
local ret = ''
|
|
|
|
|
|
|
|
if theme_hls[mode] and theme_hls[mode][section] then
|
|
|
|
ret = M.component_format_highlight(theme_hls[mode][section], is_focused)
|
|
|
|
elseif theme_hls[mode] and section > 'c' and theme_hls[mode][section_highlight_map[section]] then
|
|
|
|
ret = M.component_format_highlight(theme_hls[mode][section_highlight_map[section]], is_focused)
|
|
|
|
elseif theme_hls['normal'] and theme_hls['normal'][section] then
|
|
|
|
ret = M.component_format_highlight(theme_hls['normal'][section], is_focused)
|
|
|
|
elseif theme_hls['normal'] and section > 'c' and theme_hls['normal'][section_highlight_map[section]] then
|
|
|
|
ret = M.component_format_highlight(theme_hls['normal'][section_highlight_map[section]], is_focused)
|
2021-02-21 14:37:59 +00:00
|
|
|
end
|
2022-03-02 13:37:08 +00:00
|
|
|
|
|
|
|
return ret
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---@description : Provides transitional highlights for section separators.
|
|
|
|
---@param left_hl string this highlights bg is used for fg of transitional hl
|
|
|
|
---@param right_hl string this highlights bg is used for bg of transitional hl
|
|
|
|
--- '▶️' and '◀️' ' needs reverse colors so the caller should swap left and right
|
|
|
|
---@return string formatted highlight group name
|
2021-08-25 11:43:12 +00:00
|
|
|
function M.get_transitional_highlights(left_hl, right_hl)
|
|
|
|
-- When both left and right highlights are same or one is absent
|
|
|
|
-- nothing to transition to.
|
2021-09-03 18:28:20 +00:00
|
|
|
if left_hl == nil or right_hl == nil or left_hl == right_hl then
|
|
|
|
return nil
|
|
|
|
end
|
2021-02-20 03:21:05 +00:00
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
-- construct the name of highlight group
|
2021-09-03 18:28:20 +00:00
|
|
|
local highlight_name = table.concat({ 'lualine_transitional', left_hl, 'to', right_hl }, '_')
|
2021-09-03 14:16:54 +00:00
|
|
|
if not M.highlight_exists(highlight_name) then
|
2021-02-20 03:21:05 +00:00
|
|
|
-- Create the highlight_group if needed
|
2021-03-22 14:29:37 +00:00
|
|
|
-- Get colors from highlights
|
2021-08-30 11:54:23 +00:00
|
|
|
local fg = modules.utils.extract_highlight_colors(left_hl, 'bg')
|
|
|
|
local bg = modules.utils.extract_highlight_colors(right_hl, 'bg')
|
2022-02-20 08:55:03 +00:00
|
|
|
if not fg and not bg then
|
|
|
|
return nil -- Color retrieval failed
|
|
|
|
end
|
2021-09-03 18:28:20 +00:00
|
|
|
if bg == fg then
|
2022-02-20 08:55:03 +00:00
|
|
|
return nil -- Separator won't be visible anyway
|
|
|
|
end
|
2021-08-09 07:53:42 +00:00
|
|
|
M.highlight(highlight_name, fg, bg, nil)
|
2022-03-02 13:37:08 +00:00
|
|
|
attach_hl(left_hl, highlight_name, 'bg', 'fg')
|
|
|
|
attach_hl(right_hl, highlight_name, 'bg', 'bg')
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
|
|
|
return '%#' .. highlight_name .. '#'
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|