Skip to content

Commit

Permalink
feat(util): allow adhoc toggling of live reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
mehalter committed Jul 3, 2024
1 parent 1741511 commit 92cbe2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lua/astrotheme/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function M.setup(opts)
M.config = require("astrotheme.lib.config").user_config(opts)
M.config.plugins = util.get_plugin_list(M.config)

util.live_reloading(M.config)
if opts.dev then util.live_reloading() end
end

return M
40 changes: 20 additions & 20 deletions lua/astrotheme/lib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
---@class astrotheme.lib.util
local M = {}

M.live_reloading = false

--- Reload a given theme
---@param opts AstroThemeOpts
---@param theme string
Expand Down Expand Up @@ -61,7 +63,7 @@ end
---@param module string
---@return AstroThemeHighlights?
function M.get_module_highlights(colors, opts, module)
if opts.dev then package.loaded[module] = nil end
if M.live_reloading then package.loaded[module] = nil end
local file_avail, file = pcall(require, module)
if type(file) == "function" then file = file(colors, opts.style) end
if file_avail then
Expand Down Expand Up @@ -105,7 +107,7 @@ end
---@return AstroThemePalette
function M.set_palettes(opts)
local palette_name = "astrotheme.palettes." .. opts.palette
if opts.dev then package.loaded[palette_name] = nil end
if M.live_reloading then package.loaded[palette_name] = nil end
local palette = require(palette_name)
palette = vim.tbl_deep_extend("force", palette, opts.palettes.global)
palette = vim.tbl_deep_extend("force", palette, opts.palettes[opts.palette])
Expand All @@ -123,24 +125,22 @@ function M.set_highlights(highlights)
end

--- Enable live reloading of AstroTheme for development
---@param opts AstroThemeOpts
function M.live_reloading(opts)
if opts.dev then
vim.api.nvim_create_augroup("AstroTheme", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
-- buffer = 0,
pattern = "*.lua",
group = "AstroTheme",
callback = function()
local theme = vim.g.colors_name
if string.match(theme, "astro") then
local command = ":colorscheme " .. theme
vim.api.nvim_feedkeys(command, "t", true)
vim.api.nvim_input "<CR>"
end
end,
})
end
function M.live_reload()
vim.api.nvim_create_autocmd("BufWritePost", {
pattern = "*/lua/astrotheme/**.lua",
group = vim.api.nvim_create_augroup("AstroThemeDev", { clear = true }),
callback = function()
local theme = vim.g.colors_name
if string.match(theme, "astro") then vim.cmd.colorscheme(theme) end
end,
})
M.live_reloading = true
end

--- Disable live reloading of AstroTheme
function M.live_reload_stop()
vim.api.nvim_del_augroup_by_name "AstroThemeDev"
M.live_reloading = false
end

--- Set terminal colors based on the currently loaded colors
Expand Down

0 comments on commit 92cbe2e

Please sign in to comment.