Refactor: Remove cterm from hlighlight extractor (#174)

- remove cterm color extraction
- fix lint issues
- apply lua-format
This commit is contained in:
Shadman 2021-04-13 18:36:51 +06:00 committed by GitHub
parent 8a99a0e9e7
commit 4f53d053a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 49 deletions

View File

@ -83,7 +83,8 @@ local Component = {
self.status = highlight.component_format_highlight( self.status = highlight.component_format_highlight(
self.options.color_highlight) .. self.status self.options.color_highlight) .. self.status
elseif self.options.color_highlight_link then elseif self.options.color_highlight_link then
self.status = '%#' .. self.options.color_highlight_link ..'#'.. self.status self.status = '%#' .. self.options.color_highlight_link .. '#' ..
self.status
end end
self.status = self.status .. default_highlight self.status = self.status .. default_highlight
end, end,
@ -117,7 +118,9 @@ local Component = {
-- variable to store component output for manupulation -- variable to store component output for manupulation
status = '', status = '',
-- Actual function the updates a component . Must be overwritten with component functionality -- Actual function the updates a component . Must be overwritten with component functionality
-- luacheck: push no unused args
update_status = function(self) end, update_status = function(self) end,
-- luacheck: pop
-- Driver code of the class -- Driver code of the class
draw = function(self, default_highlight) draw = function(self, default_highlight)

View File

@ -36,17 +36,17 @@ Diagnostics.new = function(self, options, child)
-- apply colors -- apply colors
if not new_diagnostics.options.color_error then if not new_diagnostics.options.color_error then
new_diagnostics.options.color_error = new_diagnostics.options.color_error =
utils.extract_highlight_colors('DiffDelete', 'guifg') or utils.extract_highlight_colors('DiffDelete', 'fg') or
Diagnostics.default_colors.error Diagnostics.default_colors.error
end end
if not new_diagnostics.options.color_warn then if not new_diagnostics.options.color_warn then
new_diagnostics.options.color_warn = new_diagnostics.options.color_warn =
utils.extract_highlight_colors('DiffText', 'guifg') or utils.extract_highlight_colors('DiffText', 'fg') or
Diagnostics.default_colors.warn Diagnostics.default_colors.warn
end end
if not new_diagnostics.options.color_info then if not new_diagnostics.options.color_info then
new_diagnostics.options.color_info = new_diagnostics.options.color_info =
utils.extract_highlight_colors('Normal', 'guifg') or utils.extract_highlight_colors('Normal', 'fg') or
Diagnostics.default_colors.info Diagnostics.default_colors.info
end end

View File

@ -33,17 +33,17 @@ Diff.new = function(self, options, child)
-- apply colors -- apply colors
if not new_instence.options.color_added then if not new_instence.options.color_added then
new_instence.options.color_added = utils.extract_highlight_colors('DiffAdd', new_instence.options.color_added = utils.extract_highlight_colors('DiffAdd',
'guifg') or 'fg') or
Diff.default_colors.added Diff.default_colors.added
end end
if not new_instence.options.color_modified then if not new_instence.options.color_modified then
new_instence.options.color_modified = new_instence.options.color_modified =
utils.extract_highlight_colors('DiffChange', 'guifg') or utils.extract_highlight_colors('DiffChange', 'fg') or
Diff.default_colors.modified Diff.default_colors.modified
end end
if not new_instence.options.color_removed then if not new_instence.options.color_removed then
new_instence.options.color_removed = new_instence.options.color_removed =
utils.extract_highlight_colors('DiffDelete', 'guifg') or utils.extract_highlight_colors('DiffDelete', 'fg') or
Diff.default_colors.removed Diff.default_colors.removed
end end

View File

@ -12,13 +12,15 @@ function M.highlight(name, foreground, background, gui, reload)
if foreground and foreground ~= 'none' then if foreground and foreground ~= 'none' then
table.insert(command, 'guifg=' .. foreground) table.insert(command, 'guifg=' .. foreground)
if cterm_colors then if cterm_colors then
table.insert(command, 'ctermfg=' .. utils_colors.get_cterm_color(foreground)) table.insert(command,
'ctermfg=' .. utils_colors.get_cterm_color(foreground))
end end
end end
if background and background ~= 'none' then if background and background ~= 'none' then
table.insert(command, 'guibg=' .. background) table.insert(command, 'guibg=' .. background)
if cterm_colors then if cterm_colors then
table.insert(command, 'ctermbg=' .. utils_colors.get_cterm_color(background)) table.insert(command,
'ctermbg=' .. utils_colors.get_cterm_color(background))
end end
end end
if gui then if gui then
@ -141,7 +143,7 @@ function M.format_highlight(is_focused, highlight_group)
section_highlight_map[highlight_group:match( section_highlight_map[highlight_group:match(
'lualine_(.)')] 'lualine_(.)')]
end end
local highlight_name = highlight_group local highlight_name
if not is_focused then if not is_focused then
highlight_name = highlight_group .. [[_inactive]] highlight_name = highlight_group .. [[_inactive]]
else else
@ -199,8 +201,8 @@ function M.get_transitional_highlights(left_section_data, right_section_data,
-- Create the highlight_group if needed -- Create the highlight_group if needed
-- Get colors from highlights -- Get colors from highlights
-- using string.format to convert decimal to hexadecimal -- using string.format to convert decimal to hexadecimal
local fg = utils.extract_highlight_colors(left_highlight_name, 'guibg') local fg = utils.extract_highlight_colors(left_highlight_name, 'bg')
local bg = utils.extract_highlight_colors(right_highlight_name, 'guibg') local bg = utils.extract_highlight_colors(right_highlight_name, 'bg')
if not fg then fg = 'none' end if not fg then fg = 'none' end
if not bg then bg = 'none' end if not bg then bg = 'none' end
-- swap the bg and fg when reverse is true. As in that case highlight will -- swap the bg and fg when reverse is true. As in that case highlight will

View File

@ -251,14 +251,14 @@ end
local function set_tabline() local function set_tabline()
if next(config.tabline) ~= nil then if next(config.tabline) ~= nil then
vim.o.tabline = "%!v:lua.require'lualine'.tabline()" vim.o.tabline = '%!v:lua.require\'lualine\'.tabline()'
vim.o.showtabline = 2 vim.o.showtabline = 2
end end
end end
local function set_statusline() local function set_statusline()
if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then
vim.o.statusline = "%!v:lua.require'lualine'.statusline()" vim.o.statusline = '%!v:lua.require\'lualine\'.statusline()'
vim.api.nvim_exec([[ vim.api.nvim_exec([[
autocmd lualine WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline() autocmd lualine WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline()
autocmd lualine WinEnter,BufEnter * set statusline< autocmd lualine WinEnter,BufEnter * set statusline<
@ -277,8 +277,4 @@ local function setup(user_config)
set_tabline() set_tabline()
end end
return { return {setup = setup, statusline = status_dispatch, tabline = tabline}
setup = setup,
statusline = status_dispatch,
tabline = tabline,
}

View File

@ -15,10 +15,10 @@ local function getHi(scope, syntaxlist)
if vim.fn.hlexists(highlight_name) ~= 0 then if vim.fn.hlexists(highlight_name) ~= 0 then
local color = utils.extract_highlight_colors(highlight_name) local color = utils.extract_highlight_colors(highlight_name)
if color.reverse then if color.reverse then
if scope == 'guibg' then if scope == 'bg' then
scope = 'guifg' scope = 'fg'
else else
scope = 'guibg' scope = 'bg'
end end
end end
if color[scope] then return color[scope] end if color[scope] then return color[scope] end
@ -104,20 +104,20 @@ end
-- Get the colors to create theme -- Get the colors to create theme
local colors = { local colors = {
normal = getHi('guibg', {'PmenuSel', 'PmenuThumb', 'TabLineSel'}), normal = getHi('bg', {'PmenuSel', 'PmenuThumb', 'TabLineSel'}),
insert = getHi('guifg', {'String', 'MoreMsg'}), insert = getHi('fg', {'String', 'MoreMsg'}),
replace = getHi('guifg', {'Number', 'Type'}), replace = getHi('fg', {'Number', 'Type'}),
visual = getHi('guifg', {'Special', 'Boolean', 'Constant'}), visual = getHi('fg', {'Special', 'Boolean', 'Constant'}),
command = getHi('guifg', {'Identifier'}), command = getHi('fg', {'Identifier'}),
back1 = getHi('guibg', {'Normal', 'StatusLineNC'}), back1 = getHi('bg', {'Normal', 'StatusLineNC'}),
fore = getHi('guifg', {'Normal', 'StatusLine'}), fore = getHi('fg', {'Normal', 'StatusLine'}),
back2 = getHi('guibg', {'StatusLine'}) back2 = getHi('bg', {'StatusLine'})
} }
-- Change brightness of colors -- Change brightness of colors
-- darken incase of light theme lighten incase of dark theme -- darken incase of light theme lighten incase of dark theme
if get_color_brightness(utils.extract_highlight_colors('Normal', 'guibg')) > 0.5 then if get_color_brightness(utils.extract_highlight_colors('Normal', 'bg')) > 0.5 then
brightness_modifier_parameter = -brightness_modifier_parameter brightness_modifier_parameter = -brightness_modifier_parameter
end end

View File

@ -2,26 +2,19 @@
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local M = {} local M = {}
-- Note for now only works for termguicolors scope can be background or foreground -- Note for now only works for termguicolors scope can be bg or fg or any other
-- attr parameter like bold/italic/reverse
function M.extract_highlight_colors(color_group, scope) function M.extract_highlight_colors(color_group, scope)
if vim.fn.hlexists(color_group) == 0 then return nil end if vim.fn.hlexists(color_group) == 0 then return nil end
local gui_colors = vim.api.nvim_get_hl_by_name(color_group, true) local color = vim.api.nvim_get_hl_by_name(color_group, true)
local cterm_colors = vim.api.nvim_get_hl_by_name(color_group, false) if color.background ~= nil then
local color = { color.bg = string.format('#%06x', color.background)
ctermfg = cterm_colors.foreground, color.background = nil
ctermbg = cterm_colors.background
}
if gui_colors.background then
color.guibg = string.format('#%06x', gui_colors.background)
gui_colors.background = nil
end end
if gui_colors.foreground then if color.foreground ~= nil then
color.guifg = string.format('#%06x', gui_colors.foreground) color.fg = string.format('#%06x', color.foreground)
gui_colors.foreground = nil color.foreground = nil
end end
cterm_colors.background = nil
cterm_colors.foreground = nil
color = vim.tbl_extend('keep', color, gui_colors, cterm_colors)
if scope then return color[scope] end if scope then return color[scope] end
return color return color
end end