Skip to content

Commit

Permalink
feat: add variant="light|dark" option to override &background (#496)
Browse files Browse the repository at this point in the history
* add variant option

* add docs for variant option

* add variant option (change requests)
  • Loading branch information
williamhCode authored Sep 20, 2024
1 parent 9154484 commit 31bd21a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Run `:NvimWebDeviconsHiTest` to see all icons and their highlighting.

### Variants

Light or dark color variants of the icons depend on `&background`.
Light or dark color variants of the icons depend on `&background`.
The variant can also be set manually in `setup` with the `variant` option.

The variant is updated:
- on `OptionSet` event for `background`, or
Expand Down Expand Up @@ -75,6 +76,9 @@ require'nvim-web-devicons'.setup {
-- prevents cases when file doesn't have any extension but still gets some icon
-- because its name happened to match some extension (default to false)
strict = true;
-- set the light or dark variant manually, instead of relying on `background`
-- (default to nil)
variant = "light|dark";
-- same as `override` but specifically for overrides by filename
-- takes effect when `strict` is true
override_by_filename = {
Expand Down
21 changes: 16 additions & 5 deletions lua/nvim-web-devicons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ local global_opts = {
strict = false,
default = false,
color_icons = true,
variant = nil,
}

-- Set the current icons tables, depending on the 'background' option.
local function refresh_icons()
local theme
if vim.o.background == "light" then
if global_opts.variant == "light" then
theme = require "nvim-web-devicons.icons-light"
else
elseif global_opts.variant == "dark" then
theme = require "nvim-web-devicons.icons-default"
else
if vim.o.background == "light" then
theme = require "nvim-web-devicons.icons-light"
else
theme = require "nvim-web-devicons.icons-default"
end
end

icons_by_filename = theme.icons_by_filename
Expand Down Expand Up @@ -372,6 +379,13 @@ function M.setup(opts)

global_opts.color_icons = if_nil(user_icons.color_icons, global_opts.color_icons)

if user_icons.variant == "light" or user_icons.variant == "dark" then
global_opts.variant = user_icons.variant
end

-- Load the icons after setting variant option
refresh_icons()

if user_icons.override and user_icons.override.default_icon then
default_icon = user_icons.override.default_icon
end
Expand Down Expand Up @@ -579,9 +593,6 @@ function M.set_default_icon(icon, color, cterm_color)
set_up_highlight(default_icon)
end

-- Load the icons already, the loaded tables depend on the 'background' setting.
refresh_icons()

function M.refresh()
refresh_icons()
M.set_up_highlights(true)
Expand Down

0 comments on commit 31bd21a

Please sign in to comment.