Skip to content

Commit 16663a4

Browse files
committed
fix: preserve decimal quota percentages
1 parent 33453cf commit 16663a4

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

‎src/output/quota-table.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ function isUnavailablePlan(model: QuotaModelRemain): boolean {
111111
}
112112

113113
function clampPct(value: number): number {
114-
return Math.max(0, Math.min(MAX_DISPLAY_PCT, Math.round(value)));
114+
const rounded = Math.round((value + Number.EPSILON) * 100) / 100;
115+
return Math.max(0, Math.min(MAX_DISPLAY_PCT, rounded));
116+
}
117+
118+
function formatPct(value: number): string {
119+
return `${clampPct(value).toFixed(2)}%`;
115120
}
116121

117122
function boostFactor(boostPermille: number | undefined | null): number {
@@ -137,7 +142,7 @@ function renderBar(remainingPct: number, color: boolean, barWidth: number = BAR_
137142
const ratio = Math.min(1, pct / 100);
138143
const filled = Math.round(barWidth * ratio);
139144
const empty = barWidth - filled;
140-
const pctStr = `${pct}%`.padStart(4);
145+
const pctStr = formatPct(pct).padStart(4);
141146
if (!color) {
142147
const bar = `[${'█'.repeat(filled)}${'.'.repeat(empty)}]`;
143148
return showPct ? `${bar} ${pctStr}` : bar;

‎test/output/quota-table.test.ts‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,46 @@ describe('renderQuotaTable', () => {
122122
const output = lines.join('\n');
123123

124124
expect(output).toContain('通用');
125-
expect(output).toContain('剩余 [█████████.] 94%');
126-
expect(output).toContain('周剩余 [██████████] 98%');
125+
expect(output).toContain('剩余 [█████████.] 94.00%');
126+
expect(output).toContain('周剩余 [██████████] 98.00%');
127127
expect(output).toContain('视频');
128128
expect(output).toContain('3 / 3');
129129
expect(output).toContain('21 / 21');
130130
expect(output).not.toContain('0 / 3');
131131
});
132132

133+
it('preserves fractional remaining percentages in compact bars', () => {
134+
const lines: string[] = [];
135+
const originalLog = console.log;
136+
137+
console.log = (message?: unknown) => {
138+
lines.push(String(message ?? ''));
139+
};
140+
141+
try {
142+
renderQuotaTable(
143+
[
144+
{
145+
...createModel(),
146+
current_interval_total_count: 0,
147+
current_interval_usage_count: 0,
148+
current_interval_remaining_percent: 85.58,
149+
current_weekly_total_count: 0,
150+
current_weekly_usage_count: 0,
151+
current_weekly_remaining_percent: 97.01,
152+
},
153+
],
154+
{ ...createConfig(), noColor: true },
155+
);
156+
} finally {
157+
console.log = originalLog;
158+
}
159+
160+
const output = lines.join('\n');
161+
expect(output).toContain('Left [█████████.] 85.58%');
162+
expect(output).toContain('Wk left [██████████] 97.01%');
163+
});
164+
133165
it('uses remaining percent to disambiguate newer used-count responses', () => {
134166
const lines: string[] = [];
135167
const originalLog = console.log;
@@ -190,7 +222,7 @@ describe('renderQuotaTable', () => {
190222
}
191223

192224
const output = lines.join('\n');
193-
expect(output).toContain('Left [████......] 40%');
225+
expect(output).toContain('Left [████......] 40.00%');
194226
expect(output).not.toContain('7 / 10');
195227
expect(output).not.toContain('3 / 10');
196228
});
@@ -281,7 +313,7 @@ describe('renderQuotaTable', () => {
281313
}
282314

283315
const output = lines.join('\n');
284-
expect(output).toContain('Wk left [██████████] 150%');
316+
expect(output).toContain('Wk left [██████████] 150.00%');
285317
});
286318

287319
it('clamps boosted weekly percent at MAX_DISPLAY_PCT (200)', () => {
@@ -310,7 +342,7 @@ describe('renderQuotaTable', () => {
310342
}
311343

312344
const output = lines.join('\n');
313-
expect(output).toContain('200%');
345+
expect(output).toContain('200.00%');
314346
expect(output).not.toContain('300%');
315347
});
316348

0 commit comments

Comments
 (0)