Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b7ac7ad

Browse files
committedMay 21, 2024·
refactor
1 parent 235bffd commit b7ac7ad

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed
 

‎lua/cyberdream/init.lua

+3-19
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,9 @@ M.setup = config.setup
1313
M.colorscheme = M.load
1414

1515
vim.api.nvim_create_user_command("CyberdreamToggleMode", function()
16-
local opts = vim.g.cyberdream_opts
17-
opts.theme.variant = opts.theme.variant == "default" and "light" or "default"
18-
M.setup(opts)
19-
vim.cmd("colorscheme cyberdream")
20-
21-
-- Trigger event
22-
vim.api.nvim_exec_autocmds("User", { pattern = "CyberdreamToggleMode", data = opts.theme.variant })
23-
24-
-- Check if lualine is loaded before setting the theme
25-
if package.loaded["lualine"] == nil then
26-
return
27-
end
28-
29-
local lualine_opts = require("lualine").get_config()
30-
local lualine_theme = opts.theme.variant == "default" and require("lualine.themes.cyberdream")
31-
or require("lualine.themes.cyberdream-light")
32-
33-
lualine_opts.options.theme = lualine_theme
34-
require("lualine").setup(lualine_opts)
16+
local new_variant = util.toggle_theme_variant()
17+
util.toggle_lualine_theme(new_variant)
18+
vim.api.nvim_exec_autocmds("User", { pattern = "CyberdreamToggleMode", data = new_variant })
3519
end, {})
3620

3721
return M

‎lua/cyberdream/util.lua

+45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local ts = require("cyberdream.treesitter")
2+
local config = require("cyberdream.config")
23
local M = {}
34

45
--- Sets the highlight group to the given table of colors.
@@ -98,4 +99,48 @@ function M.parse_extra_template(template, t)
9899
return template
99100
end
100101

102+
--- Override options with a new table of values.
103+
--- @param new_opts Config
104+
--- @return Config
105+
function M.set_options(new_opts)
106+
local opts = vim.g.cyberdream_opts
107+
vim.g.cyberdream_opts = vim.tbl_deep_extend("force", opts, new_opts)
108+
109+
return vim.g.cyberdream_opts
110+
end
111+
112+
--- Apply options to the colorscheme.
113+
--- @param opts Config
114+
function M.apply_options(opts)
115+
-- Update the colorscheme
116+
config.setup(opts)
117+
vim.cmd("colorscheme cyberdream")
118+
end
119+
120+
--- Toggle the theme variant between "default" and "light".
121+
--- @return string new variant
122+
function M.toggle_theme_variant()
123+
local opts = vim.g.cyberdream_opts
124+
opts.theme.variant = opts.theme.variant == "default" and "light" or "default"
125+
M.set_options(opts)
126+
M.apply_options(opts)
127+
128+
return opts.theme.variant
129+
end
130+
131+
--- Toggle theme for lualine
132+
--- @param new_variant string "default" | "light"
133+
function M.toggle_lualine_theme(new_variant)
134+
if package.loaded["lualine"] == nil then
135+
return
136+
end
137+
138+
local lualine_opts = require("lualine").get_config()
139+
local lualine_theme = new_variant == "default" and require("lualine.themes.cyberdream")
140+
or require("lualine.themes.cyberdream-light")
141+
142+
lualine_opts.options.theme = lualine_theme
143+
require("lualine").setup(lualine_opts)
144+
end
145+
101146
return M

0 commit comments

Comments
 (0)
Please sign in to comment.