(used: number, window: number, barWidth = 10)
| 73 | * Returns '' when the window size is unknown (nothing useful to show). |
| 74 | */ |
| 75 | export function formatContextMeter(used: number, window: number, barWidth = 10): string { |
| 76 | if (!window || window <= 0 || used <= 0) return ''; |
| 77 | const pct = Math.min(100, Math.round((used / window) * 100)); |
| 78 | const filled = Math.min(barWidth, Math.round((pct / 100) * barWidth)); |
| 79 | const bar = '█'.repeat(filled) + '░'.repeat(barWidth - filled); |
| 80 | const k = (n: number) => (n >= 1000 ? `${(n / 1000).toFixed(n >= 10000 ? 0 : 1)}k` : String(n)); |
| 81 | return `${k(used)}/${k(window)} ${bar} ${pct}%`; |
| 82 | } |
no test coverage detected