Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
425 changes: 425 additions & 0 deletions apps/ccusage/src/_promotions.ts

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions apps/ccusage/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { blocksCommand } from './blocks.ts';
import { dailyCommand } from './daily.ts';
import { monthlyCommand } from './monthly.ts';
import { sessionCommand } from './session.ts';
import { setupStatuslineCommand } from './setup-statusline.ts';
import { statuslineCommand } from './statusline.ts';
import { weeklyCommand } from './weekly.ts';

Expand All @@ -14,6 +15,7 @@ export {
dailyCommand,
monthlyCommand,
sessionCommand,
setupStatuslineCommand,
statuslineCommand,
weeklyCommand,
};
Expand All @@ -28,6 +30,7 @@ export const subCommandUnion = [
['session', sessionCommand],
['blocks', blocksCommand],
['statusline', statuslineCommand],
['setup-statusline', setupStatuslineCommand],
] as const;

/**
Expand Down
257 changes: 257 additions & 0 deletions apps/ccusage/src/commands/setup-statusline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { Result } from '@praha/byethrow';
import { define } from 'gunshi';
import nanoSpawn from 'nano-spawn';
import pc from 'picocolors';
import { DEFAULT_CLAUDE_CODE_PATH, DEFAULT_CLAUDE_CONFIG_PATH, USER_HOME_DIR } from '../_consts.ts';
import { log } from '../logger.ts';

const runnerChoices = ['auto', 'bun', 'npx'] as const;
const visualBurnRateChoices = ['off', 'emoji', 'text', 'emoji-text'] as const;
const costSourceChoices = ['auto', 'ccusage', 'cc', 'both'] as const;
const promotionDisplayChoices = ['auto', 'active-only', 'off'] as const;

/**
* Detects whether bun is available on the system
*/
async function detectBun(): Promise<boolean> {
return Result.pipe(
await Result.try({
try: async () => {
await nanoSpawn('bun', ['--version']);
return true;
},
catch: () => false,
})(),
Result.unwrap(false),
);
}

/**
* Finds the Claude Code settings.json path
* Prefers XDG config path, falls back to legacy path
*/
function findSettingsPath(): { settingsPath: string; isXdg: boolean } {
const xdgSettingsPath = path.join(DEFAULT_CLAUDE_CONFIG_PATH, 'settings.json');
const legacySettingsPath = path.join(USER_HOME_DIR, DEFAULT_CLAUDE_CODE_PATH, 'settings.json');

// Prefer XDG path if it exists, otherwise check legacy
if (existsSync(xdgSettingsPath)) {
return { settingsPath: xdgSettingsPath, isXdg: true };
}
if (existsSync(legacySettingsPath)) {
return { settingsPath: legacySettingsPath, isXdg: false };
}

// Default to XDG path for new installations
return { settingsPath: xdgSettingsPath, isXdg: true };
}

/**
* Builds the statusline command string from options
*/
function buildCommand(
runner: string,
options: {
visualBurnRate: string;
showPromotions: boolean;
promotionDisplay: string;
costSource: string;
showSessionDuration: boolean;
showLinesChanged: boolean;
},
): string {
const prefix = runner === 'bun' ? 'bun x' : 'npx -y';
const parts = [`${prefix} ccusage statusline`];

if (options.visualBurnRate !== 'off') {
parts.push(`--visual-burn-rate ${options.visualBurnRate}`);
}

if (!options.showPromotions) {
parts.push('--no-show-promotions');
}

if (options.promotionDisplay !== 'auto') {
parts.push(`--promotion-display ${options.promotionDisplay}`);
}

if (options.costSource !== 'auto') {
parts.push(`--cost-source ${options.costSource}`);
}

if (!options.showSessionDuration) {
parts.push('--no-show-session-duration');
}

if (!options.showLinesChanged) {
parts.push('--no-show-lines-changed');
}

return parts.join(' ');
}

export const setupStatuslineCommand = define({
name: 'setup-statusline',
description: 'Auto-configure Claude Code statusline integration',
toKebab: true,
args: {
runner: {
type: 'enum',
choices: runnerChoices,
description: 'Package runner: auto (detect bun), bun, or npx',
default: 'auto',
negatable: false,
},
force: {
type: 'boolean',
short: 'f',
description: 'Overwrite existing statusLine configuration',
default: false,
},
dryRun: {
type: 'boolean',
description: 'Show what would be written without making changes',
default: false,
toKebab: true,
},
visualBurnRate: {
type: 'enum',
choices: visualBurnRateChoices,
description: 'Burn rate visualization style',
default: 'off',
negatable: false,
toKebab: true,
},
showPromotions: {
type: 'boolean',
description: 'Enable promotion display in statusline (default: true)',
negatable: true,
default: true,
toKebab: true,
},
costSource: {
type: 'enum',
choices: costSourceChoices,
description: 'Session cost source: auto, ccusage, cc, or both',
default: 'auto',
negatable: false,
toKebab: true,
},
promotionDisplay: {
type: 'enum',
choices: promotionDisplayChoices,
description: 'Promotion display: auto (with countdown), active-only (off-peak only), off',
default: 'auto',
negatable: false,
toKebab: true,
},
showSessionDuration: {
type: 'boolean',
description: 'Show session duration in statusline (default: true)',
negatable: true,
default: true,
toKebab: true,
},
showLinesChanged: {
type: 'boolean',
description: 'Show lines added/removed in statusline (default: true)',
negatable: true,
default: true,
toKebab: true,
},
},
async run(ctx) {
// Detect runner
const resolvedRunner = await (async (): Promise<'bun' | 'npx'> => {
if (ctx.values.runner === 'bun') {
return 'bun';
}
if (ctx.values.runner === 'npx') {
return 'npx';
}
// auto detection
const hasBun = await detectBun();
return hasBun ? 'bun' : 'npx';
})();

log(`${pc.dim('Runner:')} ${pc.bold(resolvedRunner)}`);

// Find settings path
const { settingsPath } = findSettingsPath();
log(`${pc.dim('Settings:')} ${settingsPath}`);

// Read existing settings
const existingSettings: Record<string, unknown> | null = existsSync(settingsPath)
? Result.pipe(
Result.try({
try: () => {
const parsed: unknown = JSON.parse(readFileSync(settingsPath, 'utf-8'));
if (parsed == null || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new Error('settings.json must contain a JSON object at the root');
}
return parsed as Record<string, unknown>;
},
catch: (error) => error,
})(),
Result.inspectError((error) => {
log(`\n${pc.red('✗')} Malformed settings.json at ${settingsPath}`);
log(` ${error instanceof Error ? error.message : String(error)}`);
log(` Please fix or remove the file and try again.`);
}),
Result.unwrap(null),
)
: {};
Comment thread
coderabbitai[bot] marked this conversation as resolved.

if (existingSettings == null) {
return;
}
Comment on lines +186 to +209
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Fail the command when settings.json is malformed.

This path logs the parse error and then returns normally, so setup-statusline still exits successfully even though nothing was configured. Please surface a non-zero failure here instead of a bare return.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/ccusage/src/commands/setup-statusline.ts` around lines 186 - 209, The
code currently swallows a malformed settings.json by logging the error and
letting existingSettings be null, then doing a bare return; change this so the
command fails non‑zero: after the Result.pipe(...) that produces
existingSettings, detect the null result and call process.exit(1) (or throw a
descriptive Error) instead of returning. Update the branch that checks
existingSettings == null (and/or the Result.unwrap(null) fallback) so that when
JSON parsing failed (Result.try / Result.inspectError path) the process
terminates with a non‑zero exit code (referencing existingSettings,
settingsPath, Result.pipe, Result.try, Result.inspectError, Result.unwrap).


// Check if statusLine already exists
if ('statusLine' in existingSettings && !ctx.values.force) {
log(`\n${pc.yellow('⚠')} statusLine is already configured in ${settingsPath}`);
log(` Use ${pc.bold('--force')} to overwrite the existing configuration.`);
return;
}

// Build command
const command = buildCommand(resolvedRunner, {
visualBurnRate: ctx.values.visualBurnRate,
showPromotions: ctx.values.showPromotions,
promotionDisplay: ctx.values.promotionDisplay,
costSource: ctx.values.costSource,
showSessionDuration: ctx.values.showSessionDuration,
showLinesChanged: ctx.values.showLinesChanged,
});

// Build new settings
const newSettings = {
...existingSettings,
statusLine: {
type: 'command',
command,
padding: 0,
},
};

const settingsJson = JSON.stringify(newSettings, null, '\t');

// Dry run mode
if (ctx.values.dryRun) {
log(`\n${pc.dim('--- dry run ---')}`);
log(`${pc.dim('Would write to:')} ${settingsPath}`);
log(settingsJson);
log(pc.dim('--- end dry run ---'));
return;
}

// Write settings
mkdirSync(path.dirname(settingsPath), { recursive: true });
writeFileSync(settingsPath, `${settingsJson}\n`, 'utf-8');

log(`\n${pc.green('✓')} Statusline configured successfully!`);
log(` ${pc.dim('Command:')} ${command}`);
log(`\n ${pc.dim('Restart Claude Code to activate the statusline.')}`);
},
});
Loading