improve wezterm workspace and pi status
e53fea391acbbc11531e943f479ad95af0338632
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 67352c986a88
bugabingas dorkfiles
e53fea391acbbc11531e943f479ad95af0338632
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+318 / −77 text lines
+34 / −6 text lines
function terminalUserVarsEnabled(): boolean { const override = process.env.PI_TERMINAL_USER_VARS?.toLowerCase(); if (override && ["0", "false", "no", "off"].includes(override)) return false; if (process.env.WEZTERM_PANE || process.env.TERM_PROGRAM === "WezTerm") return true; if (process.env.TERM_PROGRAM === "iTerm.app") return true; return false;}function setUserVar(name: string, value: string) { if (!terminalUserVarsEnabled()) return; if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) return; const encoded = Buffer.from(value, "utf8").toString("base64"); writeOSC(`1337;SetUserVar=${name}=${encoded}`);}function setPiState(state: "idle" | "busy" | "done" | "error" | "aborted") { setUserVar("PI_STATE", state);}function agentExitCode(event: { messages?: unknown[] }): number {function agentOutcome(event: { messages?: unknown[] }): { exitCode: number; state: "done" | "error" | "aborted" } { setPiState("busy"); onAgentDone(agentExitCode(event)); setPiState("idle");+284 / −71 text lines
local ai_shell = require 'bugabinga.ai_shell'local busy_frame = function () local frames = { '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏' } return frames[(os.time() % #frames) + 1]endlocal progress_badge = function ( progress ) if not progress or progress == 'None' then return nil end if progress == 'Indeterminate' then return busy_frame(), palette.info end if type( progress ) == 'table' and progress.Error ~= nil then return '✗', palette.error end if type( progress ) == 'table' and progress.Percentage ~= nil then return busy_frame(), palette.info end return busy_frame(), palette.infoendlocal pi_state_badge = function ( state ) if state == 'busy' or state == 'running' or state == 'thinking' then return busy_frame(), palette.info end if state == 'done' or state == 'success' then return '✓', palette.ui.important_local end if state == 'error' or state == 'failed' then return '✗', palette.error end if state == 'aborted' then return '×', palette.warning end if state == 'waiting' or state == 'input' or state == 'needs_input' then return '?', palette.warning end return nilendlocal pane_progress = function ( pane ) local ok, progress = pcall( pane.get_progress, pane ) if ok then return progress end return nilend -- Workspaces created from zoxide entries are often full paths. -- In the status bar, basename carries enough identity and avoids crowding tabs. if name:find( '/', 1, true ) or name:find( '\\', 1, true ) then return basename( name ) or name end if #name > 16 then return wez.truncate_right( name, 16 )local classify_workspace_program = function ( program ) if program == 'pi' then return 'pi' end if program == 'nvim' or program == 'vim' then return 'nvim' end if program == 'git' or program == 'gh' then return 'git' end if program == 'yazi' or program == 'ranger' or program == 'lf' then return 'files' end if program == 'aerc' or program == 'neomutt' then return 'mail' end if program == 'wthrr' then return 'weather' end return nilend summary = { name = workspace, windows = 0, tabs = 0, panes = 0, programs = {} } summary = { name = workspace, windows = 0, tabs = 0, panes = 0, badge = nil, badge_color = nil } local program = detect_program( info.pane ) local class = classify_workspace_program( program ) if class then summary.programs[class] = (summary.programs[class] or 0) + 1 local vars = safe_user_vars( info.pane ) local badge, color = pi_state_badge( vars.PI_STATE ) if not badge then badge, color = progress_badge( pane_progress( info.pane ) ) end if badge and (not summary.badge or badge == '✗' or badge == '?') then summary.badge = badge summary.badge_color = color append_count( parts, 'π', summary.programs.pi ) append_count( parts, '', summary.programs.nvim ) append_count( parts, '', summary.programs.git ) append_count( parts, '', summary.programs.files ) append_count( parts, '', summary.programs.mail ) append_count( parts, '', summary.programs.weather ) if #parts == 1 and active and summary.tabs > 1 then table.insert( elements, { Foreground = { Color = active and palette.ui.important_local or palette.ui.minor } } ) table.insert( elements, { Foreground = { Color = summaries[name].badge_color or (active and palette.ui.important_local or palette.ui.minor) } } ) table.insert( elements, { Text = (active and ' ● ' or ' ○ ') .. workspace_badge( summaries[name], active ) } ) table.insert( elements, { Text = (active and ' ● ' or ' ○ ') .. workspace_badge( summaries[name], active ) .. ' ' } ) table.insert( elements, { Text = ' +' .. tostring( #ordered - 5 ) } ) table.insert( elements, { Text = ' +' .. tostring( #ordered - 5 ) .. ' ' } ) return tab.tab_title, program_icon( explicit_program ) return tab.tab_title, program_icon( explicit_program ), explicit_program return program, program_icon( program ) return program, program_icon( program ), programendlocal noisy_unseen_programs = { pi = true, nvim = true, vim = true, yazi = true, aerc = true, weechat = true, cmus = true,}local mux_tab_label = function ( mux_tab ) local explicit_title = nil local ok_title, title = pcall( function () return mux_tab:get_title() end ) if ok_title and title and title ~= '' then explicit_title = title end local ok_pane, active_pane = pcall( function () return mux_tab:active_pane() end ) local program = nil local state_badge = nil local state_badge_color = nil local has_unseen_output = false if ok_pane and active_pane then program = detect_program( active_pane ) local vars = safe_user_vars( active_pane ) local progress = pane_progress( active_pane ) local pi_badge, pi_color = pi_state_badge( vars.PI_STATE ) local progress_state_badge, progress_color = progress_badge( progress ) state_badge = pi_badge or progress_state_badge state_badge_color = pi_color or progress_color end if not noisy_unseen_programs[program] then local ok_panes, panes = pcall( function () return mux_tab:panes_with_info() end ) if ok_panes then for _, info in ipairs( panes ) do local ok_unseen, unseen = pcall( function () return info.pane:has_unseen_output() end ) if ok_unseen and unseen then has_unseen_output = true break end end end end local label = explicit_title or program or 'nu' return wez.truncate_right( label, 12 ), program_icon( program or detect_program_from_title( label ) ), state_badge, state_badge_color, has_unseen_outputendlocal text_width = function ( text ) return wez.column_width( text )endlocal render_fake_tab = function ( elements, tab, compact ) local bg = tab.active and palette.ui.focus or palette.ui.backdrop local fg = tab.active and palette.ui.backdrop or palette.ui.minor if tab.state_badge_color and not tab.active then bg = tab.state_badge_color fg = palette.content.backdrop elseif tab.has_unseen_output and not tab.active then bg = palette.warning fg = palette.content.backdrop end local suffix = tab.state_badge and (' ' .. tab.state_badge) or (tab.has_unseen_output and ' !' or '') local text if compact and not tab.active then text = string.format( ' %d %s%s ', tab.index + 1, tab.icon, suffix ) else text = string.format( ' %d %s %s%s ', tab.index + 1, tab.icon, tab.label, suffix ) end table.insert( elements, { Background = { Color = bg } } ) table.insert( elements, { Foreground = { Color = fg } } ) if tab.active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end table.insert( elements, { Text = text } ) table.insert( elements, { Attribute = { Intensity = 'Normal' } } ) table.insert( elements, { Background = { Color = palette.content.backdrop } } ) table.insert( elements, { Text = ' ' } ) return text_width( text ) + 1endlocal fake_tabs = function ( window, max_width ) local ok_window, mux_window = pcall( function () return window:mux_window() end ) if not ok_window or not mux_window then return {} end local ok_tabs, tabs = pcall( function () return mux_window:tabs_with_info() end ) if not ok_tabs then return {} end local tab_models = {} local full_width = 0 for _, info in ipairs( tabs ) do local label, icon, state_badge, state_badge_color, has_unseen_output = mux_tab_label( info.tab ) local model = { index = info.index, active = info.is_active, label = label, icon = icon, state_badge = state_badge, state_badge_color = state_badge_color, has_unseen_output = has_unseen_output, } local suffix = model.state_badge and (' ' .. model.state_badge) or (model.has_unseen_output and ' !' or '') full_width = full_width + text_width( string.format( ' %d %s %s%s ', model.index + 1, model.icon, model.label, suffix ) ) + 1 table.insert( tab_models, model ) end local compact = full_width > max_width local active_index = 1 for i, model in ipairs( tab_models ) do if model.active then active_index = i break end end local selected = { [active_index] = true } local suffix = tab_models[active_index].state_badge and (' ' .. tab_models[active_index].state_badge) or (tab_models[active_index].has_unseen_output and ' !' or '') 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 ) local used = text_width( active_text ) + 1 local overflow_reserve = 4 local budget = math.max( used, max_width - overflow_reserve ) local left = active_index - 1 local right = active_index + 1 while left >= 1 or right <= #tab_models do local progressed = false for _, i in ipairs { right, left } do local model = tab_models[i] if model then local s = model.state_badge and (' ' .. model.state_badge) or (model.has_unseen_output and ' !' or '') local text = compact and string.format( ' %d %s%s ', model.index + 1, model.icon, s ) or string.format( ' %d %s %s%s ', model.index + 1, model.icon, model.label, s ) local width = text_width( text ) + 1 if used + width <= budget then selected[i] = true used = used + width progressed = true end end end right = right + 1 left = left - 1 if not progressed and used >= budget then break end end local rendered = {} local hidden = 0 for i, model in ipairs( tab_models ) do if selected[i] then table.insert( rendered, model ) else hidden = hidden + 1 end end local elements = {} if hidden > 0 then table.insert( elements, { Foreground = { Color = palette.ui.minor } } ) table.insert( elements, { Text = '+' .. tostring( hidden ) .. ' ' } ) end for _, model in ipairs( rendered ) do render_fake_tab( elements, model, compact ) end return elements local title, icon = tab_title( tab ) local title, icon, program = tab_title( tab ) local pi_badge, pi_badge_color = pi_state_badge( (tab.active_pane.user_vars or {}).PI_STATE ) local active_progress_badge, active_progress_color = progress_badge( tab.active_pane.progress ) local state_badge = pi_badge or active_progress_badge local state_badge_color = pi_badge_color or active_progress_color local time = wez.time.now():format '%H:%M' local ai_status = ai_shell.get_status( window ) local status = { time, ai_status, leader, } local status = {} -- Special program indicators if prog == 'pi' then table.insert( status, wez.format { { Foreground = { Color = '#d151d1' } }, { Attribute = { Intensity = 'Bold' } }, { Text = 'pi' }, } ) elseif prog and prog ~= 'nu' then if prog and prog ~= 'nu' and prog ~= 'pi' then local right_elements = { { Text = table.concat( status, DELIMITER ), }, } if key_table then table.insert( right_elements, { Text = DELIMITER } ) table.insert( right_elements, { Background = { Color = palette.error, }, } ) table.insert( right_elements, { Foreground = { Color = palette.ui.backdrop, }, } ) table.insert( right_elements, { Text = ' ' .. key_table .. ' ', } ) local status_parts = {} for _, value in ipairs( status ) do if value and value ~= '' then table.insert( status_parts, value ) end end local right_elements = {} local status_text = table.concat( status_parts, DELIMITER ) if status_text ~= '' then table.insert( right_elements, { Foreground = { Color = palette.ui.minor } } ) table.insert( right_elements, { Text = status_text .. ' ' } ) end local pane_dims = pane:get_dimensions() local tab_budget = math.max( 24, math.floor( pane_dims.cols * 0.45 ) ) if status_text ~= '' then tab_budget = tab_budget - text_width( status_text ) - 2 end for _, element in ipairs( fake_tabs( window, tab_budget ) ) do table.insert( right_elements, element ) local left_elements = { { Text = ' ' }, { Text = battery_icons }, } for _, element in ipairs( workspace_hud( current_workspace ) ) do table.insert( left_elements, element ) local left_elements = {} if key_table or leader then local mode_text = key_table or 'LEADER' left_elements = { { Background = { Color = palette.error } }, { Foreground = { Color = palette.ui.backdrop } }, { Attribute = { Intensity = 'Bold' } }, { Text = ' ' .. mode_text .. ' ' }, 'ResetAttributes', { Text = ' ' }, } else left_elements = { { Text = battery_icons }, } for _, element in ipairs( workspace_hud( current_workspace ) ) do table.insert( left_elements, element ) end