repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

update wezterm workspace switching

a5b8b5a873919d19e3e9a956b443d3b9109c2e45

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

+97 / −20 text lines

Diff layout

wezterm/bugabinga/key_binds.lua

Modified

+5 / −0 text lines

190190 {
191191 key = 'z',
192192 mods = 'LEADER',
193 action = workspace_switcher.switch_workspace_in_new_window(),
194 },
195 {
196 key = 'Z',
197 mods = 'LEADER|SHIFT',
193198 action = workspace_switcher.switch_workspace(),
194199 },
195200 {

wezterm/bugabinga/status.lua

Modified

+14 / −1 text lines

22local bat = require 'bugabinga.battery'
44local DELIMITER = ' · '
5local palette = require 'bugabinga.nugu.palette_dark'.palette
5local nugu_dark = require 'bugabinga.nugu.palette_dark'.palette
6local nugu_light = require 'bugabinga.nugu.palette_light'.palette
7local palette = nugu_dark
68local icon_names = require 'bugabinga.icons'
810local animation_tick = 0
12local use_palette_for_scheme = function ( scheme )
13 palette = scheme == 'nugu-light' and nugu_light or nugu_dark
14end
16local use_palette_for_window = function ( window )
17 local ok, config = pcall( function () return window:effective_config() end )
18 if ok and config then use_palette_for_scheme( config.color_scheme ) end
19end
1021local zoomed = function ( pane )
1122 local tab = pane:tab()
1223 local any_is_zoomed = false
415426end
417428wez.on( 'format-tab-title', function ( tab, tabs, panes, config, hover, max_width )
429 if config then use_palette_for_scheme( config.color_scheme ) end
418430 local title, icon, program = tab_title( tab )
419431 local pi_badge, pi_badge_color = pi_state_badge( (tab.active_pane.user_vars or {}).PI_STATE )
420432 local active_progress_badge, active_progress_color = progress_badge( tab.active_pane.progress )
471483-- Show which key table is active in the status area
472484wez.on( 'update-status', function ( window, pane )
485 use_palette_for_window( window )
473486 animation_tick = animation_tick + 1
474487 local key_table = window:active_key_table()
475488 local current_workspace = window:active_workspace()

wezterm/bugabinga/workspace_switcher.lua

Modified

+78 / −19 text lines

3939 return choice_table, workspace_ids
4040end
42local 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
52end
4254local function get_zoxide_elements( choice_table, workspace_ids )
4355 local stdout = run_child_process( zoxide_path .. ' query -l ' )
4557 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
61end
63local 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 )
5368 end
5469 return choice_table
5570end
5873 local choices = {}
5974 local _, workspace_ids = get_workspace_elements( choices )
6075 get_zoxide_elements( choices, workspace_ids )
76 get_workspace_dir_elements( choices, workspace_ids )
6177 return choices
6278end
87103 )
88104end
106local 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
124end
126local 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 )
130end
132local function workspace_chosen_in_new_window( window, workspace )
133 spawn_workspace_window( window, workspace )
134end
90136local function workspace_exists( workspace )
91137 for _, workspace_name in ipairs( mux.get_workspace_names() ) do
92138 if workspace == workspace_name then
96142 return false
97143end
99local function switch_workspace()
145local function choose_workspace( title, fuzzy_description, handler )
100146 return wezterm.action_callback( function ( window, pane )
101147 local choices = get_choices()
103149 window:perform_action(
104150 act.InputSelector {
105151 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
115153 end ),
116 title = 'Choose Workspace',
154 title = title,
117155 description = 'Select a workspace and press Enter = accept, Esc = cancel, / = filter',
118 fuzzy_description = 'Workspace to switch: ',
156 fuzzy_description = fuzzy_description,
119157 choices = choices,
120158 fuzzy = true,
121159 },
124162 end )
125163end
165local 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 )
173end
175local 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 )
183end
127185return {
128186 switch_workspace = switch_workspace,
187 switch_workspace_in_new_window = switch_workspace_in_new_window,