repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

update wezterm workspace switching

13ae98aa631701e153f443afb152f0b1a92fe5bb

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

wezterm/bugabinga/key_binds.lua

Modified
191 191 key = 'z',
192 192 mods = 'LEADER',
193 + action = workspace_switcher.switch_workspace_in_new_window(),
194 + },
196 + key = 'Z',
197 + mods = 'LEADER|SHIFT',
193 198 action = workspace_switcher.switch_workspace(),

wezterm/bugabinga/status.lua

Modified
2 2 local wez = require 'wezterm'
3 3 local bat = require 'bugabinga.battery'
5 5 local DELIMITER = ' · '
6 -local palette = require 'bugabinga.nugu.palette_dark'.palette
6 +local nugu_dark = require 'bugabinga.nugu.palette_dark'.palette
7 +local nugu_light = require 'bugabinga.nugu.palette_light'.palette
8 +local palette = nugu_dark
7 9 local icon_names = require 'bugabinga.icons'
9 11 local animation_tick = 0
13 +local use_palette_for_scheme = function ( scheme )
14 + palette = scheme == 'nugu-light' and nugu_light or nugu_dark
15 +end
17 +local use_palette_for_window = function ( window )
18 + local ok, config = pcall( function () return window:effective_config() end )
19 + if ok and config then use_palette_for_scheme( config.color_scheme ) end
20 +end
11 22 local zoomed = function ( pane )
12 23 local tab = pane:tab()
13 24 local any_is_zoomed = false
417 428 wez.on( 'format-tab-title', function ( tab, tabs, panes, config, hover, max_width )
429 + if config then use_palette_for_scheme( config.color_scheme ) end
418 430 local title, icon, program = tab_title( tab )
419 431 local pi_badge, pi_badge_color = pi_state_badge( (tab.active_pane.user_vars or {}).PI_STATE )
420 432 local active_progress_badge, active_progress_color = progress_badge( tab.active_pane.progress )
471 483 -- Show which key table is active in the status area
472 484 wez.on( 'update-status', function ( window, pane )
485 + use_palette_for_window( window )
473 486 animation_tick = animation_tick + 1
474 487 local key_table = window:active_key_table()
475 488 local current_workspace = window:active_workspace()

wezterm/bugabinga/workspace_switcher.lua

Modified
39 39 return choice_table, workspace_ids
42 +local function add_path_element( choice_table, workspace_ids, path )
43 + if not path or path == '' then return end
44 + local updated_path = string.gsub( path, wezterm.home_dir, '~' )
45 + if not workspace_ids[updated_path] then
46 + table.insert( choice_table, {
47 + id = path,
48 + label = updated_path,
49 + } )
50 + workspace_ids[updated_path] = true
51 + end
52 +end
42 54 local function get_zoxide_elements( choice_table, workspace_ids )
43 55 local stdout = run_child_process( zoxide_path .. ' query -l ' )
45 57 for _, path in ipairs( wezterm.split_by_newlines( stdout ) ) do
46 - local updated_path = string.gsub( path, wezterm.home_dir, '~' )
47 - if not workspace_ids[updated_path] then
48 - table.insert( choice_table, {
49 - id = path,
50 - label = updated_path,
51 - } )
52 - end
58 + add_path_element( choice_table, workspace_ids, path )
59 + end
60 + return choice_table
61 +end
63 +local function get_workspace_dir_elements( choice_table, workspace_ids )
64 + local workspace_dir = wezterm.home_dir .. '/Workspace'
65 + local stdout = run_child_process( 'find ' .. workspace_dir .. ' -mindepth 1 -maxdepth 1 -type d | sort' )
66 + for _, path in ipairs( wezterm.split_by_newlines( stdout ) ) do
67 + add_path_element( choice_table, workspace_ids, path )
54 69 return choice_table
58 73 local choices = {}
59 74 local _, workspace_ids = get_workspace_elements( choices )
60 75 get_zoxide_elements( choices, workspace_ids )
76 + get_workspace_dir_elements( choices, workspace_ids )
61 77 return choices
106 +local function spawn_workspace_window( _window, workspace, cwd )
107 + -- `mux.spawn_window()` and `wezterm cli spawn --new-window` create mux
108 + -- windows, which can appear in the workspace HUD without necessarily mapping
109 + -- a new OS/WM window. For the workflow-window use-case, force a fresh GUI.
110 + local args = { 'wezterm', 'start', '--always-new-process', '--workspace', workspace }
111 + if cwd then
112 + table.insert( args, '--cwd' )
113 + table.insert( args, cwd )
114 + end
116 + if wezterm.background_child_process then
117 + wezterm.background_child_process( args )
118 + else
119 + local ok, err = pcall( function () wezterm.run_child_process( args ) end )
120 + if not ok then
121 + wezterm.log_error( 'Failed to spawn workspace window: ' .. tostring( err ) )
122 + end
123 + end
124 +end
126 +local function zoxide_chosen_in_new_window( window, path, label_path )
127 + spawn_workspace_window( window, label_path, path )
128 + -- increment zoxide path score
129 + run_child_process( zoxide_path .. ' add ' .. path )
130 +end
132 +local function workspace_chosen_in_new_window( window, workspace )
133 + spawn_workspace_window( window, workspace )
134 +end
90 136 local function workspace_exists( workspace )
91 137 for _, workspace_name in ipairs( mux.get_workspace_names() ) do
92 138 if workspace == workspace_name then
96 142 return false
99 -local function switch_workspace()
145 +local function choose_workspace( title, fuzzy_description, handler )
100 146 return wezterm.action_callback( function ( window, pane )
101 147 local choices = get_choices()
103 149 window:perform_action(
104 150 act.InputSelector {
105 151 action = wezterm.action_callback( function ( inner_window, inner_pane, id, label )
106 - if id and label then
107 - if workspace_exists( id ) then
108 - -- workspace is choosen
109 - workspace_chosen( inner_window, inner_pane, id )
110 - else
111 - -- path is choosen
112 - zoxide_chosen( inner_window, inner_pane, id, label )
113 - end
114 - end
152 + if id and label then handler( inner_window, inner_pane, id, label ) end
115 153 end ),
116 - title = 'Choose Workspace',
154 + title = title,
117 155 description = 'Select a workspace and press Enter = accept, Esc = cancel, / = filter',
118 - fuzzy_description = 'Workspace to switch: ',
156 + fuzzy_description = fuzzy_description,
119 157 choices = choices,
120 158 fuzzy = true,
165 +local function switch_workspace()
166 + return choose_workspace( 'Choose Workspace', 'Workspace to switch: ', function ( window, pane, id, label )
167 + if workspace_exists( id ) then
168 + workspace_chosen( window, pane, id )
169 + else
170 + zoxide_chosen( window, pane, id, label )
171 + end
172 + end )
173 +end
175 +local function switch_workspace_in_new_window()
176 + return choose_workspace( 'Choose Workspace in New Window', 'Workspace for new window: ', function ( window, pane, id, label )
177 + if workspace_exists( id ) then
178 + workspace_chosen_in_new_window( window, id )
179 + else
180 + zoxide_chosen_in_new_window( window, id, label )
181 + end
182 + end )
183 +end
127 185 return {
128 186 switch_workspace = switch_workspace,
187 + switch_workspace_in_new_window = switch_workspace_in_new_window,