ctxline-claude 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/package.json +1 -1
- package/statusline.js +65 -31
package/README.md
CHANGED
|
@@ -105,14 +105,15 @@ Remove-Item "$env:USERPROFILE\.claude\cache\usage-cache.json" -ErrorAction Silen
|
|
|
105
105
|
| **Weekly** | Weekly usage allowance + time until the weekly reset (subscription users) |
|
|
106
106
|
| **Task** | The in-progress todo, when there is one |
|
|
107
107
|
|
|
108
|
-
>
|
|
109
|
-
> limits.
|
|
108
|
+
> [!NOTE]
|
|
109
|
+
> Usage bars change color automatically as you approach your limits.
|
|
110
110
|
|
|
111
111
|
## How it works
|
|
112
112
|
|
|
113
|
-
- **Source** — context comes from Claude Code's session data
|
|
114
|
-
- **
|
|
115
|
-
- **
|
|
113
|
+
- **Source** — context comes from Claude Code's session data. Usage bars are read straight from the `rate_limits` field Claude Code pipes in (no network), falling back to `https://api.anthropic.com/api/oauth/usage` (the same `/usage` data — 5-hour and weekly limits) when that field isn't present yet. API-key users skip usage entirely.
|
|
114
|
+
- **No network on the fast path** — when `rate_limits` is in the session data, there's no API call at all. The fetch below only runs as a fallback (e.g. the first render of a session, before the field appears).
|
|
115
|
+
- **Adaptive timing** — for the fallback fetch: 1.5s timeout on the first prompt (cold start), 1.2s after (connection reused).
|
|
116
|
+
- **Caching** — the fallback fetch is cached at `~/.claude/cache/usage-cache.json`, shared across sessions. Within 30s the cache renders directly (the API call is skipped); if a live call fails, the last value (up to 10 min old) is shown so the bar never vanishes. The reset countdown recomputes every render.
|
|
116
117
|
- **Never breaks** — every failure path falls back silently; the statusline always prints.
|
|
117
118
|
|
|
118
119
|
## FAQ
|
|
@@ -120,7 +121,7 @@ Remove-Item "$env:USERPROFILE\.claude\cache\usage-cache.json" -ErrorAction Silen
|
|
|
120
121
|
<details>
|
|
121
122
|
<summary>Does this use the same data as /usage?</summary>
|
|
122
123
|
|
|
123
|
-
Yes.
|
|
124
|
+
Yes — the same 5-hour and weekly limits. It reads them from the session data Claude Code provides when available, and falls back to Anthropic's usage API (the endpoint `/usage` uses) otherwise.
|
|
124
125
|
|
|
125
126
|
</details>
|
|
126
127
|
|
|
@@ -141,7 +142,7 @@ No. All failures are handled silently and the statusline always renders.
|
|
|
141
142
|
<details>
|
|
142
143
|
<summary>Does it expose my API keys / auth tokens?</summary>
|
|
143
144
|
|
|
144
|
-
No. Your credentials never leave your machine.
|
|
145
|
+
No. Your credentials never leave your machine. On the fast path no token is read at all — usage comes straight from the session data. Only on the fallback fetch is the OAuth token read locally (from `~/.claude/.credentials.json` or the macOS keychain), used solely to authenticate the request to Anthropic's own usage API — the same endpoint `/usage` uses. Nothing is sent to any third party, logged, or cached; only the resulting usage percentages are stored locally.
|
|
145
146
|
|
|
146
147
|
</details>
|
|
147
148
|
|
package/package.json
CHANGED
package/statusline.js
CHANGED
|
@@ -163,6 +163,37 @@ function normalizePercentage(value) {
|
|
|
163
163
|
return Math.max(0, Math.min(100, Math.round(value)));
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
// Build usage bars from stdin `rate_limits` (Claude.ai Pro/Max, present only after the
|
|
167
|
+
// first API response of a session). Same data as the OAuth usage API, so reading it here
|
|
168
|
+
// skips the network/credentials/cache path entirely. `resets_at` is a Unix epoch in
|
|
169
|
+
// SECONDS (not ISO) — ×1000 before Date. Returns { current, weekly } bars, or null when
|
|
170
|
+
// rate_limits is absent or the required five_hour segment is unusable (caller falls back).
|
|
171
|
+
function buildUsageFromStdin(data) {
|
|
172
|
+
const rl = data?.rate_limits;
|
|
173
|
+
if (!rl) return null;
|
|
174
|
+
|
|
175
|
+
const toEntry = (seg) => {
|
|
176
|
+
if (!seg) return null;
|
|
177
|
+
const pct = normalizePercentage(seg.used_percentage);
|
|
178
|
+
if (pct == null) return null;
|
|
179
|
+
// resets_at is a Unix epoch in SECONDS. Coerce + validate defensively: a non-numeric
|
|
180
|
+
// or out-of-range value would make new Date(...).toISOString() throw, and this path
|
|
181
|
+
// runs outside outputStatus's try/catch. Fall back to resetsAt: null on anything bad.
|
|
182
|
+
let resetsAt = null;
|
|
183
|
+
const epoch = Number(seg.resets_at);
|
|
184
|
+
if (Number.isFinite(epoch) && epoch > 0) {
|
|
185
|
+
const d = new Date(epoch * 1000);
|
|
186
|
+
if (!Number.isNaN(d.getTime())) resetsAt = d.toISOString();
|
|
187
|
+
}
|
|
188
|
+
return { percentage: pct, resetsAt };
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const fiveHour = toEntry(rl.five_hour);
|
|
192
|
+
if (!fiveHour) return null; // five_hour is the required bar
|
|
193
|
+
const weekly = toEntry(rl.seven_day);
|
|
194
|
+
return buildUsageBars(fiveHour, weekly);
|
|
195
|
+
}
|
|
196
|
+
|
|
166
197
|
// Validate a single usage entry ({ percentage, resetsAt }). Returns true only for a
|
|
167
198
|
// finite 0-100 percentage and a parseable (or absent) resetsAt.
|
|
168
199
|
function isValidUsageEntry(entry) {
|
|
@@ -393,21 +424,45 @@ function outputFallback(usage) {
|
|
|
393
424
|
process.stdout.write(parts.join(' \u2502 '));
|
|
394
425
|
}
|
|
395
426
|
|
|
396
|
-
//
|
|
397
|
-
|
|
427
|
+
// Resolve usage bars for a (possibly null) parsed stdin payload.
|
|
428
|
+
// Order: API-key users get none; otherwise prefer stdin `rate_limits` (no network),
|
|
429
|
+
// then fall back to the cache+API flow when stdin lacks it (cold start / non-Pro/Max).
|
|
430
|
+
function resolveUsage(data, callback) {
|
|
398
431
|
if (IS_API_KEY) {
|
|
399
|
-
callback(null);
|
|
400
|
-
} else {
|
|
401
|
-
getUsageWithCache(callback);
|
|
432
|
+
return callback(null);
|
|
402
433
|
}
|
|
434
|
+
const fromStdin = buildUsageFromStdin(data);
|
|
435
|
+
if (fromStdin) {
|
|
436
|
+
return callback(fromStdin);
|
|
437
|
+
}
|
|
438
|
+
getUsageWithCache(callback);
|
|
403
439
|
}
|
|
404
440
|
|
|
405
441
|
// Process with timeout
|
|
406
|
-
if
|
|
407
|
-
|
|
408
|
-
|
|
442
|
+
// Parse the accumulated stdin into a payload object, or null if empty/unparseable.
|
|
443
|
+
function parseInput(input) {
|
|
444
|
+
if (!input || input.length === 0) return null;
|
|
445
|
+
try {
|
|
446
|
+
return JSON.parse(input);
|
|
447
|
+
} catch (e) {
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Resolve usage for `data` (preferring stdin rate_limits), then render and exit.
|
|
453
|
+
function emit(data) {
|
|
454
|
+
resolveUsage(data, (usage) => {
|
|
455
|
+
if (data) {
|
|
456
|
+
outputStatus(data, usage);
|
|
457
|
+
} else {
|
|
458
|
+
outputFallback(usage);
|
|
459
|
+
}
|
|
409
460
|
process.exit(0);
|
|
410
461
|
});
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (process.stdin.isTTY) {
|
|
465
|
+
emit(null);
|
|
411
466
|
} else {
|
|
412
467
|
let input = '';
|
|
413
468
|
let timeoutReached = false;
|
|
@@ -416,19 +471,7 @@ if (process.stdin.isTTY) {
|
|
|
416
471
|
|
|
417
472
|
const timeout = setTimeout(() => {
|
|
418
473
|
timeoutReached = true;
|
|
419
|
-
|
|
420
|
-
if (input.length > 0) {
|
|
421
|
-
try {
|
|
422
|
-
const data = JSON.parse(input);
|
|
423
|
-
outputStatus(data, usage);
|
|
424
|
-
} catch (e) {
|
|
425
|
-
outputFallback(usage);
|
|
426
|
-
}
|
|
427
|
-
} else {
|
|
428
|
-
outputFallback(usage);
|
|
429
|
-
}
|
|
430
|
-
process.exit(0);
|
|
431
|
-
});
|
|
474
|
+
emit(parseInput(input));
|
|
432
475
|
}, overallTimeout);
|
|
433
476
|
|
|
434
477
|
process.stdin.setEncoding('utf8');
|
|
@@ -436,15 +479,6 @@ if (process.stdin.isTTY) {
|
|
|
436
479
|
process.stdin.on('end', () => {
|
|
437
480
|
if (timeoutReached) return;
|
|
438
481
|
clearTimeout(timeout);
|
|
439
|
-
|
|
440
|
-
getUsage((usage) => {
|
|
441
|
-
try {
|
|
442
|
-
const data = JSON.parse(input);
|
|
443
|
-
outputStatus(data, usage);
|
|
444
|
-
} catch (e) {
|
|
445
|
-
outputFallback(usage);
|
|
446
|
-
}
|
|
447
|
-
process.exit(0);
|
|
448
|
-
});
|
|
482
|
+
emit(parseInput(input));
|
|
449
483
|
});
|
|
450
484
|
}
|