From 61665c1235292207a7622f91d4d48177971cffe5 Mon Sep 17 00:00:00 2001 From: jc <46619361+juancwu@users.noreply.github.com> Date: Thu, 6 Jun 2024 15:23:59 -0400 Subject: [PATCH] (nvim) add fn & cmd to toggle colorscheme --- nvim/lua/juancwu/config/init.lua | 7 +++++++ nvim/lua/juancwu/utils/colors.lua | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/nvim/lua/juancwu/config/init.lua b/nvim/lua/juancwu/config/init.lua index 78c5fe5..418eb7c 100644 --- a/nvim/lua/juancwu/config/init.lua +++ b/nvim/lua/juancwu/config/init.lua @@ -58,6 +58,13 @@ function M.setup(opts) vim.api.nvim_create_user_command("FormatEnable", function(args) vim.g.disable_autoformat = false end, { desc = "Enable Autoformat" }) + + -- create command to toggle colorscheme + vim.api.nvim_create_user_command( + "ToggleColors", + require("juancwu.utils.colors").toggle_colors, + { desc = "Toggle colorscheme" } + ) end setmetatable(M, { diff --git a/nvim/lua/juancwu/utils/colors.lua b/nvim/lua/juancwu/utils/colors.lua index 28e2346..7a05e2c 100644 --- a/nvim/lua/juancwu/utils/colors.lua +++ b/nvim/lua/juancwu/utils/colors.lua @@ -1,5 +1,7 @@ ---@class juancwu.utils.colors local M = {} +local light = "catppuccin-latte" +local dark = "catppuccin-mocha" ---@return boolean function M.is_daytime() @@ -9,13 +11,23 @@ end ---@return string function M.get_timebased_colorscheme() - local light = "catppuccin-latte" - local dark = "catppuccin-mocha" if M.is_daytime() then + vim.g.is_light_colors = true return light else + vim.g.is_light_colors = false return dark end end +function M.toggle_colors() + if vim.g.is_light_colors then + vim.g.is_light_colors = false + vim.cmd.colorscheme(dark) + else + vim.g.is_light_colors = true + vim.cmd.colorscheme(light) + end +end + return M