Skip to content

Commit

Permalink
Merge pull request #27 from MLFlexer/19-add-callbacks-when-changing-w…
Browse files Browse the repository at this point in the history
…orkspace-session

19 add callbacks when changing workspace session
  • Loading branch information
MLFlexer authored Jul 23, 2024
2 parents 6b01851 + d1fe2a8 commit a09c4a5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,34 @@ If you want your project switcher only to select projects from this list, but st
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.

```lua
wezterm.on("smart_workspace_switcher.workspace_chosen", function(window, path)
wezterm.on("smart_workspace_switcher.workspace_switcher.chosen", function(window, path)
local base_path = string.gsub(path, "(.*[/\\])(.*)", "%2")
window:set_right_status(wezterm.format({
{ Foreground = { Color = colors.colors.ansi[5] } },
{ Text = base_path .. " " },
}))
end)

wezterm.on("smart_workspace_switcher.workspace_switcher.created", function(window, path)
local base_path = string.gsub(path, "(.*[/\\])(.*)", "%2")
window:set_right_status(wezterm.format({
{ Foreground = { Color = colors.colors.ansi[5] } },
{ Text = base_path .. " " },
}))
end)

```

#### Callbacks when switching workspace
Use the `smart_workspace_switcher.workspace_chosen` event which is emitted when choosing the workspace to add a callback function.
#### Events
Use the events which are emitted when choosing the workspace to add a callback function. The following events are available:
* `smart_workspace_switcher.workspace_switcher.start` - when the fuzzy finder starts
* `smart_workspace_switcher.workspace_switcher.selected` - when a element is selected
* `smart_workspace_switcher.workspace_switcher.created` - after a new workspace is created and switched to upon selecting
* `smart_workspace_switcher.workspace_switcher.chosen` - after switching to a new workspace upon selecting

See example for use below:
```lua
wezterm.on("smart_workspace_switcher.workspace_chosen", function(window, path)
wezterm.on("smart_workspace_switcher.workspace_switcher.chosen", function(window, path)
wezterm.log_info("THIS IS EMITTED FROM THE CALLBACK")
end)
```
Expand Down
38 changes: 31 additions & 7 deletions plugin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ local zoxide_path = "zoxide"
---@return string
local workspace_formatter = function(label)
return wezterm.format({
{ Text = "󱂬: " .. label },
{ Text = "󱂬 : " .. label },
})
end

---@param cmd string
---@return string
local run_child_process = function(cmd)
local is_windows = string.find(wezterm.target_triple, "windows") ~= nil
local stdout
local success, stdout, stderr
if is_windows then
_, stdout, _ = wezterm.run_child_process({ "cmd", "/c", cmd })
success, stdout, stderr = wezterm.run_child_process({ "cmd", "/c", cmd })
else
_, stdout, _ = wezterm.run_child_process({ os.getenv("SHELL"), "-c", cmd })
success, stdout, stderr = wezterm.run_child_process({ os.getenv("SHELL"), "-c", cmd })
end

if not success then
wezterm.log_error("Child process '" .. cmd .. "' failed with stderr: '" .. stderr .. "'")
end
return stdout
end
Expand Down Expand Up @@ -56,13 +60,14 @@ end
---@return action_callback
local function workspace_switcher(extra_args)
return wezterm.action_callback(function(window, pane)
wezterm.emit("smart_workspace_switcher.workspace_switcher.start", window)
local workspaces = get_zoxide_workspaces(extra_args)

window:perform_action(
act.InputSelector({
action = wezterm.action_callback(function(inner_window, inner_pane, id, label)
if not id and not label then -- do nothing
else
if id and label then
wezterm.emit("smart_workspace_switcher.workspace_switcher.selected", window, id, label)
local fullPath = string.gsub(label, "^~", wezterm.home_dir)
if fullPath:sub(1, 1) == "/" or fullPath:sub(3, 3) == "\\" then
-- if path is choosen
Expand All @@ -76,6 +81,16 @@ local function workspace_switcher(extra_args)
}),
inner_pane
)
for _, mux_win in ipairs(wezterm.mux.all_windows()) do
if mux_win:get_workspace() == label then
wezterm.emit(
"smart_workspace_switcher.workspace_switcher.created",
mux_win,
id,
label
)
end
end
-- increment path score
run_child_process(zoxide_path .. " add " .. fullPath)
else
Expand All @@ -86,8 +101,17 @@ local function workspace_switcher(extra_args)
}),
inner_pane
)
for _, mux_win in ipairs(wezterm.mux.all_windows()) do
if mux_win:get_workspace() == label then
wezterm.emit(
"smart_workspace_switcher.workspace_switcher.chosen",
mux_win,
id,
label
)
end
end
end
wezterm.emit("smart_workspace_switcher.workspace_chosen", window, id)
end
end),
title = "Choose Workspace",
Expand Down

0 comments on commit a09c4a5

Please sign in to comment.