feat(util): added util function to get a fg/bg color from the active colorscheme

This commit is contained in:
Folke Lemaitre 2024-03-26 12:41:57 +01:00
parent 1d2cb7d8d1
commit c00e3aa6b1
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -139,14 +139,31 @@ function M.statuscolumn()
return table.concat(components, "")
end
---@return {fg?:string}?
function M.fg(name)
local color = M.color(name)
return color and { fg = color } or nil
end
---@param name string
---@param bg? boolean
---@return string?
function M.color(name, bg)
---@type {foreground?:number}?
---@diagnostic disable-next-line: deprecated
local hl = vim.api.nvim_get_hl and vim.api.nvim_get_hl(0, { name = name, link = false })
or vim.api.nvim_get_hl_by_name(name, true)
---@diagnostic disable-next-line: undefined-field
local fg = hl and (hl.fg or hl.foreground)
return fg and { fg = string.format("#%06x", fg) } or nil
---@type string?
local color = nil
if hl then
if bg then
color = hl.bg or hl.background
else
color = hl.fg or hl.foreground
end
end
return color and string.format("#%06x", color) or nil
end
M.skip_foldexpr = {} ---@type table<number,boolean>