Skip to content

Commit a09c4a5

Browse files
authored
Merge pull request #27 from MLFlexer/19-add-callbacks-when-changing-workspace-session
19 add callbacks when changing workspace session
2 parents 6b01851 + d1fe2a8 commit a09c4a5

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,34 @@ If you want your project switcher only to select projects from this list, but st
7878
Adding the path as a part of the right-status can be done with the `smart_workspace_switcher.workspace_chosen` event which is emitted when choosing the workspace.
7979

8080
```lua
81-
wezterm.on("smart_workspace_switcher.workspace_chosen", function(window, path)
81+
wezterm.on("smart_workspace_switcher.workspace_switcher.chosen", function(window, path)
8282
local base_path = string.gsub(path, "(.*[/\\])(.*)", "%2")
8383
window:set_right_status(wezterm.format({
8484
{ Foreground = { Color = colors.colors.ansi[5] } },
8585
{ Text = base_path .. " " },
8686
}))
8787
end)
88+
89+
wezterm.on("smart_workspace_switcher.workspace_switcher.created", function(window, path)
90+
local base_path = string.gsub(path, "(.*[/\\])(.*)", "%2")
91+
window:set_right_status(wezterm.format({
92+
{ Foreground = { Color = colors.colors.ansi[5] } },
93+
{ Text = base_path .. " " },
94+
}))
95+
end)
96+
8897
```
8998

90-
#### Callbacks when switching workspace
91-
Use the `smart_workspace_switcher.workspace_chosen` event which is emitted when choosing the workspace to add a callback function.
99+
#### Events
100+
Use the events which are emitted when choosing the workspace to add a callback function. The following events are available:
101+
* `smart_workspace_switcher.workspace_switcher.start` - when the fuzzy finder starts
102+
* `smart_workspace_switcher.workspace_switcher.selected` - when a element is selected
103+
* `smart_workspace_switcher.workspace_switcher.created` - after a new workspace is created and switched to upon selecting
104+
* `smart_workspace_switcher.workspace_switcher.chosen` - after switching to a new workspace upon selecting
105+
106+
See example for use below:
92107
```lua
93-
wezterm.on("smart_workspace_switcher.workspace_chosen", function(window, path)
108+
wezterm.on("smart_workspace_switcher.workspace_switcher.chosen", function(window, path)
94109
wezterm.log_info("THIS IS EMITTED FROM THE CALLBACK")
95110
end)
96111
```

plugin/init.lua

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@ local zoxide_path = "zoxide"
1010
---@return string
1111
local workspace_formatter = function(label)
1212
return wezterm.format({
13-
{ Text = "󱂬: " .. label },
13+
{ Text = "󱂬 : " .. label },
1414
})
1515
end
1616

1717
---@param cmd string
1818
---@return string
1919
local run_child_process = function(cmd)
2020
local is_windows = string.find(wezterm.target_triple, "windows") ~= nil
21-
local stdout
21+
local success, stdout, stderr
2222
if is_windows then
23-
_, stdout, _ = wezterm.run_child_process({ "cmd", "/c", cmd })
23+
success, stdout, stderr = wezterm.run_child_process({ "cmd", "/c", cmd })
2424
else
25-
_, stdout, _ = wezterm.run_child_process({ os.getenv("SHELL"), "-c", cmd })
25+
success, stdout, stderr = wezterm.run_child_process({ os.getenv("SHELL"), "-c", cmd })
26+
end
27+
28+
if not success then
29+
wezterm.log_error("Child process '" .. cmd .. "' failed with stderr: '" .. stderr .. "'")
2630
end
2731
return stdout
2832
end
@@ -56,13 +60,14 @@ end
5660
---@return action_callback
5761
local function workspace_switcher(extra_args)
5862
return wezterm.action_callback(function(window, pane)
63+
wezterm.emit("smart_workspace_switcher.workspace_switcher.start", window)
5964
local workspaces = get_zoxide_workspaces(extra_args)
6065

6166
window:perform_action(
6267
act.InputSelector({
6368
action = wezterm.action_callback(function(inner_window, inner_pane, id, label)
64-
if not id and not label then -- do nothing
65-
else
69+
if id and label then
70+
wezterm.emit("smart_workspace_switcher.workspace_switcher.selected", window, id, label)
6671
local fullPath = string.gsub(label, "^~", wezterm.home_dir)
6772
if fullPath:sub(1, 1) == "/" or fullPath:sub(3, 3) == "\\" then
6873
-- if path is choosen
@@ -76,6 +81,16 @@ local function workspace_switcher(extra_args)
7681
}),
7782
inner_pane
7883
)
84+
for _, mux_win in ipairs(wezterm.mux.all_windows()) do
85+
if mux_win:get_workspace() == label then
86+
wezterm.emit(
87+
"smart_workspace_switcher.workspace_switcher.created",
88+
mux_win,
89+
id,
90+
label
91+
)
92+
end
93+
end
7994
-- increment path score
8095
run_child_process(zoxide_path .. " add " .. fullPath)
8196
else
@@ -86,8 +101,17 @@ local function workspace_switcher(extra_args)
86101
}),
87102
inner_pane
88103
)
104+
for _, mux_win in ipairs(wezterm.mux.all_windows()) do
105+
if mux_win:get_workspace() == label then
106+
wezterm.emit(
107+
"smart_workspace_switcher.workspace_switcher.chosen",
108+
mux_win,
109+
id,
110+
label
111+
)
112+
end
113+
end
89114
end
90-
wezterm.emit("smart_workspace_switcher.workspace_chosen", window, id)
91115
end
92116
end),
93117
title = "Choose Workspace",

0 commit comments

Comments
 (0)