repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

improve wezterm workspace and pi status

e53fea391acbbc11531e943f479ad95af0338632

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (2)

+318 / −77 text lines

Diff layout

pi/agent/extensions/terminal-signals/index.ts

Modified

+34 / −6 text lines

1919 * - OSC 133;D;<exit-code> (command completed) on agent_end WHEN UNFOCUSED
2020 * → terminal fires a completion notification (e.g. Ghostty tab badge)
2121 *
22 * - OSC 1337;SetUserVar=PI_STATE=<base64> on WezTerm/iTerm2
23 * → terminal config can render durable agent state (busy/done/error)
24 *
2225 * Agent-end signals are suppressed when the terminal is focused — you're
2326 * already looking at Pi. They fire on focus-loss after agent completes,
2427 * or immediately if you tabbed away during the run.
4144 process.stdout.write(`${OSC}${sequence}${BEL}`);
47function terminalUserVarsEnabled(): boolean {
48 const override = process.env.PI_TERMINAL_USER_VARS?.toLowerCase();
49 if (override && ["0", "false", "no", "off"].includes(override)) return false;
50 if (process.env.WEZTERM_PANE || process.env.TERM_PROGRAM === "WezTerm") return true;
51 if (process.env.TERM_PROGRAM === "iTerm.app") return true;
52 return false;
53}
55function setUserVar(name: string, value: string) {
56 if (!terminalUserVarsEnabled()) return;
57 if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) return;
58 const encoded = Buffer.from(value, "utf8").toString("base64");
59 writeOSC(`1337;SetUserVar=${name}=${encoded}`);
60}
62function setPiState(state: "idle" | "busy" | "done" | "error" | "aborted") {
63 setUserVar("PI_STATE", state);
64}
4466function startProgress() {
4567 writeOSC("9;4;3");
5779 writeOSC(`133;D;${exitCode}`);
60function agentExitCode(event: { messages?: unknown[] }): number {
82function agentOutcome(event: { messages?: unknown[] }): { exitCode: number; state: "done" | "error" | "aborted" } {
6183 const messages = Array.isArray(event.messages) ? event.messages : [];
6385 for (let i = messages.length - 1; i >= 0; i--) {
6486 const message = messages[i] as { role?: unknown; stopReason?: unknown } | undefined;
6587 if (message?.role !== "assistant") continue;
66 if (message.stopReason === "aborted") return 130;
67 if (message.stopReason === "error") return 1;
68 return 0;
88 if (message.stopReason === "aborted") return { exitCode: 130, state: "aborted" };
89 if (message.stopReason === "error") return { exitCode: 1, state: "error" };
90 return { exitCode: 0, state: "done" };
6991 }
71 return 0;
93 return { exitCode: 0, state: "done" };
7496export default function (pi: ExtensionAPI) {
89111 pi.on("session_start", async (event, _ctx: ExtensionContext) => {
90112 focus.attach();
114 setPiState("idle");
92116 if (event.reason === "new" || event.reason === "resume" || event.reason === "fork") {
93117 stopProgress();
94118 pendingDoneExitCode = null;
98122 pi.on("agent_start", async (_event, _ctx: ExtensionContext) => {
99123 pendingDoneExitCode = null;
124 setPiState("busy");
100125 markCommandExecuted();
101126 startProgress();
102127 });
104129 pi.on("agent_end", async (event, _ctx: ExtensionContext) => {
105 onAgentDone(agentExitCode(event));
130 const outcome = agentOutcome(event);
131 setPiState(outcome.state);
132 onAgentDone(outcome.exitCode);
106133 });
108135 // Focus change: if agent already finished, send completion signal on tab-away
119146 focus.offFocus(onFocusChange);
120147 focus.detach();
121148 stopProgress();
149 setPiState("idle");
122150 pendingDoneExitCode = null;
123151 });

wezterm/bugabinga/status.lua

Modified

+284 / −71 text lines

22local bat = require 'bugabinga.battery'
44local DELIMITER = ' · '
5local ai_shell = require 'bugabinga.ai_shell'
65local palette = require 'bugabinga.nugu.palette_dark'.palette
76local icon_names = require 'bugabinga.icons'
8281 return ''
8382end
84local busy_frame = function ()
85 local frames = { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' }
86 return frames[(os.time() % #frames) + 1]
87end
89local progress_badge = function ( progress )
90 if not progress or progress == 'None' then return nil end
91 if progress == 'Indeterminate' then return busy_frame(), palette.info end
92 if type( progress ) == 'table' and progress.Error ~= nil then return '✗', palette.error end
93 if type( progress ) == 'table' and progress.Percentage ~= nil then return busy_frame(), palette.info end
94 return busy_frame(), palette.info
95end
97local pi_state_badge = function ( state )
98 if state == 'busy' or state == 'running' or state == 'thinking' then return busy_frame(), palette.info end
99 if state == 'done' or state == 'success' then return '✓', palette.ui.important_local end
100 if state == 'error' or state == 'failed' then return '✗', palette.error end
101 if state == 'aborted' then return '×', palette.warning end
102 if state == 'waiting' or state == 'input' or state == 'needs_input' then return '?', palette.warning end
103 return nil
104end
106local pane_progress = function ( pane )
107 local ok, progress = pcall( pane.get_progress, pane )
108 if ok then return progress end
109 return nil
110end
85112local short_workspace = function ( name )
86113 local home = wez.home_dir
87114 if home and name:sub( 1, #home ) == home then
88115 name = '~' .. name:sub( #home + 1 )
89116 end
90 if #name > 28 then
91 return '…/' .. (basename( name ) or name)
118 -- Workspaces created from zoxide entries are often full paths.
119 -- In the status bar, basename carries enough identity and avoids crowding tabs.
120 if name:find( '/', 1, true ) or name:find( '\\', 1, true ) then
121 return basename( name ) or name
122 end
124 if #name > 16 then
125 return wez.truncate_right( name, 16 )
92126 end
93127 return name
94128end
96local classify_workspace_program = function ( program )
97 if program == 'pi' then return 'pi' end
98 if program == 'nvim' or program == 'vim' then return 'nvim' end
99 if program == 'git' or program == 'gh' then return 'git' end
100 if program == 'yazi' or program == 'ranger' or program == 'lf' then return 'files' end
101 if program == 'aerc' or program == 'neomutt' then return 'mail' end
102 if program == 'wthrr' then return 'weather' end
103 return nil
104end
106130local workspace_summaries = function ()
107131 local ok, mux_windows = pcall( wez.mux.all_windows )
108132 if not ok then return {} end
113137 if ok_workspace and workspace then
114138 local summary = summaries[workspace]
115139 if not summary then
116 summary = { name = workspace, windows = 0, tabs = 0, panes = 0, programs = {} }
140 summary = { name = workspace, windows = 0, tabs = 0, panes = 0, badge = nil, badge_color = nil }
117141 summaries[workspace] = summary
118142 end
119143 summary.windows = summary.windows + 1
126150 if ok_panes then
127151 for _, info in ipairs( panes ) do
128152 summary.panes = summary.panes + 1
129 local program = detect_program( info.pane )
130 local class = classify_workspace_program( program )
131 if class then
132 summary.programs[class] = (summary.programs[class] or 0) + 1
153 local vars = safe_user_vars( info.pane )
154 local badge, color = pi_state_badge( vars.PI_STATE )
155 if not badge then badge, color = progress_badge( pane_progress( info.pane ) ) end
156 if badge and (not summary.badge or badge == '✗' or badge == '?') then
157 summary.badge = badge
158 summary.badge_color = color
133159 end
134160 end
135161 end
141167 return summaries
142168end
144local append_count = function ( parts, icon, count )
145 if count and count > 0 then
146 table.insert( parts, icon .. (count > 1 and tostring( count ) or '') )
147 end
148end
150170local workspace_badge = function ( summary, active )
151171 local parts = { short_workspace( summary.name ) }
152 append_count( parts, 'π', summary.programs.pi )
153 append_count( parts, '', summary.programs.nvim )
154 append_count( parts, '', summary.programs.git )
155 append_count( parts, '', summary.programs.files )
156 append_count( parts, '󰇮', summary.programs.mail )
157 append_count( parts, '󰖐', summary.programs.weather )
159 if #parts == 1 and active and summary.tabs > 1 then
173 if summary.badge then
174 table.insert( parts, summary.badge )
175 end
177 if active and summary.tabs > 1 then
160178 table.insert( parts, tostring( summary.tabs ) .. 't' )
161179 end
182200 if shown > 5 then break end
184202 local active = name == active_workspace
185 table.insert( elements, { Foreground = { Color = active and palette.ui.important_local or palette.ui.minor } } )
203 table.insert( elements, { Foreground = { Color = summaries[name].badge_color or (active and palette.ui.important_local or palette.ui.minor) } } )
186204 if active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end
187 table.insert( elements, { Text = (active and ' ● ' or ' ○ ') .. workspace_badge( summaries[name], active ) } )
205 table.insert( elements, { Text = (active and ' ● ' or ' ○ ') .. workspace_badge( summaries[name], active ) .. ' ' } )
188206 table.insert( elements, { Attribute = { Intensity = 'Normal' } } )
189207 end
191209 if #ordered > 5 then
192210 table.insert( elements, { Foreground = { Color = palette.ui.minor } } )
193 table.insert( elements, { Text = ' +' .. tostring( #ordered - 5 ) } )
211 table.insert( elements, { Text = ' +' .. tostring( #ordered - 5 ) .. ' ' } )
194212 end
214 table.insert( elements, { Text = ' ' } )
196216 return elements
197217end
199219local tab_title = function ( tab )
200220 if tab.tab_title and #tab.tab_title > 0 then
201221 local explicit_program = detect_program_from_title( tab.tab_title )
202 return tab.tab_title, program_icon( explicit_program )
222 return tab.tab_title, program_icon( explicit_program ), explicit_program
203223 end
204224 local vars = tab.active_pane.user_vars or {}
205225 local program = normalize_program( first_command( vars.WEZTERM_PROG ) )
206226 or detect_program_from_title( tab.active_pane.title )
207227 or normalize_program( tab.active_pane.foreground_process_name )
208228 or 'nu'
209 return program, program_icon( program )
229 return program, program_icon( program ), program
230end
232local noisy_unseen_programs = {
233 pi = true,
234 nvim = true,
235 vim = true,
236 yazi = true,
237 aerc = true,
238 weechat = true,
239 cmus = true,
240}
242local mux_tab_label = function ( mux_tab )
243 local explicit_title = nil
244 local ok_title, title = pcall( function () return mux_tab:get_title() end )
245 if ok_title and title and title ~= '' then explicit_title = title end
247 local ok_pane, active_pane = pcall( function () return mux_tab:active_pane() end )
248 local program = nil
249 local state_badge = nil
250 local state_badge_color = nil
251 local has_unseen_output = false
253 if ok_pane and active_pane then
254 program = detect_program( active_pane )
255 local vars = safe_user_vars( active_pane )
256 local progress = pane_progress( active_pane )
257 local pi_badge, pi_color = pi_state_badge( vars.PI_STATE )
258 local progress_state_badge, progress_color = progress_badge( progress )
259 state_badge = pi_badge or progress_state_badge
260 state_badge_color = pi_color or progress_color
261 end
263 if not noisy_unseen_programs[program] then
264 local ok_panes, panes = pcall( function () return mux_tab:panes_with_info() end )
265 if ok_panes then
266 for _, info in ipairs( panes ) do
267 local ok_unseen, unseen = pcall( function () return info.pane:has_unseen_output() end )
268 if ok_unseen and unseen then
269 has_unseen_output = true
270 break
271 end
272 end
273 end
274 end
276 local label = explicit_title or program or 'nu'
277 return wez.truncate_right( label, 12 ), program_icon( program or detect_program_from_title( label ) ), state_badge, state_badge_color, has_unseen_output
278end
280local text_width = function ( text )
281 return wez.column_width( text )
282end
284local render_fake_tab = function ( elements, tab, compact )
285 local bg = tab.active and palette.ui.focus or palette.ui.backdrop
286 local fg = tab.active and palette.ui.backdrop or palette.ui.minor
288 if tab.state_badge_color and not tab.active then
289 bg = tab.state_badge_color
290 fg = palette.content.backdrop
291 elseif tab.has_unseen_output and not tab.active then
292 bg = palette.warning
293 fg = palette.content.backdrop
294 end
296 local suffix = tab.state_badge and (' ' .. tab.state_badge) or (tab.has_unseen_output and ' !' or '')
297 local text
298 if compact and not tab.active then
299 text = string.format( ' %d %s%s ', tab.index + 1, tab.icon, suffix )
300 else
301 text = string.format( ' %d %s %s%s ', tab.index + 1, tab.icon, tab.label, suffix )
302 end
304 table.insert( elements, { Background = { Color = bg } } )
305 table.insert( elements, { Foreground = { Color = fg } } )
306 if tab.active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end
307 table.insert( elements, { Text = text } )
308 table.insert( elements, { Attribute = { Intensity = 'Normal' } } )
309 table.insert( elements, { Background = { Color = palette.content.backdrop } } )
310 table.insert( elements, { Text = ' ' } )
312 return text_width( text ) + 1
313end
315local fake_tabs = function ( window, max_width )
316 local ok_window, mux_window = pcall( function () return window:mux_window() end )
317 if not ok_window or not mux_window then return {} end
319 local ok_tabs, tabs = pcall( function () return mux_window:tabs_with_info() end )
320 if not ok_tabs then return {} end
322 local tab_models = {}
323 local full_width = 0
324 for _, info in ipairs( tabs ) do
325 local label, icon, state_badge, state_badge_color, has_unseen_output = mux_tab_label( info.tab )
326 local model = {
327 index = info.index,
328 active = info.is_active,
329 label = label,
330 icon = icon,
331 state_badge = state_badge,
332 state_badge_color = state_badge_color,
333 has_unseen_output = has_unseen_output,
334 }
335 local suffix = model.state_badge and (' ' .. model.state_badge) or (model.has_unseen_output and ' !' or '')
336 full_width = full_width + text_width( string.format( ' %d %s %s%s ', model.index + 1, model.icon, model.label, suffix ) ) + 1
337 table.insert( tab_models, model )
338 end
340 local compact = full_width > max_width
341 local active_index = 1
342 for i, model in ipairs( tab_models ) do
343 if model.active then
344 active_index = i
345 break
346 end
347 end
349 local selected = { [active_index] = true }
350 local suffix = tab_models[active_index].state_badge and (' ' .. tab_models[active_index].state_badge)
351 or (tab_models[active_index].has_unseen_output and ' !' or '')
352 local active_text = string.format( ' %d %s %s%s ', tab_models[active_index].index + 1, tab_models[active_index].icon, tab_models[active_index].label, suffix )
353 local used = text_width( active_text ) + 1
354 local overflow_reserve = 4
355 local budget = math.max( used, max_width - overflow_reserve )
357 local left = active_index - 1
358 local right = active_index + 1
359 while left >= 1 or right <= #tab_models do
360 local progressed = false
362 for _, i in ipairs { right, left } do
363 local model = tab_models[i]
364 if model then
365 local s = model.state_badge and (' ' .. model.state_badge) or (model.has_unseen_output and ' !' or '')
366 local text = compact
367 and string.format( ' %d %s%s ', model.index + 1, model.icon, s )
368 or string.format( ' %d %s %s%s ', model.index + 1, model.icon, model.label, s )
369 local width = text_width( text ) + 1
370 if used + width <= budget then
371 selected[i] = true
372 used = used + width
373 progressed = true
374 end
375 end
376 end
378 right = right + 1
379 left = left - 1
380 if not progressed and used >= budget then break end
381 end
383 local rendered = {}
384 local hidden = 0
385 for i, model in ipairs( tab_models ) do
386 if selected[i] then
387 table.insert( rendered, model )
388 else
389 hidden = hidden + 1
390 end
391 end
393 local elements = {}
394 if hidden > 0 then
395 table.insert( elements, { Foreground = { Color = palette.ui.minor } } )
396 table.insert( elements, { Text = '+' .. tostring( hidden ) .. ' ' } )
397 end
399 for _, model in ipairs( rendered ) do
400 render_fake_tab( elements, model, compact )
401 end
403 return elements
210404end
212406wez.on( 'format-tab-title', function ( tab, tabs, panes, config, hover, max_width )
213 local title, icon = tab_title( tab )
407 local title, icon, program = tab_title( tab )
408 local pi_badge, pi_badge_color = pi_state_badge( (tab.active_pane.user_vars or {}).PI_STATE )
409 local active_progress_badge, active_progress_color = progress_badge( tab.active_pane.progress )
410 local state_badge = pi_badge or active_progress_badge
411 local state_badge_color = pi_badge_color or active_progress_color
214412 local has_unseen_output = false
215 for _, pane_info in ipairs( tab.panes or {} ) do
216 if pane_info.has_unseen_output then
217 has_unseen_output = true
218 break
413 if not noisy_unseen_programs[program] then
414 for _, pane_info in ipairs( tab.panes or {} ) do
415 if pane_info.has_unseen_output then
416 has_unseen_output = true
417 break
418 end
219419 end
220420 end
224424 if tab.is_active then
225425 bg = palette.ui.focus
226426 fg = palette.ui.backdrop
427 elseif state_badge_color then
428 bg = state_badge_color
429 fg = palette.content.backdrop
227430 elseif has_unseen_output then
228431 bg = palette.warning
229432 fg = palette.content.backdrop
236439 fg = palette.ui.minor
237440 end
239 local suffix = has_unseen_output and ' !' or ''
240 local text = string.format( ' %d %s %s%s ', tab.tab_index + 1, icon, title, suffix )
442 local suffix = state_badge and (' ' .. state_badge) or (has_unseen_output and ' !' or '')
443 local text = string.format( ' %d %s %s%s ', tab.tab_index + 1, icon, title, suffix )
241444 text = wez.truncate_right( text, math.max( 1, max_width ) )
243446 return {
267470 local pane_domain_ok, pane_domain = pcall( pane.get_domain_name, pane )
268471 if not pane_domain_ok then return end
270 local time = wez.time.now():format '%H:%M'
271 local ai_status = ai_shell.get_status( window )
272 local status = { time, ai_status, leader, }
473 local status = {}
274 -- Special program indicators
275475 local prog = detect_program( pane )
276 if prog == 'pi' then
277 table.insert( status, wez.format {
278 { Foreground = { Color = '#d151d1' } },
279 { Attribute = { Intensity = 'Bold' } },
280 { Text = 'pi' },
281 } )
282 elseif prog and prog ~= 'nu' then
476 if prog and prog ~= 'nu' and prog ~= 'pi' then
283477 table.insert( status, program_icon( prog ) .. ' ' .. prog )
284478 end
286 if key_table then
287 table.insert( status, key_table )
288 end
289480 if zoomed_state then
290481 table.insert( status, zoomed_state )
291482 end
294485 table.insert( status, '@' .. pane_domain )
295486 end
297 local right_elements = {
298 { Text = table.concat( status, DELIMITER ), },
299 }
300 if key_table then
301 table.insert( right_elements, { Text = DELIMITER } )
302 table.insert( right_elements, { Background = { Color = palette.error, }, } )
303 table.insert( right_elements, { Foreground = { Color = palette.ui.backdrop, }, } )
304 table.insert( right_elements, { Text = ' ' .. key_table .. ' ', } )
488 local status_parts = {}
489 for _, value in ipairs( status ) do
490 if value and value ~= '' then table.insert( status_parts, value ) end
491 end
493 local right_elements = {}
494 local status_text = table.concat( status_parts, DELIMITER )
495 if status_text ~= '' then
496 table.insert( right_elements, { Foreground = { Color = palette.ui.minor } } )
497 table.insert( right_elements, { Text = status_text .. ' ' } )
498 end
499 local pane_dims = pane:get_dimensions()
500 local tab_budget = math.max( 24, math.floor( pane_dims.cols * 0.45 ) )
501 if status_text ~= '' then tab_budget = tab_budget - text_width( status_text ) - 2 end
502 for _, element in ipairs( fake_tabs( window, tab_budget ) ) do
503 table.insert( right_elements, element )
305504 end
307506 window:set_right_status( wez.format( right_elements ) )
310509 battery_icons = battery_icons .. ' '
311510 end
313 local left_elements = {
314 { Text = '  ' },
315 { Text = battery_icons },
316 }
317 for _, element in ipairs( workspace_hud( current_workspace ) ) do
318 table.insert( left_elements, element )
512 local left_elements = {}
513 if key_table or leader then
514 local mode_text = key_table or 'LEADER'
515 left_elements = {
516 { Background = { Color = palette.error } },
517 { Foreground = { Color = palette.ui.backdrop } },
518 { Attribute = { Intensity = 'Bold' } },
519 { Text = ' 󱁐 ' .. mode_text .. ' ' },
520 'ResetAttributes',
521 { Text = ' ' },
522 }
523 else
524 left_elements = {
525 { Text = battery_icons },
526 }
527 for _, element in ipairs( workspace_hud( current_workspace ) ) do
528 table.insert( left_elements, element )
529 end
319530 end
321532 window:set_left_status( wez.format( left_elements ) )
327538 cfg.hide_tab_bar_if_only_one_tab = false
328539 cfg.use_fancy_tab_bar = false
329540 cfg.tab_max_width = 32
541 cfg.show_tabs_in_tab_bar = false
542 cfg.show_new_tab_button_in_tab_bar = false
330543 cfg.tab_bar_at_bottom = true
331544 cfg.status_update_interval = 1000
332545end