This repository was archived by the owner on May 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinit.lua
More file actions
296 lines (262 loc) · 7.88 KB
/
Copy pathinit.lua
File metadata and controls
296 lines (262 loc) · 7.88 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
local wezterm = require("wezterm")
local act = wezterm.action
local mux = wezterm.mux
local is_windows = string.find(wezterm.target_triple, "windows") ~= nil
---@alias action_callback any
---@alias MuxWindow any
---@alias Pane any
---@alias workspace_ids table<string, boolean>
---@alias choice_opts {extra_args?: string, workspace_ids?: workspace_ids}
---@alias InputSelector_choices { id: string, label: string }[]
---@class public_module
---@field zoxide_path string
---@field choices {get_zoxide_elements: (fun(choices: InputSelector_choices, opts: choice_opts?): InputSelector_choices), get_workspace_elements: (fun(choices: InputSelector_choices): (InputSelector_choices, workspace_ids))}
---@field workspace_formatter fun(label: string): string
local pub = {
zoxide_path = "zoxide",
choices = {},
workspace_formatter = function(label)
return wezterm.format({
{ Text = " : " .. label },
})
end,
}
local cached_zoxide_choices = nil
local cache_timestamp = 0
function pub.invalidate_cache()
cached_zoxide_choices = nil
cache_timestamp = 0
end
wezterm.on("smart_workspace_switcher.workspace_switcher.invalidate_cache", function()
pub.invalidate_cache()
end)
---@param cmd string
---@return string
local run_child_process = function(cmd)
local process_args = { os.getenv("SHELL"), "-c", cmd }
if is_windows then
process_args = { "cmd", "/c", cmd }
end
local success, stdout, stderr = wezterm.run_child_process(process_args)
if not success then
wezterm.log_error("Child process '" .. cmd .. "' failed with stderr: '" .. stderr .. "'")
end
return stdout
end
---@param choice_table InputSelector_choices
---@return InputSelector_choices, workspace_ids
function pub.choices.get_workspace_elements(choice_table)
local workspace_ids = {}
for _, workspace in ipairs(mux.get_workspace_names()) do
table.insert(choice_table, {
id = workspace,
label = pub.workspace_formatter(workspace),
})
workspace_ids[workspace] = true
end
return choice_table, workspace_ids
end
---@param choice_table InputSelector_choices
---@param opts? choice_opts
---@return InputSelector_choices
function pub.choices.get_zoxide_elements(choice_table, opts)
if opts == nil then
opts = { extra_args = "", workspace_ids = {} }
end
local stdout = run_child_process(pub.zoxide_path .. " query -l " .. (opts.extra_args or ""))
for _, path in ipairs(wezterm.split_by_newlines(stdout)) do
local updated_path = string.gsub(path, wezterm.home_dir, "~")
if not opts.workspace_ids[updated_path] then
table.insert(choice_table, {
id = path,
label = updated_path,
})
end
end
return choice_table
end
-- Modify the get_choices function to include caching
---Returns choices for the InputSelector
---@param opts? choice_opts
---@param cache_ttl? number
---@return InputSelector_choices
function pub.get_choices(opts, cache_ttl)
if opts == nil then
opts = { extra_args = "" }
end
-- Default cache_ttl to 0 (no cache)
cache_ttl = cache_ttl or 0
---@type InputSelector_choices
local choices = {}
choices, opts.workspace_ids = pub.choices.get_workspace_elements(choices)
-- Check if zoxide cache is valid
local current_time = os.time()
if cache_ttl > 0 and cached_zoxide_choices and (current_time - cache_timestamp) < cache_ttl then
for _, choice in ipairs(cached_zoxide_choices) do
if not opts.workspace_ids[choice.label] then
table.insert(choices, choice)
end
end
else
local zoxide_choices = {}
local stdout = run_child_process(pub.zoxide_path .. " query -l " .. (opts.extra_args or ""))
for _, path in ipairs(wezterm.split_by_newlines(stdout)) do
local updated_path = string.gsub(path, wezterm.home_dir, "~")
table.insert(zoxide_choices, {
id = path,
label = updated_path,
})
if not opts.workspace_ids[updated_path] then
table.insert(choices, {
id = path,
label = updated_path,
})
end
end
if cache_ttl > 0 then
cached_zoxide_choices = zoxide_choices
cache_timestamp = current_time
end
end
return choices
end
---@param workspace string
---@return MuxWindow
local function get_current_mux_window(workspace)
for _, mux_win in ipairs(mux.all_windows()) do
if mux_win:get_workspace() == workspace then
return mux_win
end
end
error("Could not find a workspace with the name: " .. workspace)
end
---Check if the workspace exists
---@param workspace string
---@return boolean
local function workspace_exists(workspace)
for _, workspace_name in ipairs(mux.get_workspace_names()) do
if workspace == workspace_name then
return true
end
end
return false
end
---InputSelector callback when zoxide supplied element is chosen
---@param window MuxWindow
---@param pane Pane
---@param path string
---@param label_path string
local function zoxide_chosen(window, pane, path, label_path)
window:perform_action(
act.SwitchToWorkspace({
name = label_path,
spawn = {
label = "Workspace: " .. label_path,
cwd = path,
},
}),
pane
)
wezterm.emit(
"smart_workspace_switcher.workspace_switcher.created",
get_current_mux_window(label_path),
path,
label_path
)
-- increment zoxide path score
run_child_process(pub.zoxide_path .. " add " .. path)
end
---InputSelector callback when workspace element is chosen
---@param window MuxWindow
---@param pane Pane
---@param workspace string
---@param label_workspace string
local function workspace_chosen(window, pane, workspace, label_workspace)
window:perform_action(
act.SwitchToWorkspace({
name = workspace,
}),
pane
)
wezterm.emit(
"smart_workspace_switcher.workspace_switcher.chosen",
get_current_mux_window(workspace),
workspace,
label_workspace
)
end
-- Modify the switch_workspace function to accept cache_ttl
---@param opts? choice_opts
---@param cache_ttl? number
---@return action_callback
function pub.switch_workspace(opts, cache_ttl)
return wezterm.action_callback(function(window, pane)
wezterm.emit("smart_workspace_switcher.workspace_switcher.start", window, pane)
local choices = pub.get_choices(opts, cache_ttl)
window:perform_action(
act.InputSelector({
action = wezterm.action_callback(function(inner_window, inner_pane, id, label)
if id and label then
wezterm.emit("smart_workspace_switcher.workspace_switcher.selected", window, id, label)
if workspace_exists(id) then
-- workspace is chosen
workspace_chosen(inner_window, inner_pane, id, label)
else
-- path is chosen
zoxide_chosen(inner_window, inner_pane, id, label)
end
else
wezterm.emit("smart_workspace_switcher.workspace_switcher.canceled", window, pane)
end
end),
title = "Choose Workspace",
description = "Select a workspace and press Enter = accept, Esc = cancel, / = filter",
fuzzy_description = "Workspace to switch: ",
choices = choices,
fuzzy = true,
}),
pane
)
end)
end
---sets default keybind to ALT-s
---@param config table
function pub.apply_to_config(config)
if config == nil then
config = {}
end
if config.keys == nil then
config.keys = {}
end
table.insert(config.keys, {
key = "s",
mods = "LEADER",
action = pub.switch_workspace(),
})
table.insert(config.keys, {
key = "S",
mods = "LEADER",
action = pub.switch_to_prev_workspace(),
})
end
function pub.switch_to_prev_workspace()
return wezterm.action_callback(function(window, pane)
local current_workspace = window:active_workspace()
local previous_workspace = wezterm.GLOBAL.previous_workspace
if current_workspace == previous_workspace or previous_workspace == nil then
return
end
wezterm.GLOBAL.previous_workspace = current_workspace
window:perform_action(
act.SwitchToWorkspace({
name = previous_workspace,
}),
pane
)
wezterm.emit("smart_workspace_switcher.workspace_switcher.switched_to_prev", window, pane, previous_workspace)
end)
end
wezterm.on("smart_workspace_switcher.workspace_switcher.selected", function(window, _, _)
wezterm.GLOBAL.previous_workspace = window:active_workspace()
end)
return pub