From 154ba7d41931566a62f83932f0792fa5def468ee Mon Sep 17 00:00:00 2001 From: siduck76 Date: Sun, 15 Aug 2021 00:44:55 +0530 Subject: [PATCH] add theme toggler (#245) --- lua/chadrc.lua | 9 ++++++--- lua/mappings.lua | 7 +++++++ lua/telescope/_extensions/themes.lua | 8 ++++++-- lua/theme.lua | 3 +++ lua/utils.lua | 18 ++++++++++++++++++ 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lua/chadrc.lua b/lua/chadrc.lua index 84d56909..0faa9b28 100644 --- a/lua/chadrc.lua +++ b/lua/chadrc.lua @@ -1,6 +1,8 @@ local M = { ui = { theme = "onedark", + fav_themes = {"onedark", "gruvchad"}, -- for theme toggle + theme_toggler = false, hidden_statusline = { -- these are filetypes, not pattern matched "NvimTree", @@ -24,9 +26,9 @@ local M = { smartindent = true, mapleader = " ", 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 = { -- UI nvim_bufferline = false, @@ -116,7 +118,8 @@ local M = { misc = { esc_Termmode = "jk", copywhole_file = "", - toggle_linenr = "n" + toggle_linenr = "n", + theme_toggle = "x" } } } diff --git a/lua/mappings.lua b/lua/mappings.lua index b0624efd..16940c14 100644 --- a/lua/mappings.lua +++ b/lua/mappings.lua @@ -160,4 +160,11 @@ if check_insertNav == true then map("i", m.prev_line, "", opt) 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)", opt) +end return M diff --git a/lua/telescope/_extensions/themes.lua b/lua/telescope/_extensions/themes.lua index 79ff4443..e0bf3d9e 100644 --- a/lua/telescope/_extensions/themes.lua +++ b/lua/telescope/_extensions/themes.lua @@ -110,6 +110,7 @@ M.theme_switcher = function(opts) end if reload_theme(final_theme) then + vim.g.current_nvchad_theme = final_theme if change then -- ask for confirmation to set as default theme 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 -- will be used in restoring nvchad theme var final_theme = current_theme + vim.g.current_nvchad_theme = final_theme end - end + end + -- open a buffer and close it to reload the statusline + vim.cmd("new|bwipeout") else final_theme = current_theme end @@ -130,7 +134,7 @@ M.theme_switcher = function(opts) -- launch the telescope picker picker:find() else - print("No themes found in " .. themes_folder) + print("No themes found in " .. vim.fn.stdpath("config") .. "/lua/themes") end end diff --git a/lua/theme.lua b/lua/theme.lua index 1e0d36b3..f1ecdaa0 100644 --- a/lua/theme.lua +++ b/lua/theme.lua @@ -1,6 +1,9 @@ local chad_theme = require("chadrc").ui.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") if present then diff --git a/lua/utils.lua b/lua/utils.lua index 46b23125..a54be638 100644 --- a/lua/utils.lua +++ b/lua/utils.lua @@ -184,4 +184,22 @@ M.reload_theme = function(theme_name) return true 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