Skip to content

Commit

Permalink
feat(highlights): support cterm 8-bit colors; fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelot committed Jun 7, 2022
1 parent c5505f6 commit 60d92a6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
1 change: 1 addition & 0 deletions cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Each component may contain _any_ of the following fields:
- `bg`: The background color. Type: as above.
- `sp`: The underline/undercurl color, if any. Type: as above.
- Style fields supported by `synIDattrstyle()`: Example: `{ bold = true, underline = true }`
- `ctermfg`, `ctermbg`, `cterm` fields as described in `:h nvim_set_hl` for 8-bit colors.
- `force`: Control whether the parent's `hl` fields will override child's hl.
Type: `bool`.
- Description: `hl` controls the colors of what is printed by the component's
Expand Down
18 changes: 16 additions & 2 deletions lua/heirline/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ local function make_hl(hl_name, hl)
vim.api.nvim_set_hl(0, hl_name, hl)
end

local function name_hl(hl)
local function name_rgb_hl(hl)
local style = vim.tbl_filter(function(value)
return not vim.tbl_contains({ "bg", "fg", "sp" }, value)
return not vim.tbl_contains({ "bg", "fg", "sp", "ctermbg", "ctermfg", "cterm" }, value)
end, vim.tbl_keys(hl))
return "Stl"
.. (hl.fg and hl.fg:gsub("#", "") or "")
Expand All @@ -28,6 +28,18 @@ local function name_hl(hl)
.. (hl.sp and hl.sp:gsub("#", "") or "")
end

local function name_cterm_hl(hl)
local style = vim.tbl_filter(function(value)
return not vim.tbl_contains({ "bg", "fg", "sp", "ctermbg", "ctermfg", "cterm" }, value)
end, vim.tbl_keys(hl.cterm or hl))
return "Stl"
.. (hl.ctermfg or "")
.. "_"
.. (hl.ctermbg or "")
.. "_"
.. table.concat(style, "")
end

local function hex(val)
if type(val) == "number" then
return string.format("#%06x", val)
Expand All @@ -45,6 +57,8 @@ local function normalize_hl(hl)
return fixed_hl
end


local name_hl = vim.o.termguicolors and name_rgb_hl or name_cterm_hl
function M.eval_hl(hl)
if vim.tbl_isempty(hl) then
return "", ""
Expand Down
24 changes: 17 additions & 7 deletions lua/heirline/utils.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
local M = {}

local TERMGUICOLORS = vim.o.termguicolors

function M.get_highlight(name)
local hl = vim.api.nvim_get_hl_by_name(name, true)
hl.fg = hl.foreground
hl.bg = hl.background
hl.sp = hl.special
hl.foreground = nil
hl.backgroung = nil
hl.special = nil
local hl = vim.api.nvim_get_hl_by_name(name, TERMGUICOLORS)
if TERMGUICOLORS then
hl.fg = hl.foreground
hl.bg = hl.background
hl.sp = hl.special
hl.foreground = nil
hl.backgroung = nil
hl.special = nil
else
hl.ctermfg = hl.foreground
hl.ctermbg = hl.background
hl.foreground = nil
hl.backgroung = nil
hl.special = nil
end
return hl
end

Expand Down

0 comments on commit 60d92a6

Please sign in to comment.