local wez = require 'wezterm' local bat = require 'bugabinga.battery' local DELIMITER = ' · ' local nugu_dark = require 'bugabinga.nugu.palette_dark'.palette local nugu_light = require 'bugabinga.nugu.palette_light'.palette local palette = nugu_dark package.loaded['bugabinga.apps'] = nil local apps = require 'bugabinga.apps' local MAX_TAB_LABEL_WIDTH = 172 local WORKSPACE_SUMMARY_TTL_SECONDS = 2.0 local PALETTE_REFRESH_TTL_SECONDS = 2.0 local workspace_summary_cache = { at = 0, value = nil } local stable_tab_programs = {} local tab_activity_until = {} local last_palette_refresh = 0 local now = function () -- update-status must never depend on optional WezTerm time APIs. os.clock() -- is enough for small TTLs and avoids stale status bars from callback errors. return os.clock() end local use_palette_for_scheme = function ( scheme ) palette = scheme == 'nugu-light' and nugu_light or nugu_dark end local use_palette_for_window = function ( window ) -- effective_config() crosses into WezTerm internals; do not poll it at 10Hz. local current_time = now() if current_time - last_palette_refresh < PALETTE_REFRESH_TTL_SECONDS then return end last_palette_refresh = current_time local ok, config = pcall( function () return window:effective_config() end ) if ok and config then use_palette_for_scheme( config.color_scheme ) end end local zoomed = function ( pane ) local tab = pane:tab() if not tab then return nil end for _, info in ipairs( tab:panes_with_info() ) do if info.is_zoomed then return '🔎 Zoomed Pane' end end return nil end local safe_user_vars = function ( pane ) local ok, vars = pcall( pane.get_user_vars, pane ) if ok and vars then return vars end return {} end local basename = function ( value ) if not value or value == '' then return nil end value = value:gsub( '\\', '/' ) return value:match( '([^/]+)$' ) or value end local program_icon = function ( program ) return apps.icon( program, wez.nerdfonts ) end local pulse_color = function () return palette.ui.important_local end local busy_badge = function () return '●', pulse_color() end local progress_badge = function ( progress ) if not progress or progress == 'None' then return nil end if progress == 'Indeterminate' then return busy_badge() 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_badge() end return busy_badge() end local colored_badge = function ( elements, badge, color, fallback ) if not badge then return end table.insert( elements, { Text = ' ' } ) table.insert( elements, { Foreground = { Color = color or fallback } } ) table.insert( elements, { Text = badge } ) table.insert( elements, { Foreground = { Color = fallback } } ) end local fixed_badge = function ( elements, badge, color, fallback ) table.insert( elements, { Text = ' ' } ) table.insert( elements, { Foreground = { Color = color or fallback } } ) table.insert( elements, { Text = badge or ' ' } ) table.insert( elements, { Foreground = { Color = fallback } } ) end local state_badge = function ( state ) if state == 'busy' or state == 'running' or state == 'thinking' or state == 'loading' then return busy_badge() 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 nil end local user_state_badge = function ( vars ) vars = vars or {} local badge, color = state_badge( vars.WEZTERM_STATE ) if badge then return badge, color end return state_badge( vars.PI_STATE ) end local ACTIVITY_MARKERS = { -- Codex/Pi/Claude terminal-title spinner frames. Never render these in labels. '◌', '○', '◐', '◓', '◑', '◒', '◎', '◉', '●', '◜', '◠', '◝', '◞', '◡', '◟', '⠁', '⠂', '⠄', '⠆', '⠇', '⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠏', '⠐', '⣾', '⣽', '⣻', '⢿', '⡿', '⣟', '⣯', '⣷', '✳', '✢', '✣', '✤', '✥', '✦', '✧', '✶', '✷', '✸', '✹', '✺', '✻', '✼', '✽', '✾', '✿', } local strip_activity_marker = function ( title ) title = apps.clean_title( title ) if not title then return nil, false end local had_marker = false local changed = true while changed do changed = false title = apps.clean_title( title ) or title for _, marker in ipairs( ACTIVITY_MARKERS ) do if title:sub( 1, #marker ) == marker then title = title:sub( #marker + 1 ) had_marker = true changed = true end if title:sub( -#marker ) == marker then title = title:sub( 1, #title - #marker ) had_marker = true changed = true end end end return apps.clean_title( title ), had_marker end local activity_badge = function ( title ) local _, had_marker = strip_activity_marker( title ) if had_marker then return busy_badge() end return nil end local short_workspace = function ( name ) local home = wez.home_dir if home and name:sub( 1, #home ) == home then name = '~' .. name:sub( #home + 1 ) end -- 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 ) end return name end local workspace_summaries = function () -- Workspace contents change slowly, but update-status runs often for spinners. -- Cache the expensive mux-wide scan and keep animation work local to tabs. local current_time = now() if workspace_summary_cache.value and current_time - workspace_summary_cache.at < WORKSPACE_SUMMARY_TTL_SECONDS then return workspace_summary_cache.value end local ok, mux_windows = pcall( wez.mux.all_windows ) if not ok then return {} end local summaries = {} for _, mux_window in ipairs( mux_windows ) do local ok_workspace, workspace = pcall( function () return mux_window:get_workspace() end ) if ok_workspace and workspace then local summary = summaries[workspace] if not summary then summary = { name = workspace, windows = 0, tabs = 0, panes = 0, badge = nil, badge_color = nil } summaries[workspace] = summary end summary.windows = summary.windows + 1 local ok_tabs, tabs = pcall( function () return mux_window:tabs() end ) if ok_tabs then for _, tab in ipairs( tabs ) do summary.tabs = summary.tabs + 1 local ok_panes, panes = pcall( function () return tab:panes_with_info() end ) if ok_panes then for _, info in ipairs( panes ) do summary.panes = summary.panes + 1 local vars = safe_user_vars( info.pane ) local badge, color = user_state_badge( vars ) if badge and (not summary.badge or badge == '✗' or badge == '?') then summary.badge = badge summary.badge_color = color end end end end end end end workspace_summary_cache = { at = current_time, value = summaries } return summaries end local workspace_hud = function ( active_workspace ) local summaries = workspace_summaries() local names = {} for name in pairs( summaries ) do table.insert( names, name ) end table.sort( names ) local ordered = {} if summaries[active_workspace] then table.insert( ordered, active_workspace ) end for _, name in ipairs( names ) do if name ~= active_workspace then table.insert( ordered, name ) end end local elements = {} local shown = 0 for _, name in ipairs( ordered ) do shown = shown + 1 if shown > 5 then break end local active = name == active_workspace local fg = active and palette.ui.important_local or palette.ui.minor local summary = summaries[name] table.insert( elements, { Foreground = { Color = fg } } ) if active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end table.insert( elements, { Text = (active and ' ● ' or ' ○ ') .. short_workspace( summary.name ) } ) if not active then colored_badge( elements, summary.badge, summary.badge_color, fg ) end if active and summary.tabs > 1 then table.insert( elements, { Text = ' ' .. tostring( summary.tabs ) .. 't' } ) end table.insert( elements, { Text = ' ' } ) table.insert( elements, { Attribute = { Intensity = 'Normal' } } ) end if #ordered > 5 then table.insert( elements, { Foreground = { Color = palette.ui.minor } } ) table.insert( elements, { Text = ' +' .. tostring( #ordered - 5 ) .. ' ' } ) end table.insert( elements, { Text = ' ' } ) return elements end local domain_hud = function ( domain ) if not domain or domain == 'local' or domain == 'Local' then return {} end local color = palette.ui.important_local if domain:find( '^scope:', 1, false ) then color = palette.warning end return { { Foreground = { Color = color } }, { Attribute = { Intensity = 'Bold' } }, { Text = '@' .. domain .. ' ' }, { Attribute = { Intensity = 'Normal' } }, } end local tab_label = function ( program, detail, max_width ) program = apps.normalize( program ) or 'nu' local base = apps.label( program ) or apps.label( 'nu' ) local label = detail and (base .. ' · ' .. detail) or base return wez.truncate_right( label, max_width or MAX_TAB_LABEL_WIDTH ) end local collapse_duplicate_suffix = function ( title ) local head, tail = title:match( '^(.*)%s+[-–—:]%s+(.+)$' ) if not head or not tail then return title end local compact_head = head:lower():gsub( '[^%w]', '' ) local compact_tail = tail:lower():gsub( '[^%w]', '' ) if compact_tail ~= '' and compact_head == compact_tail then return head end return title end local title_detail = function ( title, program, include_shell_titles ) title = strip_activity_marker( title ) if not title then return nil end -- Keep OSC titles visible. Only strip generic app prefixes/spinner frames and -- exact duplicate suffixes; do not special-case current task/window names. local detail = apps.title_detail( title, program, include_shell_titles ) or title detail = strip_activity_marker( detail ) detail = collapse_duplicate_suffix( detail ) detail = apps.clean_title( detail ) if not detail or apps.normalize( detail ) == apps.normalize( program ) then return nil end return detail end local detect_from_process_info = function ( info ) local detected = nil local visit visit = function ( node ) if detected or type( node ) ~= 'table' then return end local tokens = {} for _, key in ipairs { 'name', 'executable' } do if node[key] then table.insert( tokens, tostring( node[key] ) ) end end if type( node.argv ) == 'table' then for _, arg in ipairs( node.argv ) do table.insert( tokens, tostring( arg ) ) end end for _, token in ipairs( tokens ) do local normalized = apps.normalize( token ) if apps.is_root( normalized ) then detected = normalized return end -- Only match substrings inside path-like tokens, never arbitrary task text. local lower = token:lower() if lower:find( '/', 1, true ) or lower:find( '\\', 1, true ) then for _, program in ipairs { 'codex', 'claude', 'opencode', 'pi' } do if lower:find( program, 1, true ) then detected = program return end end end end if type( node.children ) == 'table' then for _, child in pairs( node.children ) do visit( child ) end end end visit( info ) return detected end local strong_title_program = function ( title ) title = apps.clean_title( title ) if not title then return nil, false end if title:sub( 1, #'π' ) == 'π' then return 'pi', false end local _, had_marker = strip_activity_marker( title ) if had_marker then return 'claude', true end return nil, false end local process_info_program = function ( tab ) local pane_id = tab.active_pane and tab.active_pane.pane_id if not pane_id then return nil end local ok_pane, mux_pane = pcall( wez.mux.get_pane, pane_id ) if not ok_pane or not mux_pane then return nil end local ok_info, info = pcall( mux_pane.get_foreground_process_info, mux_pane ) if not ok_info or not info then return nil end local program = detect_from_process_info( info ) if apps.is_root( program ) then return program end return nil end local tab_title = function ( tab ) local id = tab.tab_id or tab.tab_index local explicit_title = apps.clean_title( tab.tab_title ) local pane_title = apps.clean_title( tab.active_pane.title ) local vars = tab.active_pane.user_vars or {} local clean_pane_title = strip_activity_marker( pane_title ) local clean_explicit_title = strip_activity_marker( explicit_title ) local from_command = apps.detect_from_commandline( vars.WEZTERM_PROG ) local from_info = process_info_program( tab ) local from_process = apps.normalize( tab.active_pane.foreground_process_name ) if not apps.is_root( from_process ) then from_process = nil end local from_explicit_title, explicit_weak = strong_title_program( explicit_title ) local from_pane_title, pane_weak = strong_title_program( pane_title ) local from_title = from_explicit_title or from_pane_title local weak_title = (from_explicit_title and explicit_weak) or (not from_explicit_title and pane_weak) local detected = (apps.is_root( from_command ) and from_command) or from_info or from_process or (not weak_title and apps.is_root( from_title ) and from_title) or (apps.is_root( from_title ) and from_title) or 'nu' -- Terminal/agent titles are volatile during activity. Cache one stable app label -- per tab and only upgrade from generic shell to a real root app. local cached = stable_tab_programs[id] if not cached or (detected ~= 'nu' and detected ~= cached) then cached = detected stable_tab_programs[id] = cached end local detail = title_detail( clean_explicit_title or clean_pane_title, cached, explicit_title ~= nil ) local label = tab_label( cached, detail, MAX_TAB_LABEL_WIDTH ) return label, program_icon( cached ), cached end local tab_colors = function ( tab, hover ) if tab.is_active then return palette.ui.focus, palette.content.backdrop end if hover then return palette.ui.accent, palette.content.backdrop end if tab.is_last_active then return palette.ui.backdrop, palette.ui.important_local end return palette.ui.backdrop, palette.content.normal end local tab_state_badge = function ( tab ) local badge, color = user_state_badge( tab.active_pane.user_vars ) if badge then return badge, color end badge, color = progress_badge( tab.active_pane.progress ) if badge then return badge, color end local id = tab.tab_id or tab.tab_index badge, color = activity_badge( tab.active_pane.title ) if badge then tab_activity_until[id] = now() + 2.0 return badge, color end for _, pane_info in ipairs( tab.panes or {} ) do if pane_info.has_unseen_output then tab_activity_until[id] = now() + 2.0 return busy_badge() end end if tab_activity_until[id] and tab_activity_until[id] > now() then return busy_badge() end tab_activity_until[id] = nil return nil end wez.on( 'format-tab-title', function ( tab, tabs, panes, config, hover, max_width ) if config then use_palette_for_scheme( config.color_scheme ) end local title, icon = tab_title( tab ) local state_badge, state_badge_color = tab_state_badge( tab ) local bg, fg = tab_colors( tab, hover ) local bar_bg = palette.content.backdrop local shadow = tab.is_active and palette.ui.accent or palette.content.backdrop local left_sep = wez.nerdfonts.ple_left_half_circle_thick or '\u{e0b4}' local right_sep = wez.nerdfonts.ple_right_half_circle_thick or '\u{e0b6}' local dot = tab.is_active and '●' or '○' local budget = math.max( 1, max_width - 10 ) local text = wez.truncate_right( string.format( ' %s %d %s %s', dot, tab.tab_index + 1, icon, title ), budget ) local elements = { { Background = { Color = bar_bg } }, { Foreground = { Color = shadow } }, { Text = ' ' }, { Foreground = { Color = bg } }, { Text = left_sep }, { Background = { Color = bg } }, { Foreground = { Color = fg } }, { Attribute = { Intensity = tab.is_active and 'Bold' or 'Normal' } }, { Text = text }, } fixed_badge( elements, state_badge, state_badge_color, fg ) table.insert( elements, { Text = ' ' } ) table.insert( elements, { Background = { Color = bar_bg } } ) table.insert( elements, { Foreground = { Color = bg } } ) table.insert( elements, { Text = right_sep } ) table.insert( elements, { Foreground = { Color = shadow } } ) table.insert( elements, { Text = ' ' } ) table.insert( elements, { Attribute = { Intensity = 'Normal' } } ) return elements end ) wez.on( 'format-window-title', function ( tab, pane, tabs, panes, config ) local ok, workspace = pcall( wez.mux.get_active_workspace ) workspace = ok and workspace or 'default' local title = tab and tab_title( tab ) or 'WezTerm' return '[' .. workspace .. '] ' .. title end ) -- Show which key table is active in the status area wez.on( 'update-status', function ( window, pane ) local ok_status, status_error = pcall( function () use_palette_for_window( window ) local key_table = window:active_key_table() local current_workspace = window:active_workspace() local zoomed_state_ok, zoomed_state = pcall( zoomed, pane ) zoomed_state = zoomed_state_ok and zoomed_state or nil local leader = nil if window:leader_is_active() then leader = '󱁐 ' end local pane_domain_ok, pane_domain = pcall( pane.get_domain_name, pane ) if not pane_domain_ok then return end local status = {} if zoomed_state then table.insert( status, zoomed_state ) end 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 window:set_right_status( wez.format( right_elements ) ) local battery_icons = bat.get_battery_icons() if battery_icons ~= '' then battery_icons = battery_icons .. ' ' end 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 for _, element in ipairs( domain_hud( pane_domain ) ) do table.insert( left_elements, element ) end end window:set_left_status( wez.format( left_elements ) ) end ) if not ok_status then wez.log_error( 'status render failed: ' .. tostring( status_error ) ) window:set_right_status( wez.format { { Foreground = { Color = palette.error } }, { Text = ' status error ' }, } ) end end ) return function ( cfg ) cfg.enable_tab_bar = true -- if this is hidden, we cannot see the right status are cfg.hide_tab_bar_if_only_one_tab = false cfg.use_fancy_tab_bar = false cfg.tab_max_width = 172 cfg.show_tabs_in_tab_bar = true cfg.show_new_tab_button_in_tab_bar = false cfg.tab_bar_at_bottom = true cfg.status_update_interval = 1000 end