repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
wezterm/bugabinga/status.lua
Rawlocal 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 FALLBACK_TAB_PROGRAM = 'nu'
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 nerd_icon = function ( fallback, ... )
for _, name in ipairs { ... } do
if wez.nerdfonts[name] then return wez.nerdfonts[name] end
end
return fallback
end
local host_domain = function ()
local triple = wez.target_triple or ''
if triple:find( 'windows', 1, true ) then
return 'WIN', nerd_icon( '□', 'dev_windows', 'md_microsoft_windows', 'cod_terminal_powershell' ), palette.warning
elseif triple:find( 'darwin', 1, true ) then
return 'MAC', nerd_icon( '□', 'dev_apple', 'fa_apple' ), palette.ui.important_local
elseif triple:find( 'linux', 1, true ) then
return 'LIN', nerd_icon( '□', 'dev_linux', 'cod_terminal_linux' ), palette.ui.important_local
elseif triple:find( 'bsd', 1, true ) then
return 'BSD', nerd_icon( '□', 'dev_freebsd' ), palette.ui.important_local
end
return 'LOC', nerd_icon( '□', 'cod_terminal' ), palette.ui.important_local
end
local domain_hud = function ( domain )
local label, icon, color
if not domain or domain == 'local' or domain == 'Local' then
label, icon, color = host_domain()
elseif domain:find( '^WSL:', 1, false ) then
label = 'WSL'
icon = nerd_icon( '□', 'dev_linux', 'cod_terminal_linux' )
color = palette.ui.important_local
elseif domain:find( '^scope:', 1, false ) then
label = 'EXE'
icon = nerd_icon( '□', 'cod_gear', 'md_cog' )
color = palette.warning
else
label = ( domain:upper():gsub( '[^%w]', '' ):sub( 1, 3 ) .. 'LOC' ):sub( 1, 3 )
icon = nerd_icon( '□', 'cod_terminal' )
color = palette.ui.important_local
end
return {
{ Foreground = { Color = color } },
{ Attribute = { Intensity = 'Bold' } },
{ Text = icon .. ' ' .. label .. ' ' },
{ Attribute = { Intensity = 'Normal' } },
}
end
local root_or_nil = function ( program )
program = apps.normalize( program )
if apps.is_root( program ) then return program end
return nil
end
local tab_label = function ( program, detail )
program = root_or_nil( program ) or FALLBACK_TAB_PROGRAM
local base = apps.label( program ) or apps.label( FALLBACK_TAB_PROGRAM )
return detail and (base .. ' · ' .. detail) or base
end
local title_program = function ( title )
title = strip_activity_marker( title )
if not title then return nil end
if title:sub( 1, #'π' ) == 'π' then return 'pi' end
-- Titles are prose while agents run. Trust only a leading app token; never
-- classify from words inside task text like "Fix Claude settings".
return root_or_nil( title:match( '^([%w%._%-]+)' ) )
end
local title_detail = function ( title, program, include_shell_titles )
title = strip_activity_marker( title )
if not title then return nil end
local detail = apps.title_detail( title, program, include_shell_titles ) or title
detail = strip_activity_marker( 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
for _, key in ipairs { 'name', 'executable' } do
detected = detected or root_or_nil( node[key] )
end
if type( node.argv ) == 'table' then
for _, arg in ipairs( node.argv ) do
detected = detected or root_or_nil( arg )
end
end
if detected then return 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 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
return detect_from_process_info( info )
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 display_title = explicit_title or pane_title
local vars = tab.active_pane.user_vars or {}
local detected = root_or_nil( apps.detect_from_commandline( vars.WEZTERM_PROG ) )
or process_info_program( tab )
or root_or_nil( tab.active_pane.foreground_process_name )
or title_program( display_title )
or stable_tab_programs[id]
or FALLBACK_TAB_PROGRAM
if detected ~= FALLBACK_TAB_PROGRAM then stable_tab_programs[id] = detected end
local detail = title_detail( display_title, detected, explicit_title ~= nil )
return tab_label( detected, detail ), program_icon( detected ), detected
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 chrome_width = 8 -- separators, padding, fixed state-badge slot
local budget = math.max( 1, max_width - chrome_width )
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 = {}
if key_table or leader then
local mode_text = key_table or 'LEADER'
right_elements = {
{ Background = { Color = palette.error } },
{ Foreground = { Color = palette.ui.backdrop } },
{ Attribute = { Intensity = 'Bold' } },
{ Text = ' ' .. mode_text .. ' ' },
'ResetAttributes',
{ Text = ' ' },
}
end
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 = {
{ 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
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