From 6d65097d4559096ed4f3579f941318d28ee9de50 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Mon, 21 Mar 2022 17:44:49 -0700 Subject: [PATCH] highlights: resolve 256 color names from rtp?/rgb.txt --- lua/fzf-lua/providers/colorschemes.lua | 3 ++- lua/fzf-lua/utils.lua | 27 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lua/fzf-lua/providers/colorschemes.lua b/lua/fzf-lua/providers/colorschemes.lua index 11611e2..c1786e1 100644 --- a/lua/fzf-lua/providers/colorschemes.lua +++ b/lua/fzf-lua/providers/colorschemes.lua @@ -64,11 +64,12 @@ M.highlights = function(opts) local contents = function (cb) + local colormap = vim.api.nvim_get_color_map() local highlights = vim.fn.getcompletion('', 'highlight') local function add_entry(hl, co) -- translate the highlight using ansi escape sequences - local x = utils.ansi_from_hl(hl, hl) + local x = utils.ansi_from_hl(hl, hl, colormap) cb(x, function(err) if co then coroutine.resume(co) end if err then diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index 3fe93ff..87024ac 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -277,6 +277,8 @@ M.ansi_colors = { -- clear = "\x1b[0m", clear = "", bold = "", + italic = "", + underline = "", black = "", red = "", green = "", @@ -284,9 +286,9 @@ M.ansi_colors = { blue = "", magenta = "", cyan = "", + white = "", grey = "", dark_grey = "", - white = "", } M.add_ansi_code = function(name, escseq) @@ -311,7 +313,7 @@ local function synIDattr(hl, w) return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(hl)), w) end -function M.ansi_from_hl(hl, s) +function M.ansi_from_hl(hl, s, colormap) if vim.fn.hlexists(hl) == 1 then -- https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797#rgb-colors -- Set foreground color as RGB: 'ESC[38;2;{r};{g};{b}m' @@ -327,20 +329,33 @@ function M.ansi_from_hl(hl, s) ['strikethrough'] = { code = 9 }, } for w, p in pairs(what) do + local escseq = nil if p.rgb then local hexcol = synIDattr(hl, w) + if hexcol and not hexcol:match("^#") and colormap then + -- try to acquire the color from the map + -- some schemes don't capitalize first letter? + local col = colormap[hexcol:sub(1,1):upper()..hexcol:sub(2)] + if col then + -- format as 6 digit hex for hex2rgb() + hexcol = ("#%06x"):format(col) + end + end local r, g, b = hex2rgb(hexcol) if r and g and b then - local escseq = ('[%d;2;%d;%d;%dm'):format(p.code, r, g, b) - s = ("%s%s%s"):format(escseq, s, M.ansi_colors.clear) + escseq = ('[%d;2;%d;%d;%dm'):format(p.code, r, g, b) + -- elseif #hexcol>0 then + -- print("unresolved", hl, w, hexcol, colormap[synIDattr(hl, w)]) end else local value = synIDattr(hl, w) if value and tonumber(value)==1 then - local escseq = ('[%dm'):format(p.code) - s = ("%s%s%s"):format(escseq, s, M.ansi_colors.clear) + escseq = ('[%dm'):format(p.code) end end + if escseq then + s = ("%s%s%s"):format(escseq, s, M.ansi_colors.clear) + end end end return s