package.loaded['bugabinga.icons'] = nil local icon_names = require 'bugabinga.icons' local M = {} local DEFAULT_ICON = '' local SHELLS = { bash = true, fish = true, nu = true, nushell = true, sh = true, zsh = true, } local ROOT_APPS = { aerc = true, btm = true, btop = true, claude = true, cmus = true, codex = true, helix = true, htop = true, kak = true, lf = true, mc = true, nano = true, neovide = true, nvim = true, opencode = true, pi = true, pwsh = true, ranger = true, ssh = true, top = true, vifm = true, vim = true, weechat = true, wsl = true, yazi = true, youtui = true, glow = true, gitu = true, presenterm = true, } local QUIET_UNSEEN = { aerc = true, claude = true, cmus = true, codex = true, nvim = true, opencode = true, pi = true, vim = true, weechat = true, yazi = true, glow = true, gitu = true, presenterm = true, youtui = true, } local LABELS = { aerc = 'Aerc', claude = 'Claude', cmus = 'Cmus', codex = 'Codex', d2 = 'D2', fd = 'fd', gh = 'GH', gitu = 'Gitu', glow = 'Glow', jar = 'JAR', javac = 'JavaC', jconsole = 'JConsole', jdb = 'JDB', jdeps = 'JDeps', jj = 'JJ', jq = 'jq', jps = 'JPS', jshell = 'JShell', mvn = 'Maven', mvnd = 'Maven', neovide = 'Neovide', nvim = 'Neovim', nu = 'Nu', opencode = 'OpenCode', pi = 'Pi', presenterm = 'Presenterm', pwsh = 'PowerShell', rg = 'rg', sd = 'sd', ssh = 'SSH', svn = 'SVN', tofu = 'OpenTofu', vim = 'Vim', ['vfp9.exe'] = 'VFP9', wsl = 'WSL', yazi = 'Yazi', } local TITLE_ORDER = { 'codex', 'claude', 'opencode', 'pi', 'nvim', 'neovide', 'vim', 'helix', 'kak', 'nano', 'gitu', 'yazi', 'ranger', 'lf', 'vifm', 'mc', 'aerc', 'weechat', 'cmus', 'youtui', 'btm', 'btop', 'htop', 'top', 'presenterm', 'glow', 'ssh', 'wsl', 'pwsh', } local CLAUDE_ACTIVITY_MARKERS = { '⠁', '⠂', '⠄', '⠆', '⠇', '⠋', '⠙', '⠸', '⠼', '⠴', '⠦', '⠧', '⠐', '✳', '✢', '✣', '✤', '✥', '✦', '✧', '✶', '✷', '✸', '✹', '✺', '✻', '✼', '✽', '✾', '✿', } local function basename(value) if not value or value == '' then return nil end value = value:gsub('\\', '/') return value:match('([^/]+)$') or value end function M.clean_title(title) if not title or title == '' then return nil end title = title:gsub('^%s+', ''):gsub('%s+$', '') if title == '' then return nil end return title end function M.first_command(commandline) if not commandline or commandline == '' then return nil end return basename(commandline:match('^%s*([^%s|;&]+)')) end function M.detect_from_commandline(commandline) commandline = M.clean_title(commandline) if not commandline then return nil end -- Command lines are arbitrary user text. Trust only argv[0]; never classify -- from arguments like `rg claude` or task text like `Fix Claude settings`. return M.normalize(M.first_command(commandline)) end function M.normalize(program) program = basename(program) if not program then return nil end program = program:lower() if program == 'vfp9.exe' then return program end program = program:gsub('%.exe$', ''):gsub('%.cmd$', ''):gsub('%.bat$', ''):gsub('%.ps1$', '') if program:find('^claude') then return 'claude' end if program:find('^codex') then return 'codex' end if program:find('^opencode') then return 'opencode' end if program:find('^pi%-') then return 'pi' end if program:find('^python') then return 'python' end if program == 'g' or program == 'lazygit' then return 'git' end if program == 'node.opti' then return 'node' end if program == 'pia' or program == 'pic' or program == 'pii' then return 'pi' end if program == 'vide' then return 'neovide' end if program == 'vim.opti' then return 'vim' end if program == 'ya' or program == 'yy' then return 'yazi' end return program end local function escape_pattern(value) return (value:gsub('([^%w])', '%%%1')) end local function starts_with(value, prefix) return value:sub(1, #prefix) == prefix end local function title_contains_program(lower_title, program) return lower_title:find('%f[%w]' .. escape_pattern(program) .. '%f[%W]') ~= nil end function M.strip_claude_activity_marker(title) title = M.clean_title(title) if not title then return nil end for _, marker in ipairs(CLAUDE_ACTIVITY_MARKERS) do if title:sub(1, #marker) == marker then return M.clean_title(title:sub(#marker + 1)) or title end end return nil end function M.detect_from_title(title) title = M.clean_title(title) if not title then return nil, false end local lower = title:lower() for _, program in ipairs(TITLE_ORDER) do if title_contains_program(lower, program) then return M.normalize(program), false end end -- UTF-8 safe: Lua pattern frontiers are byte-oriented and made Pi flaky. -- Keep after explicit app matches so Pi-hosted Codex/Claude titles render as Codex/Claude. if starts_with(title, 'π') then return 'pi', false end local stripped = M.strip_claude_activity_marker(title) if stripped then local stripped_lower = stripped:lower() for _, program in ipairs(TITLE_ORDER) do if title_contains_program(stripped_lower, program) then return M.normalize(program), true end end -- These are Claude-specific activity markers. Other agent spinners (Codex/Pi) -- are stripped in status.lua, not here, so fallback to Claude is safe here. return 'claude', true end return nil, false end function M.is_shell(program) program = M.normalize(program) return program ~= nil and SHELLS[program] == true end function M.is_root(program) program = M.normalize(program) return program ~= nil and ROOT_APPS[program] == true end function M.quiet_unseen(program) program = M.normalize(program) return program ~= nil and QUIET_UNSEEN[program] == true end function M.label(program) program = M.normalize(program) if not program then return nil end return LABELS[program] or program:gsub('%.exe$', ''):gsub('^%l', string.upper) end function M.icon_name(program) program = M.normalize(program) if not program then return nil end return icon_names[program] end function M.icon(program, nerdfonts) local icon_name = M.icon_name(program) if icon_name and nerdfonts and nerdfonts[icon_name] then return nerdfonts[icon_name] end return DEFAULT_ICON end function M.title_detail(title, program, include_shell_titles) title = M.clean_title(title) program = M.normalize(program) if not title then return nil end if not program then return title end title = M.strip_claude_activity_marker(title) or title if program == 'pi' and starts_with(title, 'π') then title = M.clean_title(title:sub(#'π' + 1):gsub('^%s*[-–—:]%s*', '')) or title -- Pi titles are usually "π - task - workspace/app". The fake tab already -- renders the app label, so drop suffixes that merely repeat the task/app. title = M.clean_title(title:gsub('%s*[-:]%s*[Pp][Ii]%s*$', '')) or title local head, tail = title:match('^(.*)%s+%-%s+(.+)$') if head and tail then local compact_head = head:lower():gsub('[^%w]', '') local compact_tail = tail:lower():gsub('[^%w]', '') local tail_is_repeat = compact_tail ~= '' and (compact_head == compact_tail or compact_head:sub(1, #compact_tail) == compact_tail or compact_head:sub(-#compact_tail) == compact_tail) if tail_is_repeat then title = M.clean_title(head) or title end end end if M.normalize(title) == program then return nil end if M.is_shell(program) and not include_shell_titles then return nil end local display = M.label(program) or program local lower = title:lower() if lower == display:lower() then return nil end local escaped_program = escape_pattern(program) local escaped_display = escape_pattern(display:lower()) local detail = lower:match('^%s*' .. escaped_program .. '%s*[:%-%|—]+%s*(.+)$') or lower:match('^%s*' .. escaped_program .. '%s+(.+)$') or lower:match('^%s*' .. escaped_display .. '%s*[:%-%|—]+%s*(.+)$') or lower:match('^%s*' .. escaped_display .. '%s+(.+)$') if not detail then return title end local start_index = #title - #detail + 1 return M.clean_title(title:sub(start_index)) end return M