Move more logic out of loops

This commit is contained in:
Iron_E 2020-07-16 08:56:53 -04:00
parent d468be76c8
commit 3921d60476
No known key found for this signature in database
GPG Key ID: B0B37DE7EDC2335F
1 changed files with 42 additions and 47 deletions

View File

@ -26,21 +26,6 @@ local BIT_16 = 3
local BIT_256 = 2 local BIT_256 = 2
local HEX = 1 local HEX = 1
-- Generate a `:highlight` command from a group and some attributes.
local function highlight(highlight_group, attributes) -- {{{ †
local highlight_cmd = {'hi! ', highlight_group}
local link = (type(attributes) == 'string' and attributes)
or attributes.link
if link then -- `highlight_group` is a link to another group.
highlight_cmd[3] = highlight_cmd[2] .. ' '
highlight_cmd[2] = 'link '
highlight_cmd[4] = link
else -- the `highlight_group` is uniquely defined.
local bg = attributes.bg
local fg = attributes.fg
local style = attributes.style
-- Get the color value of a color variable, or "NONE" as a default. -- Get the color value of a color variable, or "NONE" as a default.
local function get(color, index) local function get(color, index)
if type(color) == 'table' and color[index] then if type(color) == 'table' and color[index] then
@ -52,40 +37,50 @@ local function highlight(highlight_group, attributes) -- {{{ †
end end
end end
-- If using hex and 256-bit colors, then populate the gui* and cterm* args. --[[ If using hex and 256-bit colors, then populate the gui* and cterm* args.
if use_hex_and_256 then highlight_cmd[#highlight_cmd + 1] = If using 16-bit colors, just populate the cterm* args. ]]
' ctermbg=' .. get(bg, BIT_256) local colorize = use_hex_and_256 and function(command, attributes) command[#command + 1] =
.. ' ctermfg=' .. get(fg, BIT_256) ' ctermbg='..get(attributes.bg, BIT_256)
.. ' guibg=' .. get(bg, HEX) ..' ctermfg='..get(attributes.fg, BIT_256)
.. ' guifg=' .. get(fg, HEX) ..' guibg='..get(attributes.bg, HEX)
-- If using 16-bit colors, just populate the cterm* args. ..' guifg='..get(attributes.fg, HEX)
else highlight_cmd[#highlight_cmd + 1] = end or function(command, attributes) command[#command + 1] =
' ctermbg=' .. get(bg, BIT_16) ' ctermbg='..get(attributes.bg, BIT_16)
.. ' ctermfg=' .. get(fg, BIT_16) ..' ctermfg='..get(attributes.fg, BIT_16)
end end
-- This function appends `selected_attributes` to the end of `highlight_cmd`. -- This function appends `selected_attributes` to the end of `highlight_cmd`.
local function append_style(selected_attributes) local function stylize(command, attributes)
highlight_cmd[#highlight_cmd + 1] = ' cterm=' .. selected_attributes command[#command + 1] = ' cterm='..attributes
-- If we're using hex populate the gui* attr args. if use_hex_and_256 then -- we're using hex populate the gui* attributes.
if use_hex_and_256 then highlight_cmd[#highlight_cmd + 1] = command[#command + 1] = ' gui='..attributes
' gui=' .. selected_attributes
end end
end end
-- Generate a `:highlight` command from a group and some attributes.
local function highlight(highlight_group, attributes) -- {{{ †
local highlight_cmd = {'hi! ', highlight_group}
local link = (type(attributes) == 'string' and attributes)
or attributes.link
if link then -- `highlight_group` is a link to another group.
highlight_cmd[3] = highlight_cmd[2]..' '
highlight_cmd[2] = 'link '
highlight_cmd[4] = link
else -- the `highlight_group` is uniquely defined.
colorize(highlight_cmd, attributes)
local style = attributes.style
if type(style) == 'table' then if type(style) == 'table' then
-- Concat all of the entries together with a comma between. -- Concat all of the entries together with a comma between before styling.
local style_all = table.concat(style, ',') stylize(table.concat(style, ','))
-- There will always be a cterm attr arg. if style.color then -- there won't is a color for undercurl.
append_style(style_all) highlight_cmd[#highlight_cmd + 1] = ' guisp='..get(style.color, HEX)
-- There won't always be a `guisp`.
if style.color then highlight_cmd[#highlight_cmd + 1] =
' guisp=' .. get(style.color, HEX)
end end
else append_style(style) else -- just style the single entry.
stylize(style)
end end
end end
@ -103,6 +98,6 @@ return function(Normal, highlights, terminal_ansi_colors)
-- Set the terminal colors. -- Set the terminal colors.
for index, color in ipairs(terminal_ansi_colors) do for index, color in ipairs(terminal_ansi_colors) do
vim.g['terminal_color_' .. index] = color[HEX] or color[BIT_256] or color[BIT_16] or 'NONE' vim.g['terminal_color_'..index] = color[HEX] or color[BIT_256] or color[BIT_16]
end end
end end