Skip to content

Commit 4739720

Browse files
committed
Implement :CrazywallSetConfig
Implement :CrazywallListConfigs Implement :CrazywallFollowRef Implement require"crazywall".add_config()
1 parent a8ced84 commit 4739720

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lua/crazywall/commands/crazywall.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
local PluginContext = require("lua.crazywall.context")
21
local Config = require("core.config")
32
local Context = require("core.context")
3+
local PluginContext = require("lua.crazywall.context")
44
local streams = require("core.streams")
55
local plugin_utils = require("lua.crazywall.utils")
66
local fold = require("core.fold")

lua/crazywall/init.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local plugin_validate = require("crazywall.validate")
2+
local validate = require("core.validate")
23
local default_config = require("core.defaults.config")
34
local PluginState = require("lua.crazywall.state")
45
local Config = require("core.config")
@@ -15,6 +16,60 @@ cmd_crazywall_dry(plugin_state)
1516
cmd_crazywall_quick(plugin_state)
1617
cmd_crazywall_follow_ref(plugin_state)
1718

19+
vim.api.nvim_create_user_command("CrazywallListConfigs", function()
20+
for name, _ in pairs(plugin_state.configs) do
21+
print(
22+
name
23+
.. (
24+
name == plugin_state.current_config_name and " {active}"
25+
or ""
26+
)
27+
)
28+
end
29+
print()
30+
end, {})
31+
32+
vim.api.nvim_create_user_command("CrazywallSetConfig", function(opts)
33+
local config_name = opts.fargs[1]
34+
if not plugin_state.configs[config_name] then
35+
return vim.api.nvim_err_writeln(
36+
"crazywall: Could not find config " .. config_name .. "."
37+
)
38+
end
39+
plugin_state.current_config_name = config_name
40+
end, {
41+
nargs = 1,
42+
complete = function(_, line)
43+
local args = vim.split(line, " ")
44+
local config_names = {}
45+
if #args ~= 2 then
46+
return
47+
end
48+
for name, _ in pairs(plugin_state) do
49+
table.insert(config_names, name)
50+
end
51+
return config_names
52+
end,
53+
})
54+
55+
M.add_config = function(name, config_table)
56+
local err = validate.types(
57+
'require"crazywall".add_config()',
58+
{ { name, "string", "name" } }
59+
)
60+
if err then
61+
error(err)
62+
end
63+
local config
64+
config, err = Config:new(config_table)
65+
if not config then
66+
error(
67+
"Error occurred when trying to add config " .. name .. ": \n" .. err
68+
)
69+
end
70+
plugin_state.configs[name] = config_table
71+
end
72+
1873
M.setup = function(opts)
1974
opts = opts or {}
2075
local keys = { "configs", "default_config_name", "follow_ref" }

0 commit comments

Comments
 (0)