Add 5h rate limit bar and session cost to statusline
Dumps harness JSON to os.tmpdir() on each render to inspect available fields. Adds a combined usage segment showing the Pro 5-hour rate limit as a colour-coded bar and session cost in USD; each part is omitted gracefully when its data is absent, so the script works for API instances too. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
'use strict';
|
||||
|
||||
const { execSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
|
||||
const R = '\x1b[0m';
|
||||
@@ -25,6 +27,10 @@ process.stdin.on('end', () => {
|
||||
let data = {};
|
||||
try { if (input.trim()) data = JSON.parse(input); } catch (_) {}
|
||||
|
||||
try {
|
||||
fs.writeFileSync(path.join(os.tmpdir(), 'statusline_data.json'), JSON.stringify(data, null, 2));
|
||||
} catch (_) {}
|
||||
|
||||
// --- Directory ---
|
||||
const cwd =
|
||||
(data.workspace && data.workspace.current_dir) ||
|
||||
@@ -83,11 +89,36 @@ process.stdin.on('end', () => {
|
||||
`${DIM}` + ' '.repeat(BAR_WIDTH - filled) +
|
||||
`${barColor}] ${pct}%${R}`;
|
||||
|
||||
// --- 5h rate limit + cost ---
|
||||
const fiveHour = data.rate_limits && data.rate_limits.five_hour;
|
||||
const totalCost = data.cost && data.cost.total_cost_usd;
|
||||
|
||||
let usageSegment = null;
|
||||
if (fiveHour || totalCost) {
|
||||
const parts = [];
|
||||
if (fiveHour) {
|
||||
const fivePct = Math.round(fiveHour.used_percentage);
|
||||
const fiveColor = fivePct >= 90 ? BAR_CRIT : fivePct >= 70 ? BAR_WARN : BAR_OK;
|
||||
const fiveFilled = Math.round((fivePct / 100) * BAR_WIDTH);
|
||||
const fiveBar =
|
||||
`${fiveColor}[` +
|
||||
'='.repeat(fiveFilled) +
|
||||
`${DIM}` + ' '.repeat(BAR_WIDTH - fiveFilled) +
|
||||
`${fiveColor}] ${fivePct}%${R}`;
|
||||
parts.push(`5h ${fiveBar}`);
|
||||
}
|
||||
if (totalCost) {
|
||||
parts.push(`${DIM}$${totalCost.toFixed(2)}${R}`);
|
||||
}
|
||||
usageSegment = parts.join(' ');
|
||||
}
|
||||
|
||||
// --- Assemble ---
|
||||
const segments = [`${CYAN}${displayDir}${R}`];
|
||||
if (gitBranch !== null) segments.push(`${GREEN}${gitBranch}${R}`);
|
||||
segments.push(`${MAGENTA}${model}${R}`);
|
||||
segments.push(bar);
|
||||
if (usageSegment !== null) segments.push(usageSegment);
|
||||
|
||||
process.stdout.write(segments.join(SEP) + '\n');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user