-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-selector.lua
100 lines (86 loc) · 2.73 KB
/
config-selector.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
local wezterm = require("wezterm")
local sep = package.config:sub(1, 1)
local H = {}
local ConfigSelector = {}
ConfigSelector.new = function(opts)
local self = setmetatable({}, { __index = ConfigSelector })
self.title = opts.title or "Config Selector"
self.config_items = {}
local dir = wezterm.config_dir .. sep .. opts.subdir
for _, path in ipairs(wezterm.read_dir(dir)) do
self:register(H.path_to_module_name(path))
end
return self
end
ConfigSelector.register = function(self, module_name)
local module = require(module_name)
-- The return value from `init` can be one of the following:
--
-- 1. "item name"
-- 2. { label = "item name" }
-- 3. { label = "item name", value = "item value" }
-- 4. { "item name1", "item name2", "item name3" }
-- 5. { { label = "item name1" }, "item name2", { label = "item name3", value = "item value" }}
local result = module.init()
if H.is_array(result) then
for _, item in ipairs(result) do
item = H.normalize_item(item)
self.config_items[item.label] = { mod = module, value = item.value }
end
else
result = H.normalize_item(result)
self.config_items[result.label] = { mod = module, value = result.value }
end
end
ConfigSelector.select = function(self, config, item_name)
local opts = self.config_items[item_name]
if opts then
opts.mod.activate(config, item_name, opts.value)
else
error(string.format("'%s' not defined for %s", item_name, self.title))
end
end
ConfigSelector.selector_action = function(self)
return wezterm.action_callback(function(window, pane)
window:perform_action(
wezterm.action.InputSelector({
action = wezterm.action_callback(function(win, _, _, label)
if not label then
return -- user cancelled
end
local overrides = win:get_config_overrides() or {}
self:select(overrides, label)
win:set_config_overrides(overrides)
end),
title = self.title,
choices = H.items_to_choices(self.config_items),
fuzzy = true,
fuzzy_description = self.title .. ": ",
}),
pane
)
end)
end
H.path_to_module_name = function(path)
path = string.sub(path, #wezterm.config_dir + 2)
path = string.gsub(path, ".lua$", "")
path = string.gsub(path, sep, ".")
return path
end
H.items_to_choices = function(items)
local choices = {}
for item_name, _ in pairs(items) do
table.insert(choices, { label = item_name })
end
table.sort(choices, function(a, b)
return a.label < b.label
end)
return choices
end
H.is_array = function(item)
return type(item) == "table" and item[1] ~= nil
end
H.normalize_item = function(item)
return type(item) == "table" and item or { label = item }
end
return ConfigSelector