import { isLimitReached } from '../../types.js'; import { getProviderLabel } from '../../stdin.js'; import { critical, warning, dim, getQuotaColor, quotaBar, RESET } from '../colors.js'; import { getAdaptiveBarWidth } from '../../utils/terminal.js'; export function renderUsageLine(ctx) { const display = ctx.config?.display; const colors = ctx.config?.colors; if (display?.showUsage === false) { return null; } if (!ctx.usageData?.planName) { return null; } if (getProviderLabel(ctx.stdin)) { return null; } const label = dim('Usage'); if (ctx.usageData.apiUnavailable) { const errorHint = formatUsageError(ctx.usageData.apiError); return `${label} ${warning(`⚠${errorHint}`, colors)}`; } if (isLimitReached(ctx.usageData)) { const resetTime = ctx.usageData.fiveHour === 100 ? formatResetTime(ctx.usageData.fiveHourResetAt) : formatResetTime(ctx.usageData.sevenDayResetAt); return `${label} ${critical(`⚠ Limit reached${resetTime ? ` (resets ${resetTime})` : ''}`, colors)}`; } const threshold = display?.usageThreshold ?? 0; const fiveHour = ctx.usageData.fiveHour; const sevenDay = ctx.usageData.sevenDay; const effectiveUsage = Math.max(fiveHour ?? 0, sevenDay ?? 0); if (effectiveUsage < threshold) { return null; } const fiveHourDisplay = formatUsagePercent(ctx.usageData.fiveHour, colors); const fiveHourReset = formatResetTime(ctx.usageData.fiveHourResetAt); const usageBarEnabled = display?.usageBarEnabled ?? true; const fiveHourPart = usageBarEnabled ? (fiveHourReset ? `${quotaBar(fiveHour ?? 0, getAdaptiveBarWidth(), colors)} ${fiveHourDisplay} (resets in ${fiveHourReset})` : `${quotaBar(fiveHour ?? 0, getAdaptiveBarWidth(), colors)} ${fiveHourDisplay}`) : (fiveHourReset ? `5h: ${fiveHourDisplay} (resets in ${fiveHourReset})` : `5h: ${fiveHourDisplay}`); const sevenDayThreshold = display?.sevenDayThreshold ?? 80; const syncingSuffix = ctx.usageData.apiError === 'rate-limited' ? ` ${dim('(syncing...)')}` : ''; if (sevenDay !== null && sevenDay >= sevenDayThreshold) { const sevenDayDisplay = formatUsagePercent(sevenDay, colors); const sevenDayReset = formatResetTime(ctx.usageData.sevenDayResetAt); const sevenDayPart = usageBarEnabled ? (sevenDayReset ? `${quotaBar(sevenDay, getAdaptiveBarWidth(), colors)} ${sevenDayDisplay} (resets in ${sevenDayReset})` : `${quotaBar(sevenDay, getAdaptiveBarWidth(), colors)} ${sevenDayDisplay}`) : (sevenDayReset ? `7d: ${sevenDayDisplay} (resets in ${sevenDayReset})` : `7d: ${sevenDayDisplay}`); return `${label} ${fiveHourPart} | ${sevenDayPart}${syncingSuffix}`; } return `${label} ${fiveHourPart}${syncingSuffix}`; } 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=usage.js.map