feat(terra): Claude subscription limits panel on the rail

CLD panel next to luna showing the 5-hour and weekly usage windows (plus
any per-model weekly caps), polled every 5 min via a new `claude-usage`
script that reuses Claude Code's OAuth login and refreshes it when expired.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-25 01:26:44 +02:00
co-authored by Claude Opus 5.5
parent 13e8db74ce
commit f84611bc6e
7 changed files with 363 additions and 2 deletions
@@ -0,0 +1,149 @@
pragma ComponentBehavior: Bound
import QtQuick
import qs.HyprChrome.Theme
import qs.HyprChrome.Widgets.Bar.Panels
import qs.HyprChrome.Widgets.Bar.Vitals
// Claude subscription limits on the rail: the 5-hour session window and the
// weekly one, plus any per-model weekly caps the plan reports.
BarPanel {
id: claude
// Overridable so the headless render can stage it without the network.
property var fiveHour: usage.fiveHour
property var sevenDay: usage.sevenDay
property var extra: usage.extra
property string error: usage.error
property bool ready: usage.ready
readonly property var usage: ClaudeState {}
panelId: "CLD"
title: "CLAUDE"
meta: claude.error !== "" ? (claude.ready ? "STALE" : "OFFLINE") : claude.ready ? "LIMITS" : "PRIMING"
// Ticks the reset countdowns without re-polling.
property date now: new Date()
Timer {
interval: 30000
running: true
repeat: true
onTriggered: claude.now = new Date()
}
implicitWidth: claude.expanded
? Math.max(claude.headerMinWidth, claude.padding * 2 + body.width)
: Math.max(claude.headerMinWidth, claude.briefLeft + brief.implicitWidth + claude.padding)
function fraction(w) {
return w ? Math.max(0, Math.min(1, w.utilization / 100)) : 0;
}
function pct(w) {
return w ? Math.round(w.utilization) + "%" : "--";
}
function until(w) {
if (!w || !w.resets_at)
return "";
const mins = Math.max(0, Math.round((new Date(w.resets_at) - claude.now) / 60000));
if (mins >= 1440)
return `${Math.floor(mins / 1440)}D ${Math.floor(mins % 1440 / 60)}H`;
if (mins >= 60)
return `${Math.floor(mins / 60)}H ${mins % 60}M`;
return `${mins}M`;
}
function tone(w) {
const f = claude.fraction(w);
return !claude.ready ? Theme.disabled : f >= 0.85 ? Theme.hot : Theme.text;
}
summary: Row {
id: brief
spacing: 10
Repeater {
model: [
{ label: "5H", window: claude.fiveHour },
{ label: "7D", window: claude.sevenDay }
]
Row {
required property var modelData
spacing: 4
anchors.verticalCenter: parent.verticalCenter
Text {
anchors.verticalCenter: parent.verticalCenter
text: parent.modelData.label
color: Theme.muted
font.family: Theme.microFont
font.pixelSize: 10
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: claude.pct(parent.modelData.window)
color: claude.tone(parent.modelData.window)
font.family: Theme.displayFont
font.pixelSize: 13
font.bold: true
}
}
}
}
Column {
id: body
width: 260
spacing: 5
Repeater {
model: [
{ label: "SESSION", window: claude.fiveHour },
{ label: "WEEKLY", window: claude.sevenDay }
].concat(claude.extra)
Column {
required property var modelData
width: body.width
spacing: 2
VitalRow {
width: parent.width
labelWidth: 64
label: parent.modelData.label
value: claude.fraction(parent.modelData.window)
readout: claude.pct(parent.modelData.window)
ready: claude.ready && !!parent.modelData.window
}
Text {
x: 64
visible: text !== ""
text: {
const t = claude.until(parent.modelData.window);
return t ? `RESETS IN ${t}` : "";
}
color: Theme.muted
font.family: Theme.microFont
font.pixelSize: 9
font.letterSpacing: 1.2
}
}
}
Text {
width: body.width
visible: claude.error !== ""
text: claude.error.toUpperCase()
color: Theme.hot
font.family: Theme.microFont
font.pixelSize: 9
font.letterSpacing: 1.2
wrapMode: Text.WordWrap
}
}
}
@@ -0,0 +1,69 @@
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell.Io
// Claude subscription limits, polled through `claude-usage` (pkgs/claude-usage.nix),
// which reuses Claude Code's OAuth login and prints the usage endpoint's JSON.
QtObject {
id: root
// Each window is { utilization: 0..100, resets_at: ISO string } or null.
property var fiveHour: null
property var sevenDay: null
// Per-model weekly windows the plan reports, as [{ label, window }].
property var extra: []
property string error: ""
property bool ready: false
// The endpoint is rate-limited and the numbers move slowly.
property int interval: 300000
readonly property var proc: Process {
id: proc
command: ["claude-usage"]
stdout: StdioCollector {
onStreamFinished: root.ingest(this.text)
}
}
readonly property var poll: Timer {
interval: root.interval
running: true
repeat: true
triggeredOnStart: true
onTriggered: {
if (!proc.running)
proc.running = true;
}
}
function ingest(text: string): void {
let msg;
try {
msg = JSON.parse(text);
} catch (e) {
root.error = "no output";
return;
}
if (msg.error) {
// Keep the last good numbers; a failed poll is not zero usage.
root.error = msg.error;
return;
}
root.error = "";
root.fiveHour = msg.five_hour ?? null;
root.sevenDay = msg.seven_day ?? null;
const found = [];
for (const key of Object.keys(msg)) {
const m = key.match(/^seven_day_(\w+)$/);
const w = msg[key];
if (m && w && typeof w.utilization === "number")
found.push({ label: m[1].toUpperCase().replace(/_/g, " "), window: w });
}
root.extra = found;
root.ready = true;
}
}