UNPKG

cost-claude

Version:

Claude Code cost monitoring, analytics, and optimization toolkit

128 lines 3.79 kB
import chalk from 'chalk'; export function formatNumber(num) { return num.toLocaleString('en-US'); } export function formatCompactNumber(num) { if (num >= 1_000_000_000) { return `${(num / 1_000_000_000).toFixed(1)}B`; } else if (num >= 1_000_000) { return `${(num / 1_000_000).toFixed(1)}M`; } else if (num >= 1_000) { return `${(num / 1_000).toFixed(1)}K`; } return num.toString(); } export function formatDuration(ms) { if (ms < 1000) { return `${ms}ms`; } else if (ms < 60000) { return `${(ms / 1000).toFixed(1)}s`; } else if (ms < 3600000) { const minutes = Math.floor(ms / 60000); const seconds = Math.round((ms % 60000) / 1000); return `${minutes}m ${seconds}s`; } else { const hours = Math.floor(ms / 3600000); const minutes = Math.round((ms % 3600000) / 60000); return `${hours}h ${minutes}m`; } } export function formatPercentage(value, good = true) { const formatted = `${value.toFixed(1)}%`; if (good) { if (value >= 80) return chalk.green(formatted); if (value >= 50) return chalk.yellow(formatted); return chalk.red(formatted); } else { if (value <= 20) return chalk.green(formatted); if (value <= 50) return chalk.yellow(formatted); return chalk.red(formatted); } } export function formatCostColored(cost) { const formatted = `$${cost.toFixed(4)}`; if (cost < 0.01) return chalk.green(formatted); if (cost < 0.1) return chalk.yellow(formatted); if (cost < 1) return chalk.magenta(formatted); return chalk.red(formatted); } export function formatCostAdaptive(cost) { if (cost >= 10) { return `$${cost.toFixed(2)}`; } else if (cost >= 1) { return `$${cost.toFixed(3)}`; } else { return `$${cost.toFixed(4)}`; } } export function formatCost(cost) { return `$${cost.toFixed(4)}`; } export function shortenProjectName(projectName, maxLength = 20) { if (projectName.length <= maxLength) { return projectName; } const parts = projectName.split('/'); if (parts.length >= 2) { const repoName = parts[parts.length - 1]; if (repoName && repoName.length <= maxLength) { return repoName; } if (repoName) { return repoName.substring(0, maxLength - 3) + '...'; } } return projectName.substring(0, maxLength - 3) + '...'; } export function formatTimestamp(timestamp) { const date = timestamp instanceof Date ? timestamp : new Date(timestamp); return date.toLocaleString('ja-JP', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }); } export function formatDate(date) { if (!date || isNaN(date.getTime())) { return 'Invalid Date'; } return date.toISOString().split('T')[0]; } export function createProgressBar(current, total, width = 20) { const percentage = Math.min(current / total, 1); const filled = Math.round(width * percentage); const empty = width - filled; const bar = '█'.repeat(filled) + '░'.repeat(empty); const percentStr = `${(percentage * 100).toFixed(0)}%`; return `[${bar}] ${percentStr}`; } export function truncate(text, maxLength) { if (text.length <= maxLength) return text; return text.substring(0, maxLength - 3) + '...'; } export function createSeparator(length, char = '─') { return char.repeat(length); } export function formatCurrency(amount) { return `$${amount.toFixed(2)}`; } //# sourceMappingURL=format.js.map