repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
wezterm/bugabinga/workspace_switcher.lua
RawFile outline
--- stolen and adapted from https://github.com/MLFlexer/smart_workspace_switcher.wezterm/blob/main/plugin/init.lua
local wezterm = require 'wezterm'
local act = wezterm.action
local mux = wezterm.mux
local zoxide_path = 'zoxide'
local is_windows = string.find( wezterm.target_triple, 'windows' ) ~= nil
local function workspace_formatter( label )
return wezterm.format {
{ Text = ' : ' .. label, },
}
end
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
local function get_workspace_elements( choice_table )
local workspace_ids = {}
for _, workspace in ipairs( mux.get_workspace_names() ) do
table.insert( choice_table, {
id = workspace,
label = workspace_formatter( workspace ),
} )
workspace_ids[workspace] = true
end
return choice_table, workspace_ids
end
local function get_zoxide_elements( choice_table, workspace_ids )
local stdout = run_child_process( zoxide_path .. ' query -l ' )
for _, path in ipairs( wezterm.split_by_newlines( stdout ) ) do
local updated_path = string.gsub( path, wezterm.home_dir, '~' )
if not workspace_ids[updated_path] then
table.insert( choice_table, {
id = path,
label = updated_path,
} )
end
end
return choice_table
end
local function get_choices()
local choices = {}
local workspace_ids = get_workspace_elements( choices )
get_zoxide_elements( choices, workspace_ids )
return choices
end
local function zoxide_chosen( window, pane, path, label_path )
local default_prog = window:effective_config().default_prog
window:perform_action(
act.SwitchToWorkspace {
name = label_path,
spawn = {
args = default_prog,
label = 'Workspace: ' .. label_path,
cwd = path,
},
},
pane
)
-- increment zoxide path score
run_child_process( zoxide_path .. ' add ' .. path )
end
local function workspace_chosen( window, pane, workspace )
window:perform_action(
act.SwitchToWorkspace {
name = workspace,
},
pane
)
end
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
local function switch_workspace()
return wezterm.action_callback( function ( window, pane )
local choices = get_choices()
window:perform_action(
act.InputSelector {
action = wezterm.action_callback( function ( inner_window, inner_pane, id, label )
if id and label then
if workspace_exists( id ) then
-- workspace is choosen
workspace_chosen( inner_window, inner_pane, id )
else
-- path is choosen
zoxide_chosen( inner_window, inner_pane, id, label )
end
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
return {
switch_workspace = switch_workspace,
}