@rakshazi/nodion 0.0.0-stage → 0.1.0
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/LICENSE +304 -0
- package/README.md +62 -2
- package/catalog.ts +192 -0
- package/index.ts +125 -0
- package/options.ts +35 -0
- package/package.json +24 -5
- package/panel.ts +84 -0
- package/plan-state.ts +155 -0
- package/plan.ts +452 -0
package/plan.ts
ADDED
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
// Nodion coding-plan usage: `GET /v1/plan` parsing and fetching, kept free of Pi runtime imports.
|
|
2
|
+
import { BASE_URL } from "./catalog.ts";
|
|
3
|
+
|
|
4
|
+
export const PLAN_TIMEOUT_MS = 10_000;
|
|
5
|
+
export const NO_KEY_MESSAGE = "No Nodion API key. Run /login nodion.";
|
|
6
|
+
export const NO_PLAN_MESSAGE = "No active Nodion coding plan for this key.";
|
|
7
|
+
export const PLAN_ENDED_MESSAGE = "This Nodion coding plan has ended.";
|
|
8
|
+
export const UNAUTHORIZED_MESSAGE = "Nodion API key was rejected. Run /login nodion.";
|
|
9
|
+
export const INVALID_RESPONSE_MESSAGE = "Invalid Nodion plan response";
|
|
10
|
+
export const NOT_JSON_MESSAGE = "Nodion plan response is not JSON";
|
|
11
|
+
|
|
12
|
+
export interface PlanWindow {
|
|
13
|
+
usedPercent: number;
|
|
14
|
+
resetsAt: Date | null;
|
|
15
|
+
windowSeconds: number | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CodingPlan {
|
|
19
|
+
planCode: string;
|
|
20
|
+
planName: string;
|
|
21
|
+
status: string;
|
|
22
|
+
walletOverage: boolean;
|
|
23
|
+
session?: PlanWindow;
|
|
24
|
+
week?: PlanWindow;
|
|
25
|
+
month?: PlanWindow;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface PlanRequest {
|
|
29
|
+
apiKey: string;
|
|
30
|
+
baseUrl?: string;
|
|
31
|
+
signal: AbortSignal;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type PlanFetch =
|
|
35
|
+
| { ok: true; plan: CodingPlan }
|
|
36
|
+
| { ok: false; reason: "no_plan" | "ended" | "unauthorized" | "failed"; message: string };
|
|
37
|
+
|
|
38
|
+
/** A server-supplied label with C0 and C1 control bytes collapsed, so it cannot break a card or footer row. */
|
|
39
|
+
function cleanText(value: unknown): string {
|
|
40
|
+
return typeof value === "string" ? value.replace(/[\u0000-\u001f\u007f-\u009f]+/g, " ").replace(/ +/g, " ").trim() : "";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** One percentage window, or undefined when it carries no usable percentage. */
|
|
44
|
+
function readWindow(value: unknown): PlanWindow | undefined {
|
|
45
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
|
|
46
|
+
const usedPercent = "used_percent" in value ? value.used_percent : undefined;
|
|
47
|
+
if (typeof usedPercent !== "number" || !Number.isFinite(usedPercent)) return undefined;
|
|
48
|
+
|
|
49
|
+
let resetsAt: Date | null = null;
|
|
50
|
+
const rawReset = "resets_at" in value ? value.resets_at : undefined;
|
|
51
|
+
if (typeof rawReset === "string") {
|
|
52
|
+
const date = new Date(rawReset);
|
|
53
|
+
if (Number.isFinite(date.getTime())) resetsAt = date;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let windowSeconds: number | undefined;
|
|
57
|
+
const rawSeconds = "window_seconds" in value ? value.window_seconds : undefined;
|
|
58
|
+
if (typeof rawSeconds === "number" && Number.isFinite(rawSeconds) && rawSeconds > 0) {
|
|
59
|
+
windowSeconds = Math.trunc(rawSeconds);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { usedPercent, resetsAt, windowSeconds };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Maps a `coding_plan` payload; throws when the object tag or every window is missing. */
|
|
66
|
+
export function parsePlan(payload: unknown): CodingPlan {
|
|
67
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
|
|
68
|
+
throw new Error(INVALID_RESPONSE_MESSAGE);
|
|
69
|
+
}
|
|
70
|
+
if (!("object" in payload) || payload.object !== "coding_plan") throw new Error(INVALID_RESPONSE_MESSAGE);
|
|
71
|
+
|
|
72
|
+
const session = readWindow(payload.session);
|
|
73
|
+
const week = readWindow(payload.week);
|
|
74
|
+
const month = readWindow(payload.month);
|
|
75
|
+
if (!session && !week && !month) throw new Error(INVALID_RESPONSE_MESSAGE);
|
|
76
|
+
|
|
77
|
+
const planCode = cleanText(payload.plan_code);
|
|
78
|
+
const planName = cleanText(payload.plan_name) || planCode || "Coding plan";
|
|
79
|
+
const status = cleanText(payload.status) || "unknown";
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
planCode,
|
|
83
|
+
planName,
|
|
84
|
+
status,
|
|
85
|
+
walletOverage: payload.wallet_overage === true,
|
|
86
|
+
...(session ? { session } : {}),
|
|
87
|
+
...(week ? { week } : {}),
|
|
88
|
+
...(month ? { month } : {}),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The `error.code` of a Nodion error body, when it has one. */
|
|
93
|
+
function readErrorCode(payload: unknown): string | undefined {
|
|
94
|
+
const error =
|
|
95
|
+
typeof payload === "object" && payload !== null && !Array.isArray(payload) && "error" in payload
|
|
96
|
+
? payload.error
|
|
97
|
+
: undefined;
|
|
98
|
+
if (typeof error !== "object" || error === null || Array.isArray(error)) return undefined;
|
|
99
|
+
const code = "code" in error ? error.code : undefined;
|
|
100
|
+
return typeof code === "string" ? code : undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Fetches the coding plan; every failure returns a reason and a message a card can print. */
|
|
104
|
+
export async function fetchPlan(request: PlanRequest, fetchImpl: typeof fetch = fetch): Promise<PlanFetch> {
|
|
105
|
+
const baseUrl = (request.baseUrl ?? BASE_URL).replace(/\/+$/, "");
|
|
106
|
+
|
|
107
|
+
let response: Response;
|
|
108
|
+
try {
|
|
109
|
+
response = await fetchImpl(`${baseUrl}/plan`, {
|
|
110
|
+
method: "GET",
|
|
111
|
+
headers: {
|
|
112
|
+
accept: "application/json",
|
|
113
|
+
authorization: `Bearer ${request.apiKey}`,
|
|
114
|
+
},
|
|
115
|
+
signal: AbortSignal.any([request.signal, AbortSignal.timeout(PLAN_TIMEOUT_MS)]),
|
|
116
|
+
});
|
|
117
|
+
} catch (error) {
|
|
118
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
119
|
+
return { ok: false, reason: "failed", message: `Nodion plan request failed: ${message}` };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Best effort: a non-2xx response may carry no JSON at all, and only the status matters there.
|
|
123
|
+
let payload: unknown;
|
|
124
|
+
let parsed = false;
|
|
125
|
+
try {
|
|
126
|
+
payload = await response.json();
|
|
127
|
+
parsed = true;
|
|
128
|
+
} catch {
|
|
129
|
+
// Left undefined on purpose.
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (response.status === 404) {
|
|
133
|
+
const code = readErrorCode(payload);
|
|
134
|
+
if (code === "no_coding_plan") return { ok: false, reason: "no_plan", message: NO_PLAN_MESSAGE };
|
|
135
|
+
if (code === "coding_plan_ended") return { ok: false, reason: "ended", message: PLAN_ENDED_MESSAGE };
|
|
136
|
+
return { ok: false, reason: "failed", message: "Nodion plan request failed: HTTP 404" };
|
|
137
|
+
}
|
|
138
|
+
if (response.status === 401) return { ok: false, reason: "unauthorized", message: UNAUTHORIZED_MESSAGE };
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
return { ok: false, reason: "failed", message: `Nodion plan request failed: HTTP ${response.status}` };
|
|
141
|
+
}
|
|
142
|
+
if (!parsed) return { ok: false, reason: "failed", message: NOT_JSON_MESSAGE };
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
return { ok: true, plan: parsePlan(payload) };
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return { ok: false, reason: "failed", message: error instanceof Error ? error.message : String(error) };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export type Severity = "normal" | "warning" | "danger";
|
|
152
|
+
export type FooterToken = "dim" | "text" | "warning" | "error";
|
|
153
|
+
export type PanelToken = "accent" | "border" | "dim" | "text" | "success" | "warning" | "error";
|
|
154
|
+
|
|
155
|
+
export interface FooterSegment {
|
|
156
|
+
text: string;
|
|
157
|
+
token: FooterToken;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface FooterStyle {
|
|
161
|
+
fg(token: FooterToken, text: string): string;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface PanelStyle {
|
|
165
|
+
fg(token: PanelToken, text: string): string;
|
|
166
|
+
bold(text: string): string;
|
|
167
|
+
width(text: string): number;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface PlanView {
|
|
171
|
+
plan?: CodingPlan;
|
|
172
|
+
error?: string;
|
|
173
|
+
updatedAt?: Date;
|
|
174
|
+
loading: boolean;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Pi's footer thresholds: over 90% is danger, over 70% warning, NaN stays normal. */
|
|
178
|
+
export function severityOf(usedPercent: number): Severity {
|
|
179
|
+
const clamped = Math.min(100, Math.max(0, usedPercent));
|
|
180
|
+
if (clamped > 90) return "danger";
|
|
181
|
+
if (clamped > 70) return "warning";
|
|
182
|
+
return "normal";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** A percentage without its sign, clamped to 0..100 and floored at "<1" when rounding hides a live value. */
|
|
186
|
+
export function formatPercent(usedPercent: number, decimals = 0): string {
|
|
187
|
+
if (!Number.isFinite(usedPercent)) return "-";
|
|
188
|
+
const digits = Number.isFinite(decimals) ? Math.max(0, Math.trunc(decimals)) : 0;
|
|
189
|
+
const clamped = Math.min(100, Math.max(0, usedPercent));
|
|
190
|
+
const rounded = clamped.toFixed(digits);
|
|
191
|
+
if (Number(rounded) === 0 && clamped > 0) return `<${(1 / 10 ** digits).toFixed(digits)}`;
|
|
192
|
+
return rounded;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The remaining time to a reset, coarse enough for a footer or a card row. */
|
|
196
|
+
export function formatCountdown(resetsAt: Date | null, now: Date): string | undefined {
|
|
197
|
+
if (!resetsAt) return undefined;
|
|
198
|
+
const delta = resetsAt.getTime() - now.getTime();
|
|
199
|
+
if (!Number.isFinite(delta)) return undefined;
|
|
200
|
+
if (delta <= 0) return "now";
|
|
201
|
+
if (delta < 60_000) return "<1m";
|
|
202
|
+
const minutes = Math.floor(delta / 60_000);
|
|
203
|
+
if (minutes < 60) return `${minutes}m`;
|
|
204
|
+
const hours = Math.floor(delta / 3_600_000);
|
|
205
|
+
if (delta < 86_400_000) return `${hours}h ${minutes % 60}m`;
|
|
206
|
+
const days = Math.floor(delta / 86_400_000);
|
|
207
|
+
return `${days}d ${hours % 24}h`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** A reset in local time; a session window shows only the clock, longer windows add the date. */
|
|
211
|
+
export function formatResetClock(resetsAt: Date | null, windowSeconds: number | undefined, timeZone?: string): string | undefined {
|
|
212
|
+
if (!resetsAt || !Number.isFinite(resetsAt.getTime())) return undefined;
|
|
213
|
+
const clockOnly = typeof windowSeconds === "number" && windowSeconds > 0 && windowSeconds < 86_400;
|
|
214
|
+
const options: Intl.DateTimeFormatOptions = clockOnly
|
|
215
|
+
? { hour: "2-digit", minute: "2-digit", hourCycle: "h23" }
|
|
216
|
+
: { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", hourCycle: "h23" };
|
|
217
|
+
return new Intl.DateTimeFormat("en-US", timeZone ? { ...options, timeZone } : options).format(resetsAt);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** How old a fetched plan is, with the same coarse units as the countdown. */
|
|
221
|
+
export function formatAge(updatedAt: Date | undefined, now: Date): string | undefined {
|
|
222
|
+
if (!updatedAt) return undefined;
|
|
223
|
+
const delta = now.getTime() - updatedAt.getTime();
|
|
224
|
+
if (!Number.isFinite(delta) || delta < 10_000) return "just now";
|
|
225
|
+
if (delta < 60_000) return `${Math.floor(delta / 1000)}s ago`;
|
|
226
|
+
if (delta < 3_600_000) return `${Math.floor(delta / 60_000)}m ago`;
|
|
227
|
+
return `${Math.floor(delta / 3_600_000)}h ago`;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The compact footer line: a status prefix and the three window percentages. */
|
|
231
|
+
export function footerSegments(plan: CodingPlan): FooterSegment[] {
|
|
232
|
+
const segments: FooterSegment[] = [];
|
|
233
|
+
if (plan.status === "past_due") segments.push({ text: "! ", token: "warning" });
|
|
234
|
+
if (plan.status === "pending_payment") segments.push({ text: "! ", token: "error" });
|
|
235
|
+
|
|
236
|
+
const windows: Array<{ label: string; window: PlanWindow | undefined }> = [
|
|
237
|
+
{ label: "5h ", window: plan.session },
|
|
238
|
+
{ label: "wk ", window: plan.week },
|
|
239
|
+
{ label: "mo ", window: plan.month },
|
|
240
|
+
];
|
|
241
|
+
for (const [index, { label, window }] of windows.entries()) {
|
|
242
|
+
if (index > 0) segments.push({ text: " · ", token: "dim" });
|
|
243
|
+
segments.push({ text: label, token: "dim" });
|
|
244
|
+
if (!window || window.resetsAt === null) {
|
|
245
|
+
// U+00A0, not ASCII spaces: Pi's footer sanitizer collapses runs of plain spaces.
|
|
246
|
+
segments.push({ text: "\u00A0\u00A0\u00A0-", token: "dim" });
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
const severity = severityOf(window.usedPercent);
|
|
250
|
+
const token: FooterToken = severity === "danger" ? "error" : severity === "warning" ? "warning" : "text";
|
|
251
|
+
segments.push({ text: `${formatPercent(window.usedPercent, 0).padStart(3, "\u00A0")}%`, token });
|
|
252
|
+
}
|
|
253
|
+
return segments;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function renderFooter(plan: CodingPlan, style: FooterStyle): string {
|
|
257
|
+
return footerSegments(plan)
|
|
258
|
+
.map((segment) => style.fg(segment.token, segment.text))
|
|
259
|
+
.join("");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
interface Cell {
|
|
263
|
+
text: string;
|
|
264
|
+
token?: PanelToken;
|
|
265
|
+
bold?: boolean;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Cuts text to a visible-column budget, marking the loss with an ellipsis. */
|
|
269
|
+
function truncateText(text: string, max: number, width: (value: string) => number): string {
|
|
270
|
+
if (width(text) <= max) return text;
|
|
271
|
+
if (max <= 0) return "";
|
|
272
|
+
let out = "";
|
|
273
|
+
for (const character of text) {
|
|
274
|
+
if (width(`${out}${character}…`) > max) break;
|
|
275
|
+
out += character;
|
|
276
|
+
}
|
|
277
|
+
return `${out}…`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Keeps the cells that fit and truncates the one that does not, so the right border always survives. */
|
|
281
|
+
function fitCells(cells: Cell[], max: number, width: (value: string) => number): Cell[] {
|
|
282
|
+
const fitted: Cell[] = [];
|
|
283
|
+
let used = 0;
|
|
284
|
+
for (const cell of cells) {
|
|
285
|
+
const cellWidth = width(cell.text);
|
|
286
|
+
if (used + cellWidth <= max) {
|
|
287
|
+
fitted.push(cell);
|
|
288
|
+
used += cellWidth;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const remaining = max - used;
|
|
292
|
+
if (remaining > 0) fitted.push({ ...cell, text: truncateText(cell.text, remaining, width) });
|
|
293
|
+
break;
|
|
294
|
+
}
|
|
295
|
+
return fitted;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function renderCells(cells: Cell[], inner: number, style: PanelStyle): string {
|
|
299
|
+
const fitted = fitCells(cells, inner, style.width);
|
|
300
|
+
let body = "";
|
|
301
|
+
for (const cell of fitted) {
|
|
302
|
+
let value = cell.text;
|
|
303
|
+
if (cell.token) value = style.fg(cell.token, value);
|
|
304
|
+
if (cell.bold) value = style.bold(value);
|
|
305
|
+
body += value;
|
|
306
|
+
}
|
|
307
|
+
const plain = fitted.map((cell) => cell.text).join("");
|
|
308
|
+
const padding = Math.max(0, inner - style.width(plain));
|
|
309
|
+
return `${style.fg("border", "│")}${body}${" ".repeat(padding)}${style.fg("border", "│")}`;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function blankRow(inner: number, style: PanelStyle): string {
|
|
313
|
+
return style.fg("border", `│${" ".repeat(inner)}│`);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** One content row with a leading and trailing space around a single styled text. */
|
|
317
|
+
function textRow(text: string, token: PanelToken, inner: number, style: PanelStyle): string {
|
|
318
|
+
const body = truncateText(text, Math.max(0, inner - 2), style.width);
|
|
319
|
+
return renderCells([{ text: " " }, { text: body, token }, { text: " " }], inner, style);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** The card body: title, plan details, three window rows, refresh state and the key hint. */
|
|
323
|
+
export function renderPlanPanel(view: PlanView, width: number, now: Date, style: PanelStyle): string[] {
|
|
324
|
+
if (width < 3) return [];
|
|
325
|
+
const inner = Math.max(1, width - 2);
|
|
326
|
+
const lines: string[] = [];
|
|
327
|
+
|
|
328
|
+
const titleBudget = inner - 3;
|
|
329
|
+
if (titleBudget < 0) {
|
|
330
|
+
lines.push(style.fg("border", `╭${"─".repeat(Math.max(0, width - 2))}╮`));
|
|
331
|
+
} else {
|
|
332
|
+
const title = truncateText("Nodion coding plan", titleBudget, style.width);
|
|
333
|
+
const dashes = Math.max(0, titleBudget - style.width(title));
|
|
334
|
+
lines.push(
|
|
335
|
+
`${style.fg("border", "╭─ ")}${style.bold(style.fg("accent", title))}${style.fg("border", ` ${"─".repeat(dashes)}╮`)}`,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const plan = view.plan;
|
|
340
|
+
if (plan) {
|
|
341
|
+
const statusToken: PanelToken =
|
|
342
|
+
plan.status === "active"
|
|
343
|
+
? "success"
|
|
344
|
+
: plan.status === "canceling"
|
|
345
|
+
? "warning"
|
|
346
|
+
: plan.status === "past_due" || plan.status === "pending_payment"
|
|
347
|
+
? "error"
|
|
348
|
+
: "dim";
|
|
349
|
+
const overage: Cell[] = plan.walletOverage
|
|
350
|
+
? [
|
|
351
|
+
{ text: " · ", token: "dim" },
|
|
352
|
+
{ text: "wallet overage on", token: "warning" },
|
|
353
|
+
]
|
|
354
|
+
: [];
|
|
355
|
+
const fixed = 1 + 3 + style.width(plan.status) + overage.reduce((sum, cell) => sum + style.width(cell.text), 0) + 1;
|
|
356
|
+
const nameBudget = Math.max(0, inner - fixed);
|
|
357
|
+
const name = truncateText(plan.planName, nameBudget, style.width);
|
|
358
|
+
lines.push(
|
|
359
|
+
renderCells(
|
|
360
|
+
[
|
|
361
|
+
{ text: " " },
|
|
362
|
+
{ text: name, token: "text", bold: true },
|
|
363
|
+
{ text: " · ", token: "dim" },
|
|
364
|
+
{ text: plan.status, token: statusToken },
|
|
365
|
+
...overage,
|
|
366
|
+
{ text: " " },
|
|
367
|
+
],
|
|
368
|
+
inner,
|
|
369
|
+
style,
|
|
370
|
+
),
|
|
371
|
+
);
|
|
372
|
+
lines.push(blankRow(inner, style));
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const windows: Array<{ label: string; window: PlanWindow | undefined; session: boolean }> = [
|
|
376
|
+
{ label: "5h", window: plan?.session, session: true },
|
|
377
|
+
{ label: "week", window: plan?.week, session: false },
|
|
378
|
+
{ label: "month", window: plan?.month, session: false },
|
|
379
|
+
];
|
|
380
|
+
for (const { label, window, session } of windows) {
|
|
381
|
+
const idle = !window || window.resetsAt === null;
|
|
382
|
+
const severity = window ? severityOf(window.usedPercent) : "normal";
|
|
383
|
+
const valueToken: PanelToken = idle
|
|
384
|
+
? "dim"
|
|
385
|
+
: severity === "danger"
|
|
386
|
+
? "error"
|
|
387
|
+
: severity === "warning"
|
|
388
|
+
? "warning"
|
|
389
|
+
: "text";
|
|
390
|
+
const percent = idle ? "-" : `${formatPercent(window.usedPercent, 1)}%`;
|
|
391
|
+
|
|
392
|
+
const cells: Cell[] = [
|
|
393
|
+
{ text: " " },
|
|
394
|
+
{ text: `${label.padEnd(5)} `, token: "dim" },
|
|
395
|
+
{ text: `${percent.padStart(6)} `, token: valueToken },
|
|
396
|
+
];
|
|
397
|
+
|
|
398
|
+
let filled = 0;
|
|
399
|
+
if (window && !idle) {
|
|
400
|
+
const clamped = Math.min(100, Math.max(0, window.usedPercent));
|
|
401
|
+
if (clamped > 0) filled = Math.min(10, Math.max(1, Math.round(clamped / 10)));
|
|
402
|
+
}
|
|
403
|
+
if (inner >= 30) {
|
|
404
|
+
if (filled > 0) cells.push({ text: "█".repeat(filled), token: valueToken });
|
|
405
|
+
cells.push({ text: "░".repeat(10 - filled), token: "dim" });
|
|
406
|
+
cells.push({ text: " " });
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const used = cells.reduce((sum, cell) => sum + style.width(cell.text), 0);
|
|
410
|
+
const available = inner - used - 1;
|
|
411
|
+
let reset: string | undefined;
|
|
412
|
+
if (window && idle && session) {
|
|
413
|
+
if (available >= style.width("no session running")) reset = "no session running";
|
|
414
|
+
} else if (window && window.resetsAt) {
|
|
415
|
+
const countdown = formatCountdown(window.resetsAt, now);
|
|
416
|
+
if (countdown) {
|
|
417
|
+
const countdownLabel = countdown === "now" ? "now" : `in ${countdown}`;
|
|
418
|
+
if (countdown === "now") {
|
|
419
|
+
if (available >= style.width(countdownLabel)) reset = countdownLabel;
|
|
420
|
+
} else {
|
|
421
|
+
const clock = formatResetClock(window.resetsAt, window.windowSeconds);
|
|
422
|
+
const full = clock ? `${countdownLabel} · ${clock}` : countdownLabel;
|
|
423
|
+
if (clock && available >= style.width(full)) reset = full;
|
|
424
|
+
else if (available >= style.width(countdownLabel)) reset = countdownLabel;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (reset !== undefined) cells.push({ text: reset, token: "dim" });
|
|
430
|
+
cells.push({ text: " " });
|
|
431
|
+
lines.push(renderCells(cells, inner, style));
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
lines.push(blankRow(inner, style));
|
|
435
|
+
|
|
436
|
+
if (plan && view.error) lines.push(textRow(`! ${view.error}`, "error", inner, style));
|
|
437
|
+
if (plan && view.loading) {
|
|
438
|
+
lines.push(textRow("refreshing…", "dim", inner, style));
|
|
439
|
+
} else if (plan && view.updatedAt) {
|
|
440
|
+
lines.push(textRow(`updated ${formatAge(view.updatedAt, now) ?? ""}`.trimEnd(), "dim", inner, style));
|
|
441
|
+
}
|
|
442
|
+
if (!plan) {
|
|
443
|
+
if (view.loading) lines.push(textRow("Loading Nodion plan usage…", "dim", inner, style));
|
|
444
|
+
else if (view.error) lines.push(textRow(`! ${view.error}`, "error", inner, style));
|
|
445
|
+
else lines.push(textRow("No plan data yet.", "dim", inner, style));
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
lines.push(blankRow(inner, style));
|
|
449
|
+
lines.push(textRow("esc close · r refresh", "dim", inner, style));
|
|
450
|
+
lines.push(style.fg("border", `╰${"─".repeat(inner)}╯`));
|
|
451
|
+
return lines;
|
|
452
|
+
}
|