Skip to content

Commit cbfd74a

Browse files
committed
feat: add vim cmd to toggle between theme modes
1 parent d78641e commit cbfd74a

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,34 @@ require("cyberdream").setup({
136136
})
137137
```
138138

139+
## 🧑‍🍳 Recipes
140+
141+
Include these alongside the `setup` function to add additional functionality to the theme.
142+
143+
#### Map a key to toggle between light and dark mode
144+
145+
```lua
146+
-- Add a custom keybinding to toggle the colorscheme
147+
vim.api.nvim_set_keymap("n", "<leader>tt", ":CyberdreamToggleMode<CR>", { noremap = true, silent = true })
148+
```
149+
150+
#### Create an `autocmd` to hook into the toggle event and run custom code
151+
152+
```lua
153+
-- The event data property will contain a string with either "default" or "light" respectively
154+
vim.api.nvim_create_autocmd("User", {
155+
pattern = "CyberdreamToggleMode",
156+
callback = function(event)
157+
-- Your custom code here!
158+
-- For example, notify the user that the colorscheme has been toggled
159+
print("Switched to " .. event.data .. " mode!")
160+
end,
161+
})
162+
```
163+
164+
![image](https://github.com/scottmckendry/cyberdream.nvim/assets/39483124/c0188d60-d62b-4a15-965d-a19757c484e6)
165+
166+
139167
## 🤝 Contributing
140168

141169
Pull requests are welcome. If a plugin you use is not supported, please open an issue and I'll try to add support for it. If you have any suggestions or feedback, please open an issue.

doc/cyberdream.txt

+31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Table of Contents *cyberdream-table-of-contents*
77
- Installation |cyberdream-installation|
88
- Usage |cyberdream-usage|
99
- Configuring |cyberdream-configuring|
10+
- ‍ Recipes |cyberdream-‍-recipes|
1011
- Contributing |cyberdream-contributing|
1112
1. Links |cyberdream-links|
1213

@@ -126,6 +127,35 @@ their default values:
126127
<
127128

128129

130+
‍ RECIPES *cyberdream-‍-recipes*
131+
132+
Include these alongside the `setup` function to add additional functionality to
133+
the theme.
134+
135+
136+
MAP A KEY TO TOGGLE BETWEEN LIGHT AND DARK MODE
137+
138+
>lua
139+
-- Add a custom keybinding to toggle the colorscheme
140+
vim.api.nvim_set_keymap("n", "<leader>tt", ":CyberdreamToggleMode<CR>", { noremap = true, silent = true })
141+
<
142+
143+
144+
CREATE AN AUTOCMD TO HOOK INTO THE TOGGLE EVENT AND RUN CUSTOM CODE
145+
146+
>lua
147+
-- The event data property will contain a string with either "default" or "light" respectively
148+
vim.api.nvim_create_autocmd("User", {
149+
pattern = "CyberdreamToggleMode",
150+
callback = function(event)
151+
-- Your custom code here!
152+
-- For example, notify the user that the colorscheme has been toggled
153+
print("Switched to " .. event.data .. " mode!")
154+
end,
155+
})
156+
<
157+
158+
129159
CONTRIBUTING *cyberdream-contributing*
130160

131161
Pull requests are welcome. If a plugin you use is not supported, please open an
@@ -137,6 +167,7 @@ feedback, please open an issue.
137167

138168
1. *image*: https://github.com/scottmckendry/cyberdream.nvim/assets/39483124/55ad863e-11e6-4539-bf67-118ea328fb5b
139169
2. *image*: https://github.com/scottmckendry/cyberdream.nvim/assets/39483124/387a32f3-da38-4e96-b1e6-ea55591ec9ae
170+
3. *image*: https://github.com/scottmckendry/cyberdream.nvim/assets/39483124/c0188d60-d62b-4a15-965d-a19757c484e6
140171

141172
Generated by panvimdoc <https://github.com/kdheepak/panvimdoc>
142173

lua/cyberdream/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ M.options = {}
3232
---@param options Config|nil
3333
function M.setup(options)
3434
M.options = vim.tbl_deep_extend("force", {}, default_options, options or {})
35+
vim.g.cyberdream_opts = M.options
3536
end
3637

3738
M.setup()

lua/cyberdream/init.lua

+6
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ M.setup = config.setup
1212

1313
M.colorscheme = M.load
1414

15+
vim.api.nvim_create_user_command("CyberdreamToggleMode", function()
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 })
19+
end, {})
20+
1521
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)