import { hostname as osHostname } from "node:os"; import { basename } from "node:path"; import { visibleWidth } from "@earendil-works/pi-tui"; import type { BuiltinStatusLineSegmentId, RenderedSegment, SegmentContext, SemanticColor, StatusLineSegment, StatusLineSegmentId } from "./types.ts"; import { normalizeCompactExtensionStatus, normalizeExtensionStatusValue } from "./powerline-config.ts"; import { fg, rainbow, applyColor } from "./theme.ts"; import { getIcons, SEP_DOT, getThinkingText } from "./icons.ts"; import { formatUsdCost } from "./currency-rates.ts"; import { getGitRemoteHost } from "./git-status.ts"; import type { IconSet } from "./icons.ts"; import type { GitHost } from "./git-status.ts"; function color(ctx: SegmentContext, semantic: SemanticColor, text: string): string { return fg(ctx.theme, semantic, text, ctx.colors); } // ═══════════════════════════════════════════════════════════════════════════ // Helpers // ═══════════════════════════════════════════════════════════════════════════ function withIcon(icon: string, text: string): string { return icon ? `${icon} ${text}` : text; } function formatTokens(n: number): string { if (n < 1000) return n.toString(); if (n < 10000) return `${(n / 1000).toFixed(1)}k`; if (n < 1000000) return `${Math.round(n / 1000)}k`; if (n < 10000000) return `${(n / 1000000).toFixed(1)}M`; return `${Math.round(n / 1000000)}M`; } function formatDuration(ms: number): string { const seconds = Math.floor(ms / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); if (hours > 0) return `${hours}h${minutes % 60}m`; if (minutes > 0) return `${minutes}m${seconds % 60}s`; return `${seconds}s`; } // ═══════════════════════════════════════════════════════════════════════════ // Segment Implementations // ═══════════════════════════════════════════════════════════════════════════ const modelSegment: StatusLineSegment = { id: "model", render(ctx) { const icons = getIcons(); const opts = ctx.options.model ?? {}; let modelName = ctx.model?.name || ctx.model?.id || "no-model"; if (opts.display === "qualified" && ctx.model?.id) { const provider = ctx.model.provider || ctx.model.providerId || ctx.model.providerName; modelName = provider && !ctx.model.id.includes("/") ? `${provider}/${ctx.model.id}` : ctx.model.id; } else if (modelName.startsWith("Claude ")) { modelName = modelName.slice(7); } let content = withIcon(icons.model, modelName); if (opts.showThinkingLevel !== false && ctx.model?.reasoning) { const level = ctx.thinkingLevel || "off"; if (level !== "off") { const thinkingText = getThinkingText(level); if (thinkingText) { content += `${SEP_DOT}${thinkingText}`; } } } return { content: color(ctx, "model", content), visible: true }; }, }; const shellModeSegment: StatusLineSegment = { id: "shell_mode", render(ctx) { if (!ctx.shellModeActive) { return { content: "", visible: false }; } const shellName = ctx.shellName ?? "shell"; const state = ctx.shellRunning ? "run" : "idle"; const cwd = ctx.shellCwd ? basename(ctx.shellCwd) : null; const parts = [shellName, state]; if (cwd) { parts.push(cwd); } return { content: color(ctx, "shellMode", parts.join(SEP_DOT)), visible: true }; }, }; const pathSegment: StatusLineSegment = { id: "path", render(ctx) { const icons = getIcons(); const opts = ctx.options.path ?? {}; const mode = opts.mode ?? "basename"; let pwd = ctx.shellModeActive && ctx.shellCwd ? ctx.shellCwd : (ctx.cwd ?? process.cwd()); const home = process.env.HOME || process.env.USERPROFILE; if (mode === "basename") { // Just the last directory component (cross-platform) pwd = basename(pwd) || pwd; } else { // Abbreviate home directory for abbreviated/full modes if (home && pwd.startsWith(home)) { pwd = `~${pwd.slice(home.length)}`; } // Strip /work/ prefix (common in containers) if (pwd.startsWith("/work/")) { pwd = pwd.slice(6); } // Truncate if too long (only for abbreviated mode) if (mode === "abbreviated") { const maxLen = opts.maxLength ?? 40; if (pwd.length > maxLen) { pwd = `…${pwd.slice(-(maxLen - 1))}`; } } } const content = withIcon(icons.folder, pwd); return { content: color(ctx, "path", content), visible: true }; }, }; /** * Icon for the branch label: the origin remote's host logo when hostIcon is * enabled and a remote is known, otherwise the plain branch icon. An * unrecognized remote falls back to the generic git logo. */ function resolveBranchIcon(icons: IconSet, hostIcon: boolean): string { if (!hostIcon) return icons.branch; const host = getGitRemoteHost(); const byHost: Record = { github: icons.github, gitlab: icons.gitlab, bitbucket: icons.bitbucket, other: icons.git, }; return host ? byHost[host] : icons.branch; } const gitSegment: StatusLineSegment = { id: "git", render(ctx) { const icons = getIcons(); const opts = ctx.options.git ?? {}; const { branch, staged, unstaged, untracked } = ctx.git; const gitStatus = (staged > 0 || unstaged > 0 || untracked > 0) ? { staged, unstaged, untracked } : null; if (!branch && !gitStatus) return { content: "", visible: false }; const isDirty = gitStatus && (gitStatus.staged > 0 || gitStatus.unstaged > 0 || gitStatus.untracked > 0); const showBranch = opts.showBranch !== false; const branchColor: SemanticColor = isDirty ? "gitDirty" : "gitClean"; // Build content - color branch separately from indicators let content = ""; if (showBranch && branch) { // Color just the branch name (icon + branch text) const branchIcon = resolveBranchIcon(icons, opts.hostIcon === true); content = color(ctx, branchColor, withIcon(branchIcon, branch)); } // Add status indicators (each with their own color, not wrapped) if (gitStatus) { const indicators: string[] = []; if (opts.showUnstaged !== false && gitStatus.unstaged > 0) { indicators.push(applyColor(ctx.theme, "warning", `*${gitStatus.unstaged}`)); } if (opts.showStaged !== false && gitStatus.staged > 0) { indicators.push(applyColor(ctx.theme, "success", `+${gitStatus.staged}`)); } if (opts.showUntracked !== false && gitStatus.untracked > 0) { indicators.push(applyColor(ctx.theme, "muted", `?${gitStatus.untracked}`)); } if (indicators.length > 0) { const indicatorText = indicators.join(" "); if (!content && showBranch === false) { // No branch shown, color the git icon with branch color content = color(ctx, branchColor, icons.git ? `${icons.git} ` : "") + indicatorText; } else { content += content ? ` ${indicatorText}` : indicatorText; } } } if (!content) return { content: "", visible: false }; return { content, visible: true }; }, }; const thinkingSegment: StatusLineSegment = { id: "thinking", render(ctx) { const level = ctx.thinkingLevel || "off"; const levelText: Record = { off: "off", minimal: "min", low: "low", medium: "med", high: "high", xhigh: "xhigh", }; const label = levelText[level] || level; const content = `think:${label}`; if (level === "high" || level === "xhigh" || level === "max") { return { content: rainbow(content), visible: true }; } if (level === "minimal") { return { content: color(ctx, "thinkingMinimal", content), visible: true }; } if (level === "low") { return { content: color(ctx, "thinkingLow", content), visible: true }; } if (level === "medium") { return { content: color(ctx, "thinkingMedium", content), visible: true }; } return { content: color(ctx, "thinking", content), visible: true }; }, }; const subagentsSegment: StatusLineSegment = { id: "subagents", render() { // Note: pi-mono doesn't have subagent tracking built-in // This would require extension state management // For now, return not visible return { content: "", visible: false }; }, }; const queueSegment: StatusLineSegment = { id: "queue", render(ctx) { const summary = ctx.queueSummary; const parts: string[] = []; if (summary.compacting && summary.queueCount > 0) { parts.push(`compact q ${summary.queueCount}`); } else if (summary.queueCount > 0) { parts.push(`q ${summary.queueCount}`); } if (summary.ideaCount > 0) { parts.push(`ideas ${summary.ideaCount}`); } if (summary.blockedCount > 0) { parts.push(`blocked ${summary.blockedCount}`); } if (parts.length === 0) return { content: "", visible: false }; return { content: color(ctx, "queue", parts.join(SEP_DOT)), visible: true }; }, }; const tokenInSegment: StatusLineSegment = { id: "token_in", render(ctx) { const icons = getIcons(); const { input } = ctx.usageStats; if (!input) return { content: "", visible: false }; const content = withIcon(icons.input, formatTokens(input)); return { content: color(ctx, "tokens", content), visible: true }; }, }; const tokenOutSegment: StatusLineSegment = { id: "token_out", render(ctx) { const icons = getIcons(); const { output } = ctx.usageStats; if (!output) return { content: "", visible: false }; const content = withIcon(icons.output, formatTokens(output)); return { content: color(ctx, "tokens", content), visible: true }; }, }; const tokenTotalSegment: StatusLineSegment = { id: "token_total", render(ctx) { const icons = getIcons(); const { input, output, cacheRead, cacheWrite } = ctx.usageStats; const total = input + output + cacheRead + cacheWrite; if (!total) return { content: "", visible: false }; const content = withIcon(icons.tokens, formatTokens(total)); return { content: color(ctx, "tokens", content), visible: true }; }, }; const costSegment: StatusLineSegment = { id: "cost", render(ctx) { const cost = ctx.usageStats.cost + (ctx.usageStats.subagentCost ?? 0); const usingSubscription = ctx.usingSubscription; if (!cost && !usingSubscription) { return { content: "", visible: false }; } const reportedCost = cost > 0 ? formatUsdCost(cost, ctx.options.cost?.currency) : null; if (!usingSubscription) { return reportedCost ? { content: color(ctx, "cost", reportedCost), visible: true } : { content: "", visible: false }; } const subscriptionDisplay = ctx.options.cost?.subscriptionDisplay ?? "subscription"; if (subscriptionDisplay === "reported-cost" && reportedCost) { return { content: color(ctx, "cost", reportedCost), visible: true }; } if (subscriptionDisplay === "both" && reportedCost) { return { content: color(ctx, "cost", `${reportedCost} (sub)`), visible: true }; } return { content: color(ctx, "cost", "(sub)"), visible: true }; }, }; const contextPctSegment: StatusLineSegment = { id: "context_pct", render(ctx) { if (ctx.customCompactionEnabled) return { content: "", visible: false }; const icons = getIcons(); const { contextTokens, contextPercent, contextWindow } = ctx; const autoIcon = ctx.autoCompactEnabled && icons.auto ? ` ${icons.auto}` : ""; const percentOnly = ctx.options.context?.format === "percent"; const hasKnownUsage = contextTokens !== null && contextPercent !== null; const approximate = ctx.contextApproximate ? "~" : ""; // "full" (default): tokens/window + one-decimal percentage + auto-compact icon. // "percent": bare rounded percentage, threshold-colored, no icons. const text = percentOnly ? (hasKnownUsage ? `${approximate}${Math.round(contextPercent)}%` : "?") : hasKnownUsage ? `${approximate}${formatTokens(contextTokens)}/${formatTokens(contextWindow)} (${contextPercent.toFixed(1)}%)${autoIcon}` : `?/${formatTokens(contextWindow)}${autoIcon}`; // Icon outside color, text inside - use semantic colors for thresholds let content: string; const colored = (semantic: "context" | "contextWarn" | "contextError") => percentOnly ? color(ctx, semantic, text) : withIcon(icons.context, color(ctx, semantic, text)); if (hasKnownUsage && contextPercent > 90) { content = colored("contextError"); } else if (hasKnownUsage && contextPercent > 70) { content = colored("contextWarn"); } else { content = colored("context"); } return { content, visible: true }; }, }; const contextTotalSegment: StatusLineSegment = { id: "context_total", render(ctx) { if (ctx.customCompactionEnabled) return { content: "", visible: false }; const icons = getIcons(); const window = ctx.contextWindow; if (!window) return { content: "", visible: false }; return { content: color(ctx, "context", withIcon(icons.context, formatTokens(window))), visible: true, }; }, }; const timeSpentSegment: StatusLineSegment = { id: "time_spent", render(ctx) { const icons = getIcons(); const elapsed = Date.now() - ctx.sessionStartTime; if (elapsed < 1000) return { content: "", visible: false }; return { content: withIcon(icons.time, formatDuration(elapsed)), visible: true }; }, }; const timeSegment: StatusLineSegment = { id: "time", render(ctx) { const icons = getIcons(); const opts = ctx.options.time ?? {}; const now = new Date(); let hours = now.getHours(); let suffix = ""; if (opts.format === "12h") { suffix = hours >= 12 ? "pm" : "am"; hours = hours % 12 || 12; } const mins = now.getMinutes().toString().padStart(2, "0"); let timeStr = `${hours}:${mins}`; if (opts.showSeconds) { timeStr += `:${now.getSeconds().toString().padStart(2, "0")}`; } timeStr += suffix; return { content: withIcon(icons.time, timeStr), visible: true }; }, }; const sessionSegment: StatusLineSegment = { id: "session", render(ctx) { const icons = getIcons(); const sessionId = ctx.sessionId; const display = sessionId?.slice(0, 8) || "new"; return { content: withIcon(icons.session, display), visible: true }; }, }; const hostnameSegment: StatusLineSegment = { id: "hostname", render() { const icons = getIcons(); const name = osHostname().split(".")[0]; return { content: withIcon(icons.host, name), visible: true }; }, }; const cacheReadSegment: StatusLineSegment = { id: "cache_read", render(ctx) { const icons = getIcons(); const { cacheRead, input } = ctx.usageStats; if (!cacheRead) return { content: "", visible: false }; const format = ctx.options.cache_read?.format ?? "tokens"; const hitRate = input + cacheRead > 0 ? ((cacheRead / (input + cacheRead)) * 100).toFixed(0) : "0"; let content: string; if (format === "percent") { content = [icons.cache, `${hitRate}%`].filter(Boolean).join(" "); } else { const tokens = [icons.cache, icons.input, formatTokens(cacheRead)].filter(Boolean).join(" "); content = format === "both" ? `${tokens} (${hitRate}%)` : tokens; } return { content: color(ctx, "tokens", content), visible: true }; }, }; const cacheWriteSegment: StatusLineSegment = { id: "cache_write", render(ctx) { const icons = getIcons(); const { cacheWrite } = ctx.usageStats; if (!cacheWrite) return { content: "", visible: false }; const parts = [icons.cache, icons.output, formatTokens(cacheWrite)].filter(Boolean); const content = parts.join(" "); return { content: color(ctx, "tokens", content), visible: true }; }, }; const extensionStatusesSegment: StatusLineSegment = { id: "extension_statuses", render(ctx) { const statuses = ctx.extensionStatuses; if (!statuses || statuses.size === 0) return { content: "", visible: false }; // Join compact statuses with a separator // Skip: empty strings, notification-style ("[...") shown above editor, // and strings that are only ANSI codes with no visible text. // Also skip statuses explicitly elevated into dedicated custom segments. const parts: string[] = []; for (const [statusKey, value] of statuses.entries()) { if (ctx.hiddenExtensionStatusKeys.has(statusKey)) continue; const normalized = value ? normalizeCompactExtensionStatus(value) : null; if (normalized) { parts.push(normalized); } } if (parts.length === 0) return { content: "", visible: false }; // Statuses already have their own styling applied by the extensions const content = parts.join(` ${SEP_DOT} `); return { content, visible: true }; }, }; // ═══════════════════════════════════════════════════════════════════════════ // Segment Registry // ═══════════════════════════════════════════════════════════════════════════ export const SEGMENTS: Record = { model: modelSegment, shell_mode: shellModeSegment, path: pathSegment, git: gitSegment, thinking: thinkingSegment, subagents: subagentsSegment, queue: queueSegment, token_in: tokenInSegment, token_out: tokenOutSegment, token_total: tokenTotalSegment, cost: costSegment, context_pct: contextPctSegment, context_total: contextTotalSegment, time_spent: timeSpentSegment, time: timeSegment, session: sessionSegment, hostname: hostnameSegment, cache_read: cacheReadSegment, cache_write: cacheWriteSegment, extension_statuses: extensionStatusesSegment, }; function renderCustomSegment(id: `custom:${string}`, ctx: SegmentContext): RenderedSegment { const customItemId = id.slice("custom:".length); const custom = ctx.customItemsById.get(customItemId); if (!custom) return { content: "", visible: false }; const rawStatus = ctx.extensionStatuses.get(custom.statusKey); const normalizedStatus = rawStatus ? normalizeExtensionStatusValue(rawStatus) : null; if (!normalizedStatus) { return custom.hideWhenMissing ? { content: "", visible: false } : { content: custom.prefix ?? custom.id, visible: true }; } let content = normalizedStatus; if (custom.prefix) { content = `${custom.prefix}${SEP_DOT}${content}`; } if (custom.color) { content = applyColor(ctx.theme, custom.color, content); } return { content, visible: true }; } function isCustomSegmentId(id: StatusLineSegmentId): id is `custom:${string}` { return id.startsWith("custom:"); } export function renderSegment(id: StatusLineSegmentId, ctx: SegmentContext): RenderedSegment { if (isCustomSegmentId(id)) { return renderCustomSegment(id, ctx); } const segment = SEGMENTS[id]; if (!segment) { return { content: "", visible: false }; } return segment.render(ctx); }