Skip to content

Commit

Permalink
refactor(cache): add cache module
Browse files Browse the repository at this point in the history
Added a "cache.lua" module that's responsible for creating a cached hash
using the user configuration and a minimal colorscheme palette.
  • Loading branch information
RedsXDD committed Jul 27, 2024
1 parent 9e78795 commit 6cd3435
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 41 deletions.
31 changes: 0 additions & 31 deletions lua/neopywal/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,13 @@ local G = config.compiler
M.setup = config.setup
M.get_colors = palette.get_colors

local function gen_cache()
-- Get cached hash.
local cached_path = G.compile_path .. G.path_sep .. "cached"
local file = io.open(cached_path)
local cached = nil
if file then
cached = file:read()
file:close()
end

-- Get current hash.
local minimal_palette = palette.get(nil, true)
local git_path = debug.getinfo(1).source:sub(2, -22) .. ".git"
local git = vim.fn.getftime(git_path) -- 2x faster vim.loop.fs_stat
local hash = require("neopywal.lib.hashing").hash({ config.user_config, minimal_palette })
.. (git == -1 and git_path or git) -- no .git in /nix/store -> cache path
.. (vim.o.winblend == 0 and 1 or 0) -- :h winblend
.. (vim.o.pumblend == 0 and 1 or 0) -- :h pumblend

-- Recompile if hash changed.
if cached ~= hash then
compiler.compile()
file = io.open(cached_path, "wb")
if file then
file:write(hash)
file:close()
end
end
end

local lock = false -- Avoid g:colors_name reloading
local did_load = false
---@param theme_style? ThemeStyles
function M.load(theme_style)
if lock then return end
if not config.did_setup then config.setup() end
if did_load then require("neopywal.utils.reset").reset() end
gen_cache()

local bg = vim.o.background
local style_bg = (theme_style ~= "dark" and theme_style ~= "light") and bg or theme_style
Expand Down
39 changes: 39 additions & 0 deletions lua/neopywal/lib/cache.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local M = {}
local palette = require("neopywal.lib.palette")
local compiler = require("neopywal.lib.compiler")

---@param user_config NeopywalOptions?
function M.gen_cache(user_config, compiler_options)
user_config = user_config or {}
if not compiler_options then error("Neopywal(ERROR): Missing compiler options to gen_cache().") end

-- Get cached hash.
local cached_path = compiler_options.compile_path .. compiler_options.path_sep .. "cached"
local file = io.open(cached_path)
local cached = nil
if file then
cached = file:read()
file:close()
end

-- Get current hash.
local minimal_palette = palette.get(nil, true)
local git_path = debug.getinfo(1).source:sub(2, -27) .. ".git"
local git = vim.fn.getftime(git_path) -- 2x faster vim.loop.fs_stat
local hash = require("neopywal.lib.hashing").hash({ user_config, minimal_palette })
.. (git == -1 and git_path or git) -- no .git in /nix/store -> cache path
.. (vim.o.winblend == 0 and 1 or 0) -- :h winblend
.. (vim.o.pumblend == 0 and 1 or 0) -- :h pumblend

-- Recompile if hash changed.
if cached ~= hash then
compiler.compile()
file = io.open(cached_path, "wb")
if file then
file:write(hash)
file:close()
end
end
end

return M
19 changes: 9 additions & 10 deletions lua/neopywal/lib/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -330,27 +330,25 @@ local function check_nil_option(option, fallback_result)
end

M.did_setup = false
M.user_config = {}
---@param user_conf? NeopywalOptions
function M.setup(user_conf)
M.user_config = user_conf or {}
M.user_config.plugins = check_nil_option(M.user_config.plugins, {})
---@param user_config? NeopywalOptions
function M.setup(user_config)
user_config = user_config or {}
user_config.plugins = check_nil_option(user_config.plugins, {})

-- Handle plugin tables.
M.default_options.default_plugins =
check_nil_option(M.user_config.default_plugins, M.default_options.default_plugins)
M.default_options.default_plugins = check_nil_option(user_config.default_plugins, M.default_options.default_plugins)
M.default_options.plugins = disable_table(M.default_options.plugins, M.default_options.default_plugins)
M.default_options.plugins.mini = disable_table(M.default_options.plugins.mini, M.default_options.default_plugins)

-- Disable fileformats if treesitter is enabled (unless the user manually specifies otherwise).
M.default_options.default_fileformats = check_nil_option(
M.user_config.default_fileformats,
not check_nil_option(M.user_config.plugins.treesitter, M.default_options.plugins.treesitter)
user_config.default_fileformats,
not check_nil_option(user_config.plugins.treesitter, M.default_options.plugins.treesitter)
)
M.default_options.fileformats = disable_table(M.default_options.fileformats, M.default_options.default_fileformats)

-- Create the final configuration table.
M.options = vim.tbl_deep_extend("keep", M.user_config, M.default_options)
M.options = vim.tbl_deep_extend("keep", user_config, M.default_options)

-- Neovide doesn't play well with transparent background colors.
M.options.transparent_background = not vim.g.neovide and M.options.transparent_background or false
Expand All @@ -374,6 +372,7 @@ function M.setup(user_conf)
custom_colors = M.options.custom_colors,
})

require("neopywal.lib.cache").gen_cache(user_config, M.compiler)
M.did_setup = true
end

Expand Down

0 comments on commit 6cd3435

Please sign in to comment.