feat: create config
module
Add support for overriding plugin default options with user preferences. Resolved user preferences are cached between switching colorschemes.
This commit is contained in:
parent
ee60efa02e
commit
14d268bc6b
40
lua/gruber-darker/config.lua
Normal file
40
lua/gruber-darker/config.lua
Normal file
@ -0,0 +1,40 @@
|
||||
---@class ConfigMgr
|
||||
---@field private resolved_opts Options|nil
|
||||
|
||||
---@class Options
|
||||
---@field message string
|
||||
|
||||
---@type ConfigMgr
|
||||
local M = {
|
||||
resolved_opts = nil
|
||||
}
|
||||
|
||||
---Get default GruberDarker colorscheme options
|
||||
---@return Options
|
||||
---@nodiscard
|
||||
function M.get_default_opts()
|
||||
return {
|
||||
message = "Big Hello",
|
||||
}
|
||||
end
|
||||
|
||||
---Get GruberDarker user preferences
|
||||
---@return Options|nil
|
||||
---@nodiscard
|
||||
function M.get_resolved_opts()
|
||||
return M.resolved_opts
|
||||
end
|
||||
|
||||
---Set GruberDarker colorscheme options
|
||||
---@param opts Options
|
||||
function M.set(opts)
|
||||
-- If user already called GruberDarker setup
|
||||
-- either with/without default options
|
||||
if M.resolved_opts ~= nil then
|
||||
return
|
||||
end
|
||||
|
||||
M.resolved_opts = vim.tbl_deep_extend("force", M.get_default_opts(), opts)
|
||||
end
|
||||
|
||||
return M
|
@ -1,16 +1,17 @@
|
||||
local highlights = require("gruber-darker.hl")
|
||||
local config = require("gruber-darker.config")
|
||||
|
||||
local M = {}
|
||||
|
||||
---Delete GruberDarker autocmds when the
|
||||
---theme changes to something else
|
||||
---@package
|
||||
function M.on_color_scheme()
|
||||
function M.on_colorscheme()
|
||||
vim.cmd([[autocmd! GruberDarker]])
|
||||
vim.cmd([[augroup! GruberDarker]])
|
||||
end
|
||||
|
||||
---Clear current highlights and set `g:colors_name`
|
||||
---Clear current highlights and set Neovim global `colors_name`
|
||||
local function prepare_env()
|
||||
if vim.g.colors_name then
|
||||
vim.cmd.hi("clear")
|
||||
@ -20,7 +21,18 @@ local function prepare_env()
|
||||
vim.g.colors_name = "GruberDarker"
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
---Change colorscheme to GruberDarker
|
||||
function M.colorsheme() end
|
||||
|
||||
---GruberDarker configuration bootstrapper
|
||||
---@param opts Options
|
||||
function M.setup(opts)
|
||||
config.set(opts or {})
|
||||
|
||||
opts = config.get_resolved_opts()
|
||||
|
||||
-- print("message: " .. opts.message)
|
||||
|
||||
prepare_env()
|
||||
-- set highlights here...
|
||||
for group, hl in pairs(highlights.groups) do
|
||||
@ -32,7 +44,7 @@ function M.setup()
|
||||
group = gruber_darker_group,
|
||||
pattern = "*",
|
||||
callback = function()
|
||||
require("gruber-darker").on_color_scheme()
|
||||
require("gruber-darker").on_colorscheme()
|
||||
end,
|
||||
})
|
||||
-- vim.api.nvim_create_autocmd("FileType", {
|
||||
@ -44,5 +56,9 @@ function M.setup()
|
||||
-- })
|
||||
end
|
||||
|
||||
return M
|
||||
M.setup({
|
||||
message = "Test message ensuring when a user calls "
|
||||
.. "`setup` with overrides, their preferences are kept.",
|
||||
})
|
||||
|
||||
return M
|
||||
|
Loading…
Reference in New Issue
Block a user