@hhoangphuoc/escape-room-cli
Version:
A CLI for playing AI-generated escape room games. Install globally with: npm install -g @hhoangphuoc/escape-room-cli
79 lines (78 loc) • 2 kB
JavaScript
/**
* Shared formatting utilities for CLI components
*/
/**
* Format token counts with truncation (12k, 1.2M format)
*/
export const formatTokens = (tokens) => {
if (tokens === undefined || tokens === null || isNaN(tokens))
return '0';
if (tokens === 0)
return '0';
if (tokens >= 1000000) {
return `${(tokens / 1000000).toFixed(1)}M`;
}
if (tokens >= 1000) {
return `${(tokens / 1000).toFixed(1)}k`;
}
return tokens.toString();
};
/**
* Format cost amounts with consistent precision
*/
export const formatCost = (cost) => {
if (cost === undefined || cost === null || isNaN(cost))
return '$0.00';
if (cost === 0)
return '$0.00';
if (cost < 0.001)
return '<$0.001';
if (cost < 0.01)
return `$${cost.toFixed(5)}`;
return `$${cost.toFixed(3)}`;
};
/**
* Get color for cost display based on amount
*/
export const getCostColor = (cost) => {
if (!cost || cost === 0 || isNaN(cost))
return 'gray';
if (cost < 1.0)
return 'green';
if (cost < 5.0)
return 'yellow';
return 'red';
};
/**
* Get color for context warning levels
*/
export const getWarningColor = (level) => {
switch (level) {
case 'critical': return 'red';
case 'high': return 'yellow';
case 'medium': return 'orange';
case 'low': return 'cyan';
default: return 'green';
}
};
/**
* Calculate context warning level from size percentage
*/
export const calculateContextWarning = (currentSize, maxSize) => {
const percentage = (currentSize / maxSize) * 100;
if (percentage >= 95)
return 'critical';
if (percentage >= 85)
return 'high';
if (percentage >= 70)
return 'medium';
if (percentage >= 50)
return 'low';
return 'none';
};
/**
* Format percentage with consistent precision
*/
export const formatPercentage = (percentage) => {
return `${Math.round(percentage)}%`;
};