repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
wezterm/bugabinga/workspace_switcher.lua
RawFile outline
- workspace_formatter line 9
- get_workspace_elements line 24
- add_path_element line 36
- get_zoxide_elements line 48
- get_workspace_dir_elements line 57
- get_choices line 68
- zoxide_chosen line 76
- workspace_chosen line 93
- spawn_workspace_window line 102
- zoxide_chosen_in_new_window line 122
- workspace_chosen_in_new_window line 128
- workspace_exists line 132
- choose_workspace line 141
- switch_workspace line 161
- switch_workspace_in_new_window line 171
--- 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 function workspace_formatter( label )
return wezterm.format {
{ Text = ' : ' .. label, },
}
end
local run_child_process = function ( args )
local success, stdout, stderr = wezterm.run_child_process( args )
if not success then
wezterm.log_error( "Child process '" .. table.concat( args, ' ' ) .. "' failed with stderr: '" .. stderr .. "'" )
end
return stdout or ''
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 add_path_element( choice_table, workspace_ids, path )
if not path or path == '' then return end
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,
} )
workspace_ids[updated_path] = true
end
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
add_path_element( choice_table, workspace_ids, path )
end
return choice_table
end
local function get_workspace_dir_elements( choice_table, workspace_ids )
local workspace_dir = wezterm.home_dir .. '/Workspace'
local stdout = run_child_process { 'find', workspace_dir, '-mindepth', '1', '-maxdepth', '1', '-type', 'd' }
local paths = wezterm.split_by_newlines( stdout )
table.sort( paths )
for _, path in ipairs( paths ) do
add_path_element( choice_table, workspace_ids, path )
end
return choice_table
end
local function get_choices()
local choices = {}
local _, workspace_ids = get_workspace_elements( choices )
get_zoxide_elements( choices, workspace_ids )
get_workspace_dir_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 spawn_workspace_window( _window, workspace, cwd )
-- `mux.spawn_window()` and `wezterm cli spawn --new-window` create mux
-- windows, which can appear in the workspace HUD without necessarily mapping
-- a new OS/WM window. For the workflow-window use-case, force a fresh GUI.
local args = { os.getenv 'WEZTERM_EXECUTABLE' or 'wezterm', 'start', '--always-new-process', '--workspace', workspace }
if cwd then
table.insert( args, '--cwd' )
table.insert( args, cwd )
end
if wezterm.background_child_process then
wezterm.background_child_process( args )
else
local ok, err = pcall( function () wezterm.run_child_process( args ) end )
if not ok then
wezterm.log_error( 'Failed to spawn workspace window: ' .. tostring( err ) )
end
end
end
local function zoxide_chosen_in_new_window( window, path, label_path )
spawn_workspace_window( window, label_path, path )
-- increment zoxide path score
run_child_process { zoxide_path, 'add', path }
end
local function workspace_chosen_in_new_window( window, workspace )
spawn_workspace_window( window, workspace )
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 choose_workspace( title, fuzzy_description, handler )
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 handler( inner_window, inner_pane, id, label ) end
end ),
title = title,
description = 'Select a workspace and press Enter = accept, Esc = cancel, / = filter',
fuzzy_description = fuzzy_description,
choices = choices,
fuzzy = true,
},
pane
)
end )
end
local function switch_workspace()
return choose_workspace( 'Choose Workspace', 'Workspace to switch: ', function ( window, pane, id, label )
if workspace_exists( id ) then
workspace_chosen( window, pane, id )
else
zoxide_chosen( window, pane, id, label )
end
end )
end
local function switch_workspace_in_new_window()
return choose_workspace( 'Choose Workspace in New Window', 'Workspace for new window: ', function ( window, pane, id, label )
if workspace_exists( id ) then
workspace_chosen_in_new_window( window, id )
else
zoxide_chosen_in_new_window( window, id, label )
end
end )
end
return {
switch_workspace = switch_workspace,
switch_workspace_in_new_window = switch_workspace_in_new_window,
}