cctotals 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/token-graph.js +38 -45
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cctotals",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Analyze and visualize Claude Code token usage with weekly graphs",
5
5
  "main": "token-graph.js",
6
6
  "bin": {
package/token-graph.js CHANGED
@@ -15,7 +15,6 @@ const stats = JSON.parse(fs.readFileSync(statsPath, 'utf8'));
15
15
  // Group daily tokens into weeks
16
16
  function getWeekKey(dateStr) {
17
17
  const date = new Date(dateStr);
18
- // Get ISO week start (Monday)
19
18
  const day = date.getDay();
20
19
  const diff = date.getDate() - day + (day === 0 ? -6 : 1);
21
20
  const weekStart = new Date(date.setDate(diff));
@@ -24,7 +23,6 @@ function getWeekKey(dateStr) {
24
23
 
25
24
  const weeklyTokens = {};
26
25
 
27
- // Process daily model tokens
28
26
  for (const dayData of stats.dailyModelTokens) {
29
27
  const weekKey = getWeekKey(dayData.date);
30
28
  if (!weeklyTokens[weekKey]) {
@@ -37,67 +35,60 @@ for (const dayData of stats.dailyModelTokens) {
37
35
  }
38
36
  }
39
37
 
40
- // Sort weeks chronologically
41
38
  const sortedWeeks = Object.keys(weeklyTokens).sort();
42
-
43
- // Find max for scaling
44
39
  const maxTokens = Math.max(...sortedWeeks.map(w => weeklyTokens[w].total));
45
40
 
46
- // Chart settings
47
- const CHART_WIDTH = 60;
48
- const LABEL_WIDTH = 12;
49
- const BAR_HEIGHT = 1;
50
- const SCALE = CHART_WIDTH / maxTokens;
51
-
52
- // Colors for terminal (Windows-compatible)
53
- const colors = {
54
- header: '\x1b[36m', // Cyan
55
- label: '\x1b[33m', // Yellow
56
- bar: '\x1b[32m', // Green
57
- total: '\x1b[35m', // Magenta
58
- reset: '\x1b[0m'
41
+ // Colors
42
+ const c = {
43
+ hdr: '\x1b[36m',
44
+ lbl: '\x1b[33m',
45
+ bar: '\x1b[32m',
46
+ num: '\x1b[35m',
47
+ res: '\x1b[0m'
59
48
  };
60
49
 
61
- console.log('\n' + colors.header + '═'.repeat(70));
62
- console.log(' WEEKLY TOKEN USAGE GRAPH');
63
- console.log('═'.repeat(70) + colors.reset);
64
- console.log();
50
+ // Terminal width
51
+ const W = process.stdout.columns || 80;
52
+ const CHART_W = Math.min(50, W - 30);
53
+ const PAD = Math.floor((W - CHART_W - 30) / 2);
65
54
 
66
- // Draw the chart
67
- console.log(colors.label + ' Week Start ' + colors.reset + '|' + colors.bar + ' Tokens (scaled)' + colors.reset);
55
+ // Header
56
+ console.log('\n' + c.hdr + ''.repeat(W));
57
+ console.log(' '.repeat(PAD) + 'WEEKLY TOKEN USAGE GRAPH');
58
+ console.log('═'.repeat(W) + c.res);
59
+ console.log();
68
60
 
69
- // Find longest bar needed for alignment
70
- const maxBarWidth = Math.floor(maxTokens / (maxTokens / CHART_WIDTH));
71
- const chartLine = ''.repeat(LABEL_WIDTH) + '' + '─'.repeat(CHART_WIDTH);
72
- console.log(colors.label + chartLine + colors.reset);
61
+ // Column headers
62
+ const hdr = ' Week Tokens';
63
+ console.log(c.lbl + hdr + ' '.repeat(CHART_W - hdr.length + 5) + '| Bar' + c.res);
64
+ console.log(c.lbl + '─'.repeat(W) + c.res);
73
65
 
74
66
  let grandTotal = 0;
75
67
  for (const week of sortedWeeks) {
76
68
  const data = weeklyTokens[week];
77
- const barLen = Math.max(1, Math.floor(data.total / (maxTokens / CHART_WIDTH)));
78
- const bar = colors.bar + '█'.repeat(barLen) + colors.reset;
79
- const label = colors.label + week + colors.reset;
80
- const spaces = ' '.repeat(Math.max(0, LABEL_WIDTH - label.length));
69
+ const barLen = Math.max(1, Math.floor((data.total / maxTokens) * CHART_W));
81
70
 
82
- // Format token count with commas
71
+ // Format: week left-aligned, tokens right-aligned
72
+ const weekStr = week;
83
73
  const tokenStr = data.total.toLocaleString();
84
- const padding = ' '.repeat(Math.max(0, CHART_WIDTH - barLen - tokenStr.length - 3));
74
+ const bar = c.bar + '█'.repeat(barLen) + c.res;
85
75
 
86
- console.log(` ${label} │${bar}${padding} ${colors.total}${tokenStr}${colors.reset}`);
76
+ const line = ` ${c.lbl}${weekStr}${c.res} ${c.num}${tokenStr}${c.res} ${bar}`;
77
+ console.log(line);
87
78
  grandTotal += data.total;
88
79
  }
89
80
 
90
- // Footer
91
- console.log(colors.label + chartLine + colors.reset);
92
- console.log(`\n ${colors.header}Total:${colors.reset} ${colors.total}${grandTotal.toLocaleString()}${colors.reset} tokens across ${sortedWeeks.length} weeks`);
93
- console.log(` ${colors.header}Average:${colors.reset} ${Math.round(grandTotal / sortedWeeks.length).toLocaleString()} tokens/week`);
94
- console.log('\n' + colors.header + '═'.repeat(70) + colors.reset + '\n');
81
+ console.log(c.lbl + '─'.repeat(W) + c.res);
82
+
83
+ const avg = Math.round(grandTotal / sortedWeeks.length);
84
+ console.log(`\n Total: ${c.num}${grandTotal.toLocaleString()}${c.res} tokens (${sortedWeeks.length} weeks)`);
85
+ console.log(` Average: ${c.num}${avg.toLocaleString()}${c.res} tokens/week`);
86
+ console.log('\n' + c.hdr + '═'.repeat(W) + c.res + '\n');
95
87
 
96
- // Detailed breakdown by model
97
- console.log(colors.header + ' DETAILED BREAKDOWN BY MODEL' + colors.reset);
98
- console.log('─'.repeat(50));
88
+ // Model breakdown
89
+ console.log(c.hdr + ' MODEL BREAKDOWN' + c.res);
90
+ console.log(c.lbl + '─'.repeat(W) + c.res);
99
91
 
100
- // Aggregate model stats
101
92
  const modelTotals = {};
102
93
  for (const week of sortedWeeks) {
103
94
  for (const [model, tokens] of Object.entries(weeklyTokens[week].byModel)) {
@@ -105,8 +96,10 @@ for (const week of sortedWeeks) {
105
96
  }
106
97
  }
107
98
 
99
+ const maxModelLen = Math.max(...Object.keys(modelTotals).map(m => m.length));
108
100
  for (const [model, tokens] of Object.entries(modelTotals)) {
109
101
  const pct = ((tokens / grandTotal) * 100).toFixed(1);
110
- console.log(` ${model.padEnd(30)} ${tokens.toLocaleString().padStart(12)} (${pct}%)`);
102
+ const padded = model.padEnd(maxModelLen);
103
+ console.log(` ${c.lbl}${padded}${c.res} ${c.num}${tokens.toLocaleString()}${c.res} (${pct}%)`);
111
104
  }
112
105
  console.log();