Skip to content

Commit

Permalink
feat: allow custom highlights and colors per theme #67
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehri01 committed Jan 27, 2024
1 parent c221939 commit a7e293a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,29 @@ require("onenord").setup({
})
```

If you use the `light` and `dark` keys, the override will be specific to those themes, otherwise they apply to both:

```lua
local colors = require("onenord.colors").load()

require("onenord").setup({
custom_highlights = {
light = {
["@constructor"] = { fg = colors.dark_blue }, -- only applies in light theme
},
},
custom_colors = {
blue = "#0000ff", -- applies in both themes
light = {
red = "#000000", -- only applies in light theme
},
dark = {
red = "#ffffff", -- only applies in dark theme
},
},
})
```

You can also use the OneNord color palette for other plugins using `local colors = require("onenord.colors").load()`!

## Integrations
Expand Down
22 changes: 22 additions & 0 deletions doc/onenord.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ Here is an example of overwriting the default highlight groups and colors:
})
<

If you use the `light` and `dark` keys, the override will be specific to those themes, otherwise they apply to both:
>lua
local colors = require("onenord.colors").load()

require("onenord").setup({
custom_highlights = {
light = {
["@constructor"] = { fg = colors.dark_blue }, -- only applies in light theme
},
},
custom_colors = {
blue = "#0000ff", -- applies in both themes
light = {
red = "#000000", -- only applies in light theme
},
dark = {
red = "#ffffff", -- only applies in dark theme
},
},
})
<

You can also use the onenord color palette for other plugins using `local colors = require("onenord.colors").load()`!

================================================================================
Expand Down
13 changes: 10 additions & 3 deletions lua/onenord/colors/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
local light_colors = require("onenord.colors.onenordlight")
local dark_colors = require("onenord.colors.onenord")
local util = require("onenord.util")

local function load()
local function get_theme()
local theme = require("onenord.config").options.theme

-- if style is set, it takes priority
Expand All @@ -10,8 +11,14 @@ local function load()
theme = vim.o.background
end

return theme
end

local function load()
local theme = get_theme()

local base_colors = theme == "light" and light_colors or dark_colors
return vim.tbl_deep_extend("force", base_colors, require("onenord.config").options.custom_colors)
return util.deep_extend_by_theme(base_colors, require("onenord.config").options.custom_colors)
end

return { load = load }
return { get_theme = get_theme, load = load }
24 changes: 21 additions & 3 deletions lua/onenord/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ function util.highlight(group, colors)
end
end

function util.deep_extend_by_theme(base, custom)
local color_theme = require("onenord.colors").get_theme()

local dark_specific = custom["dark"]
local light_specific = custom["light"]
custom["light"] = nil
custom["dark"] = nil

base = vim.tbl_deep_extend("force", base, custom)
if color_theme == "light" and light_specific then
base = vim.tbl_deep_extend("force", base, light_specific)
elseif color_theme == "dark" and dark_specific then
base = vim.tbl_deep_extend("force", base, dark_specific)
end

custom["light"] = light_specific
custom["dark"] = dark_specific
return base
end

-- Load the theme
function util.load(colors, exec_autocmd)
local config = require("onenord.config").options
Expand All @@ -34,10 +54,8 @@ function util.load(colors, exec_autocmd)
vim.g.colors_name = "onenord"

-- Load highlights
colors = vim.tbl_deep_extend("force", colors, config.custom_colors)
local base_highlights = theme.highlights(colors, config)

local highlights = vim.tbl_deep_extend("force", base_highlights, config.custom_highlights)
local highlights = util.deep_extend_by_theme(base_highlights, config.custom_highlights)

for group, color in pairs(highlights) do
util.highlight(group, color)
Expand Down

0 comments on commit a7e293a

Please sign in to comment.