make metatable in config init.lua

This commit is contained in:
jc 2023-12-19 00:05:33 -05:00
commit d15112bc7d
No known key found for this signature in database
2 changed files with 23 additions and 7 deletions

View file

@ -12,5 +12,7 @@ end
vim.opt.rtp:prepend(lazypath)
require("juancwu.config").setup({
colorscheme = "tokyonight",
colorscheme = function()
require("tokyonight").load()
end,
})

View file

@ -1,4 +1,4 @@
---@class Config
---@class Config: ConfigOptions
local M = {}
---@class ConfigOptions
@ -34,16 +34,30 @@ function M.setup(opts)
-- try to load colorscheme
xpcall(function()
if type(options.colorscheme) == "function" then
options.colorscheme()
if type(M.colorscheme) == "function" then
M.colorscheme()
else
vim.cmd.colorscheme(options.colorscheme)
vim.cmd.colorscheme(M.colorscheme)
end
end, function(err)
local msg = "Failed to load colorscheme " .. options.colorscheme .. "\n\n" .. err
print(msg)
if type(M.colorscheme) == "string" then
local msg = "Failed to load colorscheme " .. M.colorscheme .. "\n\n" .. err
print(msg)
else
print("Failed to load colorscheme\n\n" .. err)
end
vim.cmd.colorscheme("rose-pine")
end)
end
setmetatable(M, {
__index = function(_, k)
if options == nil then
return vim.deepcopy(defaultOpts)[k]
end
return options[k]
end
})
return M