From 31bd21ac46b2b6039aa0b856ca02b018cf549ef7 Mon Sep 17 00:00:00 2001 From: William Hou <83525937+williamhCode@users.noreply.github.com> Date: Thu, 19 Sep 2024 22:00:42 -0400 Subject: [PATCH] feat: add variant="light|dark" option to override &background (#496) * add variant option * add docs for variant option * add variant option (change requests) --- README.md | 6 +++++- lua/nvim-web-devicons.lua | 21 ++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8014e3193..a542b2ad9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 = { diff --git a/lua/nvim-web-devicons.lua b/lua/nvim-web-devicons.lua index ec857188d..7c77ba6c8 100644 --- a/lua/nvim-web-devicons.lua +++ b/lua/nvim-web-devicons.lua @@ -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 @@ -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 @@ -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)