Skip to content

Commit 92cbe2e

Browse files
committed
feat(util): allow adhoc toggling of live reloading
1 parent 1741511 commit 92cbe2e

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

lua/astrotheme/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function M.setup(opts)
4343
M.config = require("astrotheme.lib.config").user_config(opts)
4444
M.config.plugins = util.get_plugin_list(M.config)
4545

46-
util.live_reloading(M.config)
46+
if opts.dev then util.live_reloading() end
4747
end
4848

4949
return M

lua/astrotheme/lib/util.lua

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
---@class astrotheme.lib.util
99
local M = {}
1010

11+
M.live_reloading = false
12+
1113
--- Reload a given theme
1214
---@param opts AstroThemeOpts
1315
---@param theme string
@@ -61,7 +63,7 @@ end
6163
---@param module string
6264
---@return AstroThemeHighlights?
6365
function M.get_module_highlights(colors, opts, module)
64-
if opts.dev then package.loaded[module] = nil end
66+
if M.live_reloading then package.loaded[module] = nil end
6567
local file_avail, file = pcall(require, module)
6668
if type(file) == "function" then file = file(colors, opts.style) end
6769
if file_avail then
@@ -105,7 +107,7 @@ end
105107
---@return AstroThemePalette
106108
function M.set_palettes(opts)
107109
local palette_name = "astrotheme.palettes." .. opts.palette
108-
if opts.dev then package.loaded[palette_name] = nil end
110+
if M.live_reloading then package.loaded[palette_name] = nil end
109111
local palette = require(palette_name)
110112
palette = vim.tbl_deep_extend("force", palette, opts.palettes.global)
111113
palette = vim.tbl_deep_extend("force", palette, opts.palettes[opts.palette])
@@ -123,24 +125,22 @@ function M.set_highlights(highlights)
123125
end
124126

125127
--- Enable live reloading of AstroTheme for development
126-
---@param opts AstroThemeOpts
127-
function M.live_reloading(opts)
128-
if opts.dev then
129-
vim.api.nvim_create_augroup("AstroTheme", { clear = true })
130-
vim.api.nvim_create_autocmd("BufWritePost", {
131-
-- buffer = 0,
132-
pattern = "*.lua",
133-
group = "AstroTheme",
134-
callback = function()
135-
local theme = vim.g.colors_name
136-
if string.match(theme, "astro") then
137-
local command = ":colorscheme " .. theme
138-
vim.api.nvim_feedkeys(command, "t", true)
139-
vim.api.nvim_input "<CR>"
140-
end
141-
end,
142-
})
143-
end
128+
function M.live_reload()
129+
vim.api.nvim_create_autocmd("BufWritePost", {
130+
pattern = "*/lua/astrotheme/**.lua",
131+
group = vim.api.nvim_create_augroup("AstroThemeDev", { clear = true }),
132+
callback = function()
133+
local theme = vim.g.colors_name
134+
if string.match(theme, "astro") then vim.cmd.colorscheme(theme) end
135+
end,
136+
})
137+
M.live_reloading = true
138+
end
139+
140+
--- Disable live reloading of AstroTheme
141+
function M.live_reload_stop()
142+
vim.api.nvim_del_augroup_by_name "AstroThemeDev"
143+
M.live_reloading = false
144144
end
145145

146146
--- Set terminal colors based on the currently loaded colors

0 commit comments

Comments
 (0)