ui(wezterm): show workspace status
0c49140c9ce36d919c0f25d503df7685f1e882e3
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 9ed9b64949df
bugabingas dorkfiles
0c49140c9ce36d919c0f25d503df7685f1e882e3
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+253 / −27 text lines
+1 / −0 text lines
+251 / −24 text lines
local icon_names = require 'bugabinga.icons'-- Detect special foreground programs via pane title-- (get_foreground_process_info returns nil on this system)local detect_program = function ( pane )local basename = function ( value ) if not value or value == '' then return nil end value = value:gsub( '\\', '/' ) return value:match( '([^/]+)$' ) or valueendlocal first_command = function ( commandline ) if not commandline or commandline == '' then return nil end local token = commandline:match( '^%s*([^%s|;&]+)' ) return basename( token )endlocal normalize_program = function ( program ) program = basename( program ) if not program then return nil end program = program:lower() if program == 'vim.opti' then return 'vim' end if program == 'node.opti' then return 'node' end if program:find( '^python' ) then return 'python' end if program == 'lazygit' then return 'git' end return programendlocal safe_user_vars = function ( pane ) local ok, vars = pcall( pane.get_user_vars, pane ) if ok and vars then return vars end return {}endlocal safe_title = function ( pane ) if ok then return title endlocal detect_program_from_title = function ( title ) if not title then return nil end local lower = title:lower() if title:find( 'π' ) or lower:find( '%f[%w]pi%f[%W]' ) then return 'pi' end for _, program in ipairs { 'nvim', 'vim', 'git', 'gh', 'yazi', 'aerc', 'weechat', 'cmus', 'wthrr', 'btm', 'top', 'nu', 'bash', } do if lower:find( program, 1, true ) then return program end end return nilendlocal detect_program = function ( pane ) local vars = safe_user_vars( pane ) local from_command = normalize_program( first_command( vars.WEZTERM_PROG ) ) if from_command then return from_command end return detect_program_from_title( safe_title( pane ) )endlocal program_icon = function ( program ) local icon_name = icon_names[program] if icon_name and wez.nerdfonts[icon_name] then return wez.nerdfonts[icon_name] end return ''endlocal short_workspace = function ( name ) local home = wez.home_dir if home and name:sub( 1, #home ) == home then name = '~' .. name:sub( #home + 1 ) end if #name > 28 then return '…/' .. (basename( name ) or name) end return nameendlocal 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 nilendlocal workspace_summaries = function () 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, programs = {} } 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 program = detect_program( info.pane ) local class = classify_workspace_program( program ) if class then summary.programs[class] = (summary.programs[class] or 0) + 1 end end end end end end end return summariesendlocal append_count = function ( parts, icon, count ) if count and count > 0 then table.insert( parts, icon .. (count > 1 and tostring( count ) or '') ) endendlocal workspace_badge = function ( summary, active ) local parts = { short_workspace( summary.name ) } 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( parts, tostring( summary.tabs ) .. 't' ) end return table.concat( parts, ' ' )endlocal 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 table.insert( elements, { Foreground = { Color = active and palette.ui.important_local or palette.ui.minor } } ) if active then table.insert( elements, { Attribute = { Intensity = 'Bold' } } ) end table.insert( elements, { Text = (active and ' ● ' or ' ○ ') .. workspace_badge( summaries[name], active ) } ) 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 return elementsendlocal tab_title = function ( tab ) if tab.tab_title and #tab.tab_title > 0 then local explicit_program = detect_program_from_title( tab.tab_title ) return tab.tab_title, program_icon( explicit_program ) end local vars = tab.active_pane.user_vars or {} local program = normalize_program( first_command( vars.WEZTERM_PROG ) ) or detect_program_from_title( tab.active_pane.title ) or normalize_program( tab.active_pane.foreground_process_name ) or 'nu' return program, program_icon( program )endwez.on( 'format-tab-title', function ( tab, tabs, panes, config, hover, max_width ) local title, icon = tab_title( tab ) local has_unseen_output = false for _, pane_info in ipairs( tab.panes or {} ) do if pane_info.has_unseen_output then has_unseen_output = true break end end local bg = palette.ui.backdrop local fg = palette.ui.normal if tab.is_active then bg = palette.ui.focus fg = palette.ui.backdrop elseif has_unseen_output then bg = palette.warning fg = palette.content.backdrop elseif hover then bg = palette.ui.accent fg = palette.ui.backdrop elseif tab.is_last_active then fg = palette.ui.important_local else fg = palette.ui.minor end local suffix = has_unseen_output and ' !' or '' local text = string.format( ' %d %s %s%s ', tab.tab_index + 1, icon, title, suffix ) text = wez.truncate_right( text, math.max( 1, max_width ) ) return { { Background = { Color = bg } }, { Foreground = { Color = fg } }, { Text = text }, }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 = pane and pane.title or tab.active_pane.title return '[' .. workspace .. '] ' .. titleend ) local zoomed_state_ok, zoomed_state = pcall(zoomed, pane) local zoomed_state_ok, zoomed_state = pcall( zoomed, pane ) local pane_domain_ok, pane_domain = pcall(pane.get_domain_name, pane) local pane_domain_ok, pane_domain = pcall( pane.get_domain_name, pane ) local os_icon = wez.target_triple:find 'windows' and ' ' or ' '+0 / −2 text lines
+1 / −1 text lines