Skip to content

Commit dad26d2

Browse files
authored
Merge pull request #8 from j4rviscmd/feat/usage-display-displaymode
feat: add display mode setting for usage/remaining tokens
2 parents e529ed0 + 69df11f commit dad26d2

3 files changed

Lines changed: 44 additions & 12 deletions

File tree

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
## Features
1717

18-
- Shows token quota usage (e.g. `45% (2h30m)`) in the status bar
18+
- Shows token quota usage (e.g. `45% (2h30m)`) or remaining tokens (e.g. `55% (2h30m)`) in the status bar
1919
- Displays time remaining until quota resets when available
2020
- Automatically refreshes at a configurable interval
2121
- Secure API key storage via VS Code Secret Storage
@@ -26,10 +26,11 @@
2626
| ------------------------------ | --------------- |
2727
| Authenticated, usage available | `⬡ 45% (2h30m)` |
2828
| Authenticated, no reset time | `⬡ 45%` |
29+
| Authenticated, remaining mode | `⬡ 55% (2h30m)` |
2930
| API key not set | `⬡ Set API Key` |
3031
| Error / fetch failed | `⬡ -` |
3132

32-
> The `` icon is the z.ai icon. Set `zaiUsage.useIcon: false` to display `z.ai:` as a text prefix instead.
33+
> The `` icon is the z.ai icon. Set `zaiUsage.useIcon: false` to display `z.ai:` as a text prefix instead. Set `zaiUsage.displayMode: "remaining"` to show remaining tokens instead of usage.
3334
3435
## Setup
3536

@@ -47,10 +48,11 @@
4748

4849
## Settings
4950

50-
| Setting | Type | Default | Description |
51-
| -------------------------- | --------- | ------- | -------------------------------------------------- |
52-
| `zaiUsage.refreshInterval` | `number` | `60` | Data refresh interval in seconds |
53-
| `zaiUsage.useIcon` | `boolean` | `true` | Use z.ai icon (``) instead of text prefix `z.ai:` |
51+
| Setting | Type | Default | Description |
52+
| -------------------------- | --------- | ------- | ------------------------------------------------------------------------------------ |
53+
| `zaiUsage.refreshInterval` | `number` | `60` | Data refresh interval in seconds |
54+
| `zaiUsage.useIcon` | `boolean` | `true` | Use z.ai icon (``) instead of text prefix `z.ai:` |
55+
| `zaiUsage.displayMode` | `string` | `usage` | Display mode: `"usage"` (e.g. `75.3%`) or `"remaining"` (e.g. `24.7%`) in status bar |
5456

5557
## Requirements
5658

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@
7171
"type": "number",
7272
"default": 10000,
7373
"description": "Priority of the z.ai Usage item in the status bar (higher = further left among right-aligned items). Change takes effect after reloading the window."
74+
},
75+
"zaiUsage.displayMode": {
76+
"type": "string",
77+
"enum": ["usage", "remaining"],
78+
"default": "usage",
79+
"enumDescriptions": [
80+
"Display token usage (e.g. 75.3%)",
81+
"Display remaining tokens (e.g. 24.7%)"
82+
],
83+
"description": "Whether to display token usage or remaining tokens in the status bar."
7484
}
7585
}
7686
}

src/extension.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,30 @@ export function activate(context: vscode.ExtensionContext): void {
380380
statusBarItem.command = undefined;
381381
const refreshSec = getRefreshInterval() / 1000;
382382
const resetStr = formatResetTime(usage.nextResetTime);
383+
384+
// Get display mode setting (default: "usage")
385+
const displayMode = vscode.workspace
386+
.getConfiguration("zaiUsage")
387+
.get<string>("displayMode", "usage");
388+
389+
// Calculate display percentage based on display mode
390+
const isRemaining = displayMode === "remaining";
391+
const displayPercentage = isRemaining
392+
? Math.round((100 - usage.percentage) * 10) / 10
393+
: usage.percentage;
394+
383395
const suffix = resetStr
384-
? `${usage.percentage}% ${resetStr}`
385-
: `${usage.percentage}%`;
396+
? `${displayPercentage}% ${resetStr}`
397+
: `${displayPercentage}%`;
386398
statusBarItem.text = getLabel(suffix);
387-
statusBarItem.tooltip = `z.ai token usage: ${usage.percentage}%${resetStr ? ` — resets in ${resetStr.replace(/[()]/g, "")}` : ""} (auto-refreshes every ${refreshSec}s)`;
399+
400+
// Build tooltip text
401+
const resetTooltip = resetStr
402+
? ` — resets in ${resetStr.replace(/[()]/g, "")}`
403+
: "";
404+
statusBarItem.tooltip =
405+
`z.ai token ${displayMode}: ${displayPercentage}%${resetTooltip} ` +
406+
`(auto-refreshes every ${refreshSec}s)`;
388407
}
389408

390409
if (apiCalled) {
@@ -462,8 +481,8 @@ export function activate(context: vscode.ExtensionContext): void {
462481
/**
463482
* Listens for workspace configuration changes and re-applies them immediately.
464483
*
465-
* When `zaiUsage.refreshInterval` or `zaiUsage.useIcon` changes, the status bar
466-
* is refreshed and the polling interval is restarted so the new settings take
484+
* When `zaiUsage.refreshInterval`, `zaiUsage.useIcon`, or `zaiUsage.displayMode` changes,
485+
* the status bar is refreshed and the polling interval is restarted so the new settings take
467486
* effect without requiring a window reload.
468487
*
469488
* When `zaiUsage.statusBarPriority` changes, the user is notified that a window
@@ -473,7 +492,8 @@ export function activate(context: vscode.ExtensionContext): void {
473492
vscode.workspace.onDidChangeConfiguration((e) => {
474493
if (
475494
e.affectsConfiguration("zaiUsage.refreshInterval") ||
476-
e.affectsConfiguration("zaiUsage.useIcon")
495+
e.affectsConfiguration("zaiUsage.useIcon") ||
496+
e.affectsConfiguration("zaiUsage.displayMode")
477497
) {
478498
updateStatusBar();
479499
startInterval();

0 commit comments

Comments
 (0)