add theme toggler (#245)

This commit is contained in:
siduck76 2021-08-15 00:44:55 +05:30
parent ea0a221230
commit 154ba7d419
5 changed files with 40 additions and 5 deletions

View file

@ -1,6 +1,8 @@
local M = { local M = {
ui = { ui = {
theme = "onedark", theme = "onedark",
fav_themes = {"onedark", "gruvchad"}, -- for theme toggle
theme_toggler = false,
hidden_statusline = { hidden_statusline = {
-- these are filetypes, not pattern matched -- these are filetypes, not pattern matched
"NvimTree", "NvimTree",
@ -24,9 +26,9 @@ local M = {
smartindent = true, smartindent = true,
mapleader = " ", mapleader = " ",
autosave = false, autosave = false,
enable_insertNav = true -- navigation within insertmode enable_insertNav = true -- navigation in insertmode
}, },
-- enable / disable plugins (true for disable) -- enable and disable plugins (true for disable)
plugin_status = { plugin_status = {
-- UI -- UI
nvim_bufferline = false, nvim_bufferline = false,
@ -116,7 +118,8 @@ local M = {
misc = { misc = {
esc_Termmode = "jk", esc_Termmode = "jk",
copywhole_file = "<C-a>", copywhole_file = "<C-a>",
toggle_linenr = "<leader>n" toggle_linenr = "<leader>n",
theme_toggle = "<leader>x"
} }
} }
} }

View file

@ -160,4 +160,11 @@ if check_insertNav == true then
map("i", m.prev_line, "<Down>", opt) map("i", m.prev_line, "<Down>", opt)
end end
local theme_toggler = require("chadrc").ui.theme_toggler
if theme_toggler == true then
local m = user_map.misc.theme_toggle
map("n", m, ":lua require('utils').toggle_theme(require('chadrc').ui.fav_themes)<CR>", opt)
end
return M return M

View file

@ -110,6 +110,7 @@ M.theme_switcher = function(opts)
end end
if reload_theme(final_theme) then if reload_theme(final_theme) then
vim.g.current_nvchad_theme = final_theme
if change then if change then
-- ask for confirmation to set as default theme -- ask for confirmation to set as default theme
local ans = string.lower(vim.fn.input("Set " .. new_theme .. " as default theme ? [y/N] ")) == "y" local ans = string.lower(vim.fn.input("Set " .. new_theme .. " as default theme ? [y/N] ")) == "y"
@ -119,8 +120,11 @@ M.theme_switcher = function(opts)
else else
-- will be used in restoring nvchad theme var -- will be used in restoring nvchad theme var
final_theme = current_theme final_theme = current_theme
vim.g.current_nvchad_theme = final_theme
end end
end end
-- open a buffer and close it to reload the statusline
vim.cmd("new|bwipeout")
else else
final_theme = current_theme final_theme = current_theme
end end
@ -130,7 +134,7 @@ M.theme_switcher = function(opts)
-- launch the telescope picker -- launch the telescope picker
picker:find() picker:find()
else else
print("No themes found in " .. themes_folder) print("No themes found in " .. vim.fn.stdpath("config") .. "/lua/themes")
end end
end end

View file

@ -1,6 +1,9 @@
local chad_theme = require("chadrc").ui.theme local chad_theme = require("chadrc").ui.theme
vim.g.nvchad_theme = chad_theme vim.g.nvchad_theme = chad_theme
-- this stores the current set theme, if later theme switcher is used but not set to default
vim.g.current_nvchad_theme = chad_theme
local present, base16 = pcall(require, "base16") local present, base16 = pcall(require, "base16")
if present then if present then

View file

@ -184,4 +184,22 @@ M.reload_theme = function(theme_name)
return true return true
end end
-- toggle between 2 themes
-- argument should be a table with 2 theme names
M.toggle_theme = function(themes)
local current_theme = vim.g.current_nvchad_theme or vim.g.nvchad_theme
for _, name in ipairs(themes) do
if name ~= current_theme then
if require("utils").reload_theme(name) then
-- open a buffer and close it to reload the statusline
vim.cmd("new|bwipeout")
vim.g.current_nvchad_theme = name
if M.change_theme(vim.g.nvchad_theme, name) then
vim.g.nvchad_theme = name
end
end
end
end
end
return M return M