Skip to content

Commit

Permalink
feat: Allow cterm highlight number through (#1194)
Browse files Browse the repository at this point in the history
Co-authored-by: Shadman <13149513+shadmansaleh@users.noreply.github.com>
  • Loading branch information
AntoinePrv and shadmansaleh committed Apr 5, 2024
1 parent 15d830d commit 0a5a668
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lua/lualine/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ local function sanitize_color(color)
end
end

---converts color_name type colors to cterm format and let cterm color pass through
---@param color string|number
---@return string
local function sanitize_color_for_cterm(color)
if type(color) == 'number' then
if color > 255 then
error("What's this it can't be higher then 255 and you've given " .. color)
end
return color
end
return modules.color_utils.rgb2cterm(sanitize_color(color))
end

function M.get_lualine_hl(name)
local hl = loaded_highlights[name]
if hl and not hl.empty then
Expand Down Expand Up @@ -113,24 +126,25 @@ function M.highlight(name, foreground, background, gui, link)
end
vim.list_extend(command, { 'link', name, link })
else
foreground = sanitize_color(foreground)
background = sanitize_color(background)
local foreground_rgb = sanitize_color(foreground)
local background_rgb = sanitize_color(background)
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].fg == foreground_rgb
and loaded_highlights[name].bg == background_rgb
and loaded_highlights[name].gui == gui
then
return -- color is already defined why are we doing this anyway ?
end
table.insert(command, name)
table.insert(command, 'guifg=' .. foreground)
table.insert(command, 'guibg=' .. background)
table.insert(command, 'guifg=' .. foreground_rgb)
table.insert(command, 'guibg=' .. background_rgb)
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))
-- Not setting color from xxxground_rgb to let possible user 256 number through
table.insert(command, 'ctermfg=' .. sanitize_color_for_cterm(foreground))
table.insert(command, 'ctermbg=' .. sanitize_color_for_cterm(background))
table.insert(command, 'cterm=' .. gui)
end
end
Expand Down

0 comments on commit 0a5a668

Please sign in to comment.