Skip to content

Commit f2b808e

Browse files
committed
fix: address PR review feedback
- Extract repeated token limit parsing logic into parseTokenLimit helper function - Add debug logging for skipped invalid JSON lines in data loader - Update FiveHourBlock type documentation to clarify gap block endTime semantics - Add blocks-specific options documentation to README Addresses feedback from code review bots on PR #23
1 parent b7d4cfc commit f2b808e

File tree

4 files changed

+22
-41
lines changed

4 files changed

+22
-41
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,12 @@ The blocks report helps you understand Claude Code's 5-hour rolling session wind
257257
- Sessions start with your first message and last for 5 hours
258258
- Shows active blocks with time remaining and burn rate projections
259259
- Helps track if you're approaching token limits within a session
260-
- The `-t max` option automatically uses your highest previous session as the limit
260+
- The `-t max` option automatically uses your highest previous block as the limit
261+
262+
#### Blocks-specific options:
263+
- `-t, --token-limit <number|max>`: Set token limit for quota warnings (use "max" for highest previous block)
264+
- `-a, --active`: Show only active block with detailed projections
265+
- `-r, --recent`: Show blocks from last 3 days (including active)
261266

262267
### Options
263268

src/commands/blocks.ts

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ function formatModels(models: string[]): string {
5050
return `${models[0]} +${models.length - 1}`;
5151
}
5252

53+
function parseTokenLimit(value: string | undefined, maxFromAll: number): number | undefined {
54+
if (!value) { return undefined; }
55+
if (value === 'max') {
56+
return maxFromAll > 0 ? maxFromAll : undefined;
57+
}
58+
const limit = Number.parseInt(value, 10);
59+
return Number.isNaN(limit) ? undefined : limit;
60+
}
61+
5362
export const blocksCommand = define({
5463
name: 'blocks',
5564
description: 'Show usage report grouped by 5-hour billing blocks',
@@ -156,18 +165,7 @@ export const blocksCommand = define({
156165
projection,
157166
tokenLimitStatus: projection && ctx.values.tokenLimit
158167
? (() => {
159-
let limit: number | undefined;
160-
if (ctx.values.tokenLimit === 'max') {
161-
// Use the max calculated from ALL blocks before filtering
162-
limit = maxTokensFromAll > 0 ? maxTokensFromAll : undefined;
163-
}
164-
else {
165-
limit = Number.parseInt(ctx.values.tokenLimit, 10);
166-
if (isNaN(limit)) {
167-
limit = undefined;
168-
}
169-
}
170-
168+
const limit = parseTokenLimit(ctx.values.tokenLimit, maxTokensFromAll);
171169
return limit
172170
? {
173171
limit,
@@ -229,18 +227,7 @@ export const blocksCommand = define({
229227

230228
if (ctx.values.tokenLimit) {
231229
// Parse token limit
232-
let limit: number | undefined;
233-
if (ctx.values.tokenLimit === 'max') {
234-
// Use the max calculated from ALL blocks before filtering
235-
limit = maxTokensFromAll > 0 ? maxTokensFromAll : undefined;
236-
}
237-
else {
238-
limit = Number.parseInt(ctx.values.tokenLimit, 10);
239-
if (isNaN(limit)) {
240-
limit = undefined;
241-
}
242-
}
243-
230+
const limit = parseTokenLimit(ctx.values.tokenLimit, maxTokensFromAll);
244231
if (limit) {
245232
const currentTokens = block.tokenCounts.inputTokens + block.tokenCounts.outputTokens;
246233
const remainingTokens = Math.max(0, limit - currentTokens);
@@ -265,19 +252,7 @@ export const blocksCommand = define({
265252
logger.box('Claude Code Token Usage Report - 5-Hour Blocks');
266253

267254
// Calculate token limit if "max" is specified
268-
let actualTokenLimit: number | undefined;
269-
if (ctx.values.tokenLimit) {
270-
if (ctx.values.tokenLimit === 'max') {
271-
// Use the max calculated from ALL blocks before filtering
272-
actualTokenLimit = maxTokensFromAll > 0 ? maxTokensFromAll : undefined;
273-
}
274-
else {
275-
actualTokenLimit = Number.parseInt(ctx.values.tokenLimit, 10);
276-
if (isNaN(actualTokenLimit)) {
277-
actualTokenLimit = undefined;
278-
}
279-
}
280-
}
255+
const actualTokenLimit = parseTokenLimit(ctx.values.tokenLimit, maxTokensFromAll);
281256

282257
const tableHeaders = ['Block Start', 'Duration/Status', 'Models', 'Tokens'];
283258
const tableAligns: ('left' | 'right')[] = ['left', 'left', 'left', 'right'];

src/data-loader.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,9 @@ export async function loadFiveHourBlockData(
883883
version: data.version,
884884
});
885885
}
886-
catch {
887-
// Skip invalid JSON lines
886+
catch (error) {
887+
// Skip invalid JSON lines but log for debugging purposes
888+
logger.debug(`Skipping invalid JSON line in 5-hour blocks: ${error instanceof Error ? error.message : String(error)}`);
888889
}
889890
}
890891
}

src/utils/five-hour-blocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type TokenCounts = {
2323
export type FiveHourBlock = {
2424
id: string; // ISO string of block start time
2525
startTime: Date;
26-
endTime: Date; // startTime + 5 hours
26+
endTime: Date; // startTime + 5 hours (for normal blocks) or gap end time (for gap blocks)
2727
actualEndTime?: Date; // Last activity in block
2828
isActive: boolean;
2929
isGap?: boolean; // True if this is a gap block

0 commit comments

Comments
 (0)