Skip to content

Commit 0a5a668

Browse files
feat: Allow cterm highlight number through (#1194)
Co-authored-by: Shadman <[email protected]>
1 parent 15d830d commit 0a5a668

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

lua/lualine/highlight.lua

+22-8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ local function sanitize_color(color)
7676
end
7777
end
7878

79+
---converts color_name type colors to cterm format and let cterm color pass through
80+
---@param color string|number
81+
---@return string
82+
local function sanitize_color_for_cterm(color)
83+
if type(color) == 'number' then
84+
if color > 255 then
85+
error("What's this it can't be higher then 255 and you've given " .. color)
86+
end
87+
return color
88+
end
89+
return modules.color_utils.rgb2cterm(sanitize_color(color))
90+
end
91+
7992
function M.get_lualine_hl(name)
8093
local hl = loaded_highlights[name]
8194
if hl and not hl.empty then
@@ -113,24 +126,25 @@ function M.highlight(name, foreground, background, gui, link)
113126
end
114127
vim.list_extend(command, { 'link', name, link })
115128
else
116-
foreground = sanitize_color(foreground)
117-
background = sanitize_color(background)
129+
local foreground_rgb = sanitize_color(foreground)
130+
local background_rgb = sanitize_color(background)
118131
gui = (gui ~= nil and gui ~= '') and gui or 'None'
119132
if
120133
loaded_highlights[name]
121-
and loaded_highlights[name].fg == foreground
122-
and loaded_highlights[name].bg == background
134+
and loaded_highlights[name].fg == foreground_rgb
135+
and loaded_highlights[name].bg == background_rgb
123136
and loaded_highlights[name].gui == gui
124137
then
125138
return -- color is already defined why are we doing this anyway ?
126139
end
127140
table.insert(command, name)
128-
table.insert(command, 'guifg=' .. foreground)
129-
table.insert(command, 'guibg=' .. background)
141+
table.insert(command, 'guifg=' .. foreground_rgb)
142+
table.insert(command, 'guibg=' .. background_rgb)
130143
table.insert(command, 'gui=' .. gui)
131144
if create_cterm_colors then
132-
table.insert(command, 'ctermfg=' .. modules.color_utils.rgb2cterm(foreground))
133-
table.insert(command, 'ctermbg=' .. modules.color_utils.rgb2cterm(background))
145+
-- Not setting color from xxxground_rgb to let possible user 256 number through
146+
table.insert(command, 'ctermfg=' .. sanitize_color_for_cterm(foreground))
147+
table.insert(command, 'ctermbg=' .. sanitize_color_for_cterm(background))
134148
table.insert(command, 'cterm=' .. gui)
135149
end
136150
end

0 commit comments

Comments
 (0)