@fenlyin/pi-subscription-usage 2.0.1 → 2.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/README.md CHANGED
@@ -96,7 +96,8 @@ pi install /path/to/pi-subscription-usage
96
96
 
97
97
  Sign in through `/login` as described above, then select a model from that provider. From then on:
98
98
 
99
- - The footer status bar shows the active quota windows and refreshes after each turn and every 5 minutes (using a 5-minute cache).
99
+ - The footer status bar shows the active quota windows and refreshes after each turn and every 5 minutes (using a 5-minute cache). A refresh never shows a loading placeholder: the previous value stays up and the new one replaces it when the request finishes.
100
+ - Each window is colored by how much quota is left — green from 50%, yellow below 50%, red below 20% — using the active Pi theme. In `used` mode the color still follows the remaining quota, and labels plus reset countdowns stay muted so the number stands out.
100
101
  - Running `/usage` always fetches fresh data for the active provider and shows progress bars, reset times (`MM/DD HH:mm`), and account details.
101
102
  - Claude is the exception: its quota is never fetched, only read from Claude Code's responses (see [Claude (Claude Code)](#claude-claude-code)).
102
103
 
package/README.zh-CN.md CHANGED
@@ -96,7 +96,8 @@ pi install /path/to/pi-subscription-usage
96
96
 
97
97
  按上面的方法登录,再选一个该提供商的模型,之后:
98
98
 
99
- - 底部状态栏会自动显示当前额度窗口,在每轮对话结束后及每 5 分钟刷新一次(带 5 分钟缓存)。
99
+ - 底部状态栏会自动显示当前额度窗口,在每轮对话结束后及每 5 分钟刷新一次(带 5 分钟缓存)。刷新过程中不会显示加载占位符:旧数值继续留在状态栏,请求完成后直接替换成新数值。
100
+ - 每个窗口按剩余额度着色——剩余 50% 以上为绿色,50% 以下为黄色,20% 以下为红色——颜色取自当前 Pi 主题。`used` 模式下颜色依旧按剩余额度判断;标签和重置倒计时保持弱化配色,突出百分比数字。
100
101
  - 执行 `/usage` 会跳过缓存直接拉取最新数据,显示进度条、重置时间(`MM/DD HH:mm`)及账号信息。
101
102
  - Claude 例外:不会主动查询额度,只读取 Claude Code 回复中附带的数据(见 [Claude (Claude Code)](#claude-claude-code))。
102
103
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fenlyin/pi-subscription-usage",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Show subscription quota for OpenAI, Codex, OpenCode Go, Grok, Kimi, and Claude (via pi-claude-bridge), plus OpenAI and Codex account reset credits in Pi",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/status.ts CHANGED
@@ -96,23 +96,59 @@ export function buildUsageStatusEvent(
96
96
  };
97
97
  }
98
98
 
99
+ /**
100
+ * Footer tone for a window, chosen by how much quota is left rather than by the
101
+ * percentage the footer happens to display. Rounded first so the color always
102
+ * agrees with the number next to it.
103
+ */
104
+ export function usageStatuslineTone(
105
+ remainingPercent: number,
106
+ ): "success" | "warning" | "error" {
107
+ const left = Math.round(remainingPercent);
108
+ if (left < 20) return "error";
109
+ if (left < 50) return "warning";
110
+ return "success";
111
+ }
112
+
113
+ /**
114
+ * Optional color hooks for the footer. Pi renders whatever `setStatus()` receives,
115
+ * so a caller holding a theme can colorize each segment; without hooks the line
116
+ * stays plain for tests, RPC mode, and non-TUI consumers.
117
+ */
118
+ export type UsageStatuslineStyle = {
119
+ /** Wraps a window label such as `5h`. */
120
+ label?: (text: string) => string;
121
+ /** Wraps the displayed percentage; `remainingPercent` is always what is left. */
122
+ percent?: (text: string, remainingPercent: number) => string;
123
+ /** Wraps `↻ 2h13m` as one unit. */
124
+ countdown?: (text: string) => string;
125
+ /** Wraps the ` · ` separator between windows. */
126
+ separator?: (text: string) => string;
127
+ };
128
+
99
129
  /** Plain fallback for Pi's default footer. Rich rendering belongs to session-ui. */
100
130
  export function formatUsageStatusline(
101
131
  report: UsageReport,
102
132
  model?: UsageModel,
103
133
  displayMode: UsageDisplayMode = DEFAULT_USAGE_DISPLAY_MODE,
104
134
  nowMs: number = Date.now(),
135
+ style?: UsageStatuslineStyle,
105
136
  ): string | undefined {
106
137
  const event = buildUsageStatusEvent(report, model, displayMode, nowMs);
107
138
  if (event.status !== "ready" || event.windows.length === 0) return undefined;
108
- return event.windows
109
- .map((window) => {
110
- const percent = `${window.label} ${formatUsagePercent(window.displayPercent)}%`;
111
- return window.resetCountdown
112
- ? `${percent} ↻ ${window.resetCountdown}`
113
- : percent;
114
- })
115
- .join(" · ");
139
+ const windows = event.windows.map((window) => {
140
+ const label = style?.label?.(window.label) ?? window.label;
141
+ const percentText = `${formatUsagePercent(window.displayPercent)}%`;
142
+ const percent = style?.percent
143
+ ? style.percent(percentText, window.remainingPercent)
144
+ : percentText;
145
+ const head = `${label} ${percent}`;
146
+ if (!window.resetCountdown) return head;
147
+ const countdown = `↻ ${window.resetCountdown}`;
148
+ return `${head} ${style?.countdown?.(countdown) ?? countdown}`;
149
+ });
150
+ if (windows.length === 1) return windows[0];
151
+ return windows.join(style?.separator?.(" · ") ?? " · ");
116
152
  }
117
153
 
118
154
  /** Compact two-unit countdown such as `3d4h`, `2h13m`, or `<1m`; past resets yield nothing. */
package/src/usage.ts CHANGED
@@ -41,8 +41,10 @@ import {
41
41
  buildUsageStatusEvent,
42
42
  formatUsageStatusline,
43
43
  type UsageStatusEvent,
44
+ type UsageStatuslineStyle,
44
45
  unavailableUsageStatusEvent,
45
46
  USAGE_STATUS_EVENT,
47
+ usageStatuslineTone,
46
48
  } from "./status.ts";
47
49
  import {
48
50
  adapterForProvider,
@@ -82,6 +84,9 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
82
84
  let statusGeneration = 0;
83
85
  let statusController: AbortController | undefined;
84
86
  let statusTimer: ReturnType<typeof setTimeout> | undefined;
87
+ // Identity of the model whose ready status is on screen. A refresh of the same
88
+ // model keeps it visible; only a different model blanks the footer first.
89
+ let statusModelKey: string | undefined;
85
90
  // Holds no credential, so session_shutdown leaves it; nothing reads it after.
86
91
  let claudeObservation: UsageReport | undefined;
87
92
 
@@ -116,6 +121,7 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
116
121
  statusController?.abort();
117
122
  statusController = undefined;
118
123
  clearStatusTimer();
124
+ statusModelKey = undefined;
119
125
  safeSetStatus(ctx, undefined);
120
126
  emitUnavailableUsage();
121
127
  }
@@ -145,12 +151,14 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
145
151
  outcome.state.status === "pending"
146
152
  ) {
147
153
  clearStatusTimer();
154
+ statusModelKey = undefined;
148
155
  safeSetStatus(ctx, undefined);
149
156
  emitUnavailableUsage();
150
157
  return;
151
158
  }
152
159
  if (outcome.state.status !== "ready") {
153
160
  emitUnavailableUsage();
161
+ statusModelKey = undefined;
154
162
  if (
155
163
  safeSetStatus(
156
164
  ctx,
@@ -174,8 +182,10 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
174
182
  model,
175
183
  displayMode,
176
184
  nowMs,
185
+ statuslineStyle(ctx),
177
186
  );
178
187
  if (!safeSetStatus(ctx, value)) return;
188
+ statusModelKey = modelIdentity(model);
179
189
  if (schedule && sessionActive) scheduleStatusRefresh(ctx, model);
180
190
  }
181
191
 
@@ -389,8 +399,14 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
389
399
  !controller.signal.aborted &&
390
400
  modelIdentity(ctx.model) === modelIdentity(model);
391
401
  try {
392
- // An observed provider answers immediately; a placeholder would only flicker.
393
- if (!observed) safeSetStatus(ctx, "usage …");
402
+ // Never announce the request itself: a same-model refresh keeps the last
403
+ // value on screen and the fresh one replaces it, so the request is invisible.
404
+ const modelKey = modelIdentity(model);
405
+ if (statusModelKey !== undefined && statusModelKey !== modelKey) {
406
+ statusModelKey = undefined;
407
+ safeSetStatus(ctx, undefined);
408
+ emitUnavailableUsage();
409
+ }
394
410
  const outcome = await queryCurrentState(
395
411
  ctx,
396
412
  model,
@@ -413,6 +429,7 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
413
429
  }
414
430
  if (!refreshIsCurrent()) return;
415
431
  try {
432
+ statusModelKey = undefined;
416
433
  safeSetStatus(ctx, "usage error");
417
434
  } catch {
418
435
  // Error reporting must not stop future refreshes.
@@ -669,6 +686,7 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
669
686
  statusController = undefined;
670
687
  cache.clear();
671
688
  failureBackoff.clear();
689
+ statusModelKey = undefined;
672
690
  safeSetStatus(ctx, undefined);
673
691
  emitUnavailableUsage();
674
692
  });
@@ -677,3 +695,16 @@ export default function subscriptionUsage(pi: ExtensionAPI): void {
677
695
  function isObservedProvider(model: UsageModel | undefined): boolean {
678
696
  return model?.provider === CLAUDE_BRIDGE_PROVIDER_ID;
679
697
  }
698
+
699
+ /** Colorizes the statusline with the active theme; without one the line stays plain. */
700
+ function statuslineStyle(ctx: ExtensionContext): UsageStatuslineStyle {
701
+ const theme = ctx.ui?.theme;
702
+ if (typeof theme?.fg !== "function") return {};
703
+ return {
704
+ label: (text) => theme.fg("muted", text),
705
+ percent: (text, remainingPercent) =>
706
+ theme.fg(usageStatuslineTone(remainingPercent), text),
707
+ countdown: (text) => theme.fg("dim", text),
708
+ separator: (text) => theme.fg("borderMuted", text),
709
+ };
710
+ }