Skip to content

Commit 219bda6

Browse files
committed
fix: implement responsive table layout for narrow terminals
Resolves #81 by adding proper responsive table support that prevents table layout disruption in narrow terminal widths. Changes: - Add ResponsiveTable utility class using cli-table3 built-in wordWrap feature - Implement dynamic column width calculation based on terminal width - Add intelligent column width allocation (wider for text, narrower for numbers) - Replace cli-table3 usage in daily, monthly, and session commands - Update pushBreakdownRows to work with new table interface - Maintain existing visual styling and user experience Features: - Automatic text wrapping at word boundaries for long content - Proportional column width distribution with minimum width guarantees - Special handling for Models column which tends to have longer text - Responsive behavior that adapts to any terminal width Technical details: - Uses cli-table3 wordWrap: true and colWidths options - Calculates available width after accounting for table borders/separators - Applies different width multipliers based on column content type - Preserves all existing table styling (colors, alignment, etc.)
1 parent 9812373 commit 219bda6

5 files changed

Lines changed: 86 additions & 8 deletions

File tree

‎src/commands/daily.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import process from 'node:process';
2-
import Table from 'cli-table3';
32
import { define } from 'gunshi';
43
import pc from 'picocolors';
54
import {
@@ -12,6 +11,7 @@ import { detectMismatches, printMismatchReport } from '../debug.ts';
1211
import { log, logger } from '../logger.ts';
1312
import { sharedCommandConfig } from '../shared-args.internal.ts';
1413
import { formatCurrency, formatModelsDisplay, formatNumber, pushBreakdownRows } from '../utils.internal.ts';
14+
import { ResponsiveTable } from '../utils.table.ts';
1515

1616
export const dailyCommand = define({
1717
name: 'daily',
@@ -73,7 +73,7 @@ export const dailyCommand = define({
7373
logger.box('Claude Code Token Usage Report - Daily');
7474

7575
// Create table
76-
const table = new Table({
76+
const table = new ResponsiveTable({
7777
head: [
7878
'Date',
7979
'Models',

‎src/commands/monthly.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import process from 'node:process';
2-
import Table from 'cli-table3';
32
import { define } from 'gunshi';
43
import pc from 'picocolors';
54
import {
@@ -12,6 +11,7 @@ import { detectMismatches, printMismatchReport } from '../debug.ts';
1211
import { log, logger } from '../logger.ts';
1312
import { sharedCommandConfig } from '../shared-args.internal.ts';
1413
import { formatCurrency, formatModelsDisplay, formatNumber, pushBreakdownRows } from '../utils.internal.ts';
14+
import { ResponsiveTable } from '../utils.table.ts';
1515

1616
export const monthlyCommand = define({
1717
name: 'monthly',
@@ -84,7 +84,7 @@ export const monthlyCommand = define({
8484
logger.box('Claude Code Token Usage Report - Monthly');
8585

8686
// Create table
87-
const table = new Table({
87+
const table = new ResponsiveTable({
8888
head: [
8989
'Month',
9090
'Models',

‎src/commands/session.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import process from 'node:process';
2-
import Table from 'cli-table3';
32
import { define } from 'gunshi';
43
import pc from 'picocolors';
54
import {
@@ -12,6 +11,7 @@ import { detectMismatches, printMismatchReport } from '../debug.ts';
1211
import { log, logger } from '../logger.ts';
1312
import { sharedCommandConfig } from '../shared-args.internal.ts';
1413
import { formatCurrency, formatModelsDisplay, formatNumber, pushBreakdownRows } from '../utils.internal.ts';
14+
import { ResponsiveTable } from '../utils.table.ts';
1515

1616
export const sessionCommand = define({
1717
name: 'session',
@@ -74,7 +74,7 @@ export const sessionCommand = define({
7474
logger.box('Claude Code Token Usage Report - By Session');
7575

7676
// Create table
77-
const table = new Table({
77+
const table = new ResponsiveTable({
7878
head: [
7979
'Session',
8080
'Models',

‎src/utils.internal.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ export function formatModelsDisplay(models: string[]): string {
2929
/**
3030
* Pushes model breakdown rows to a table
3131
* @param table - The table to push rows to
32+
* @param table.push - Method to add rows to the table
3233
* @param breakdowns - Array of model breakdowns
3334
* @param extraColumns - Number of extra empty columns before the data (default: 1 for models column)
3435
* @param trailingColumns - Number of extra empty columns after the data (default: 0)
3536
*/
3637
export function pushBreakdownRows(
37-
table: any[],
38+
table: { push: (row: (string | number)[]) => void },
3839
breakdowns: Array<{
3940
modelName: string;
4041
inputTokens: number;
@@ -47,7 +48,7 @@ export function pushBreakdownRows(
4748
trailingColumns = 0,
4849
): void {
4950
for (const breakdown of breakdowns) {
50-
const row: any[] = [` └─ ${formatModelName(breakdown.modelName)}`];
51+
const row: (string | number)[] = [` └─ ${formatModelName(breakdown.modelName)}`];
5152

5253
// Add extra empty columns before data
5354
for (let i = 0; i < extraColumns; i++) {

‎src/utils.table.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import process from 'node:process';
2+
import Table from 'cli-table3';
3+
4+
type TableRow = (string | number)[];
5+
type TableOptions = {
6+
head: string[];
7+
colAligns?: ('left' | 'right' | 'center')[];
8+
style?: {
9+
head?: string[];
10+
};
11+
};
12+
13+
export class ResponsiveTable {
14+
private head: string[];
15+
private rows: TableRow[] = [];
16+
private colAligns: ('left' | 'right' | 'center')[];
17+
private style?: { head?: string[] };
18+
19+
constructor(options: TableOptions) {
20+
this.head = options.head;
21+
this.colAligns = options.colAligns ?? Array.from({ length: this.head.length }, () => 'left');
22+
this.style = options.style;
23+
}
24+
25+
push(row: TableRow): void {
26+
this.rows.push(row);
27+
}
28+
29+
toString(): string {
30+
const terminalWidth = process.stdout.columns || 120;
31+
32+
// Calculate reasonable column widths for responsive layout
33+
const numColumns = this.head.length;
34+
const tableOverhead = 3 * numColumns + 1; // borders and separators
35+
const availableWidth = terminalWidth - tableOverhead;
36+
37+
// Create proportional column widths, with minimums
38+
const baseWidth = Math.floor(availableWidth / numColumns);
39+
const columnWidths = this.head.map((_, index) => {
40+
const align = this.colAligns[index];
41+
// Give wider columns to text content, narrower to numbers
42+
if (align === 'right') {
43+
// Numeric columns - smaller width
44+
return Math.max(Math.floor(baseWidth * 0.7), 8);
45+
}
46+
else if (index === 0) {
47+
// First column (Date/Session) - medium width
48+
return Math.max(Math.floor(baseWidth * 0.8), 10);
49+
}
50+
else if (index === 1) {
51+
// Models column - can be longer, needs more space
52+
return Math.max(Math.floor(baseWidth * 1.2), 12);
53+
}
54+
else {
55+
// Other columns - standard width
56+
return Math.max(baseWidth, 8);
57+
}
58+
});
59+
60+
// Create table with responsive column widths and word wrapping
61+
const table = new Table({
62+
head: this.head,
63+
style: this.style,
64+
colAligns: this.colAligns,
65+
colWidths: columnWidths,
66+
wordWrap: true,
67+
wrapOnWordBoundary: true,
68+
});
69+
70+
// Add all rows to the table
71+
for (const row of this.rows) {
72+
table.push(row);
73+
}
74+
75+
return table.toString();
76+
}
77+
}

0 commit comments

Comments
 (0)