import { isLimitReached } from '../types.js'; import { getContextPercent, getBufferedPercent, getModelName, getProviderLabel, getTotalTokens } from '../stdin.js'; import { getOutputSpeed } from '../speed-tracker.js'; import { coloredBar, critical, cyan, dim, magenta, red, warning, yellow, getContextColor, getQuotaColor, quotaBar, claudeOrange, RESET } from './colors.js'; import { getAdaptiveBarWidth } from '../utils/terminal.js'; const DEBUG = process.env.DEBUG?.includes('claude-hud') || process.env.DEBUG === '*'; /** * Renders the full session line (model + context bar + project + git + counts + usage + duration). * Used for compact layout mode. */ export function renderSessionLine(ctx) { const model = getModelName(ctx.stdin); const rawPercent = getContextPercent(ctx.stdin); const bufferedPercent = getBufferedPercent(ctx.stdin); const autocompactMode = ctx.config?.display?.autocompactBuffer ?? 'enabled'; const percent = autocompactMode === 'disabled' ? rawPercent : bufferedPercent; if (DEBUG && autocompactMode === 'disabled') { console.error(`[claude-hud:context] autocompactBuffer=disabled, showing raw ${rawPercent}% (buffered would be ${bufferedPercent}%)`); } const colors = ctx.config?.colors; const barWidth = getAdaptiveBarWidth(); const bar = coloredBar(percent, barWidth, colors); const parts = []; const display = ctx.config?.display; const contextValueMode = display?.contextValue ?? 'percent'; const contextValue = formatContextValue(ctx, percent, contextValueMode); const contextValueDisplay = `${getContextColor(percent, colors)}${contextValue}${RESET}`; // Model and context bar (FIRST) // Plan name only shows if showUsage is enabled (respects hybrid toggle) const providerLabel = getProviderLabel(ctx.stdin); const showUsage = display?.showUsage !== false; const planName = showUsage ? ctx.usageData?.planName : undefined; const hasApiKey = !!process.env.ANTHROPIC_API_KEY; const billingLabel = showUsage ? (planName ?? (hasApiKey ? red('API') : undefined)) : undefined; const planDisplay = providerLabel ?? billingLabel; const modelDisplay = planDisplay ? `${model} | ${planDisplay}` : model; if (display?.showModel !== false && display?.showContextBar !== false) { parts.push(`${cyan(`[${modelDisplay}]`)} ${bar} ${contextValueDisplay}`); } else if (display?.showModel !== false) { parts.push(`${cyan(`[${modelDisplay}]`)} ${contextValueDisplay}`); } else if (display?.showContextBar !== false) { parts.push(`${bar} ${contextValueDisplay}`); } else { parts.push(contextValueDisplay); } // Project path + git status (SECOND) let projectPart = null; if (display?.showProject !== false && ctx.stdin.cwd) { // Split by both Unix (/) and Windows (\) separators for cross-platform support const segments = ctx.stdin.cwd.split(/[/\\]/).filter(Boolean); const pathLevels = ctx.config?.pathLevels ?? 1; // Always join with forward slash for consistent display // Handle root path (/) which results in empty segments const projectPath = segments.length > 0 ? segments.slice(-pathLevels).join('/') : '/'; projectPart = yellow(projectPath); } let gitPart = ''; const gitConfig = ctx.config?.gitStatus; const showGit = gitConfig?.enabled ?? true; if (showGit && ctx.gitStatus) { const gitParts = [ctx.gitStatus.branch]; // Show dirty indicator if ((gitConfig?.showDirty ?? true) && ctx.gitStatus.isDirty) { gitParts.push('*'); } // Show ahead/behind (with space separator for readability) if (gitConfig?.showAheadBehind) { if (ctx.gitStatus.ahead > 0) { gitParts.push(` ↑${ctx.gitStatus.ahead}`); } if (ctx.gitStatus.behind > 0) { gitParts.push(` ↓${ctx.gitStatus.behind}`); } } // Show file stats in Starship-compatible format (!modified +added ✘deleted ?untracked) if (gitConfig?.showFileStats && ctx.gitStatus.fileStats) { const { modified, added, deleted, untracked } = ctx.gitStatus.fileStats; const statParts = []; if (modified > 0) statParts.push(`!${modified}`); if (added > 0) statParts.push(`+${added}`); if (deleted > 0) statParts.push(`✘${deleted}`); if (untracked > 0) statParts.push(`?${untracked}`); if (statParts.length > 0) { gitParts.push(` ${statParts.join(' ')}`); } } gitPart = `${magenta('git:(')}${cyan(gitParts.join(''))}${magenta(')')}`; } if (projectPart && gitPart) { parts.push(`${projectPart} ${gitPart}`); } else if (projectPart) { parts.push(projectPart); } else if (gitPart) { parts.push(gitPart); } // Session name (custom title from /rename, or auto-generated slug) if (display?.showSessionName && ctx.transcript.sessionName) { parts.push(dim(ctx.transcript.sessionName)); } // Config counts (respects environmentThreshold) if (display?.showConfigCounts !== false) { const totalCounts = ctx.claudeMdCount + ctx.rulesCount + ctx.mcpCount + ctx.hooksCount; const envThreshold = display?.environmentThreshold ?? 0; if (totalCounts > 0 && totalCounts >= envThreshold) { if (ctx.claudeMdCount > 0) { parts.push(dim(`${ctx.claudeMdCount} CLAUDE.md`)); } if (ctx.rulesCount > 0) { parts.push(dim(`${ctx.rulesCount} rules`)); } if (ctx.mcpCount > 0) { parts.push(dim(`${ctx.mcpCount} MCPs`)); } if (ctx.hooksCount > 0) { parts.push(dim(`${ctx.hooksCount} hooks`)); } } } // Usage limits display (shown when enabled in config, respects usageThreshold) if (display?.showUsage !== false && ctx.usageData?.planName && !providerLabel) { if (ctx.usageData.apiUnavailable) { const errorHint = formatUsageError(ctx.usageData.apiError); parts.push(warning(`usage: ⚠${errorHint}`, colors)); } else if (isLimitReached(ctx.usageData)) { const resetTime = ctx.usageData.fiveHour === 100 ? formatResetTime(ctx.usageData.fiveHourResetAt) : formatResetTime(ctx.usageData.sevenDayResetAt); parts.push(critical(`⚠ Limit reached${resetTime ? ` (resets ${resetTime})` : ''}`, colors)); } else { const usageThreshold = display?.usageThreshold ?? 0; const fiveHour = ctx.usageData.fiveHour; const sevenDay = ctx.usageData.sevenDay; const effectiveUsage = Math.max(fiveHour ?? 0, sevenDay ?? 0); if (effectiveUsage >= usageThreshold) { const syncingSuffix = ctx.usageData.apiError === 'rate-limited' ? ` ${dim('(syncing...)')}` : ''; const fiveHourDisplay = formatUsagePercent(fiveHour, colors); const fiveHourReset = formatResetTime(ctx.usageData.fiveHourResetAt); const usageBarEnabled = display?.usageBarEnabled ?? true; const fiveHourPart = usageBarEnabled ? (fiveHourReset ? `${quotaBar(fiveHour ?? 0, barWidth, colors)} ${fiveHourDisplay} (${fiveHourReset} / 5h)` : `${quotaBar(fiveHour ?? 0, barWidth, colors)} ${fiveHourDisplay}`) : (fiveHourReset ? `5h: ${fiveHourDisplay} (${fiveHourReset})` : `5h: ${fiveHourDisplay}`); const sevenDayThreshold = display?.sevenDayThreshold ?? 80; if (sevenDay !== null && sevenDay >= sevenDayThreshold) { const sevenDayDisplay = formatUsagePercent(sevenDay, colors); const sevenDayReset = formatResetTime(ctx.usageData.sevenDayResetAt); const sevenDayPart = usageBarEnabled ? (sevenDayReset ? `${quotaBar(sevenDay, barWidth, colors)} ${sevenDayDisplay} (${sevenDayReset} / 7d)` : `${quotaBar(sevenDay, barWidth, colors)} ${sevenDayDisplay}`) : (sevenDayReset ? `7d: ${sevenDayDisplay} (${sevenDayReset})` : `7d: ${sevenDayDisplay}`); parts.push(`${fiveHourPart} | ${sevenDayPart}${syncingSuffix}`); } else { parts.push(`${fiveHourPart}${syncingSuffix}`); } } } } // Session duration if (display?.showSpeed) { const speed = getOutputSpeed(ctx.stdin); if (speed !== null) { parts.push(dim(`out: ${speed.toFixed(1)} tok/s`)); } } if (display?.showDuration !== false && ctx.sessionDuration) { parts.push(dim(`⏱️ ${ctx.sessionDuration}`)); } if (ctx.extraLabel) { parts.push(dim(ctx.extraLabel)); } // Custom line (static user-defined text) const customLine = display?.customLine; if (customLine) { parts.push(claudeOrange(customLine)); } let line = parts.join(' | '); // Token breakdown at high context if (display?.showTokenBreakdown !== false && percent >= 85) { const usage = ctx.stdin.context_window?.current_usage; if (usage) { const input = formatTokens(usage.input_tokens ?? 0); const cache = formatTokens((usage.cache_creation_input_tokens ?? 0) + (usage.cache_read_input_tokens ?? 0)); line += dim(` (in: ${input}, cache: ${cache})`); } } return line; } function formatTokens(n) { if (n >= 1000000) { return `${(n / 1000000).toFixed(1)}M`; } if (n >= 1000) { return `${(n / 1000).toFixed(0)}k`; } return n.toString(); } function formatContextValue(ctx, percent, mode) { if (mode === 'tokens') { const totalTokens = getTotalTokens(ctx.stdin); const size = ctx.stdin.context_window?.context_window_size ?? 0; if (size > 0) { return `${formatTokens(totalTokens)}/${formatTokens(size)}`; } return formatTokens(totalTokens); } if (mode === 'remaining') { return `${Math.max(0, 100 - percent)}%`; } return `${percent}%`; } function formatUsagePercent(percent, colors) { if (percent === null) { return dim('--'); } const color = getQuotaColor(percent, colors); return `${color}${percent}%${RESET}`; } function formatUsageError(error) { if (!error) return ''; if (error === 'rate-limited') return ' (syncing...)'; if (error.startsWith('http-')) return ` (${error.slice(5)})`; return ` (${error})`; } function formatResetTime(resetAt) { if (!resetAt) return ''; const now = new Date(); const diffMs = resetAt.getTime() - now.getTime(); if (diffMs <= 0) return ''; const diffMins = Math.ceil(diffMs / 60000); if (diffMins < 60) return `${diffMins}m`; const hours = Math.floor(diffMins / 60); const mins = diffMins % 60; if (hours >= 24) { const days = Math.floor(hours / 24); const remHours = hours % 24; if (remHours > 0) return `${days}d ${remHours}h`; return `${days}d`; } return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`; } //# sourceMappingURL=session-line.js.map