Skip to content

Commit 7f12573

Browse files
authored
Merge pull request #618 from rajbos/rajbos/fix-co2-kg-display
feat: display CO2 in kg when ≥ 1 000 g for readability
2 parents 4e5d168 + 929abc4 commit 7f12573

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

cli/src/commands/environmental.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { Command } from 'commander';
55
import chalk from 'chalk';
6-
import { discoverSessionFiles, calculateDetailedStats, fmt, formatTokens, ENVIRONMENTAL } from '../helpers';
6+
import { discoverSessionFiles, calculateDetailedStats, formatTokens, ENVIRONMENTAL } from '../helpers';
77
import type { PeriodStats } from '../../../vscode-extension/src/types';
88

99
export const environmentalCommand = new Command('environmental')
@@ -38,7 +38,7 @@ export const environmentalCommand = new Command('environmental')
3838
console.log(chalk.dim('Methodology: Estimates based on industry averages for AI inference'));
3939
console.log(chalk.dim(` CO₂: ${ENVIRONMENTAL.CO2_PER_1K_TOKENS} gCO₂e per 1K tokens`));
4040
console.log(chalk.dim(` Water: ${ENVIRONMENTAL.WATER_USAGE_PER_1K_TOKENS} L per 1K tokens`));
41-
console.log(chalk.dim(` Tree absorption: ${fmt(ENVIRONMENTAL.CO2_ABSORPTION_PER_TREE_PER_YEAR)} g CO₂/year\n`));
41+
console.log(chalk.dim(` Tree absorption: ${formatCo2(ENVIRONMENTAL.CO2_ABSORPTION_PER_TREE_PER_YEAR)} CO₂/year\n`));
4242

4343
for (const period of periods) {
4444
printEnvironmentalStats(period.label, period.emoji, period.stats);
@@ -75,6 +75,14 @@ export const environmentalCommand = new Command('environmental')
7575
console.log(chalk.dim(`Last updated: ${stats.lastUpdated.toLocaleString()}\n`));
7676
});
7777

78+
/** Format CO₂ in grams, switching to kg notation when ≥ 1 000 g */
79+
function formatCo2(grams: number): string {
80+
if (grams >= 1000) {
81+
return `${(grams / 1000).toFixed(2)} kgCO₂e`;
82+
}
83+
return `${grams.toFixed(3)} gCO₂e`;
84+
}
85+
7886
function printEnvironmentalStats(label: string, emoji: string, stats: PeriodStats): void {
7987
console.log(chalk.bold(`${emoji} ${label}`));
8088
console.log(chalk.dim('─'.repeat(55)));
@@ -86,7 +94,7 @@ function printEnvironmentalStats(label: string, emoji: string, stats: PeriodStat
8694
}
8795

8896
console.log(` Tokens used: ${chalk.bold.yellow(formatTokens(stats.tokens))}`);
89-
console.log(` CO₂ emissions: ${chalk.bold(stats.co2.toFixed(3))} gCO₂e`);
97+
console.log(` CO₂ emissions: ${chalk.bold(formatCo2(stats.co2))}`);
9098
console.log(` Water usage: ${chalk.bold(stats.waterUsage.toFixed(3))} liters`);
9199

92100
if (stats.treesEquivalent > 0) {

vscode-extension/src/webview/environmental/main.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ function smartFixed(value: number): string {
7979
return formatFixed(Math.round(value), 0);
8080
}
8181

82+
/** Format CO₂ in grams, switching to kg notation when ≥ 1 000 g */
83+
function formatCo2Grams(grams: number): string {
84+
if (grams >= 1000) {
85+
return `${smartFixed(grams / 1000)} kg`;
86+
}
87+
return `${smartFixed(grams)} g`;
88+
}
89+
8290
type AnalogyItem = { icon: string; text: string };
8391

8492
const co2AnalogyItems = (grams: number): AnalogyItem[] => [
@@ -188,10 +196,10 @@ function buildImpactCards(
188196
],
189197
// CO₂ card
190198
[
191-
['📅 Today', `${smartFixed(stats.today.co2)} g`, co2AnalogyItems(stats.today.co2)],
192-
['📈 Last 30 Days', `${smartFixed(stats.last30Days.co2)} g`, co2AnalogyItems(stats.last30Days.co2)],
193-
['📆 Previous Month', `${smartFixed(stats.lastMonth.co2)} g`, co2AnalogyItems(stats.lastMonth.co2)],
194-
['🌍 Projected Year', `${smartFixed(projectedCo2)} g`, co2AnalogyItems(projectedCo2)],
199+
['📅 Today', formatCo2Grams(stats.today.co2), co2AnalogyItems(stats.today.co2)],
200+
['📈 Last 30 Days', formatCo2Grams(stats.last30Days.co2), co2AnalogyItems(stats.last30Days.co2)],
201+
['📆 Previous Month', formatCo2Grams(stats.lastMonth.co2), co2AnalogyItems(stats.lastMonth.co2)],
202+
['🌍 Projected Year', formatCo2Grams(projectedCo2), co2AnalogyItems(projectedCo2)],
195203
],
196204
// Water card
197205
[

0 commit comments

Comments
 (0)