Skip to content

Instantly share code, notes, and snippets.

@SomtoUgeh
Created August 11, 2025 09:39
Show Gist options
  • Select an option

  • Save SomtoUgeh/4e74f7ad8ec7618116635b7f42e54034 to your computer and use it in GitHub Desktop.

Select an option

Save SomtoUgeh/4e74f7ad8ec7618116635b7f42e54034 to your computer and use it in GitHub Desktop.
Extending ccusage to show more information as you use claude code
#!/usr/bin/env bun
// @ts-ignore - Bun types are available at runtime
import { spawn } from "bun";
// Read JSON input from stdin
// @ts-ignore - Bun global is available at runtime
const input = await Bun.stdin.text();
const data = JSON.parse(input);
// Extract values
// @ts-ignore - Bun global is available at runtime
const currentDir = data.workspace?.current_dir || Bun.cwd();
// Get ccusage status - provide default values for missing fields
const enrichedData = {
...data,
session_id: data.session_id || "unknown",
transcript_path: data.transcript_path || "/tmp/unknown",
cwd: data.cwd || currentDir,
model: {
...data.model,
id: data.model?.id || "unknown",
display_name: data.model?.display_name || ""
},
workspace: {
...data.workspace,
project_dir: data.workspace?.project_dir || currentDir
}
};
const enrichedInput = JSON.stringify(enrichedData);
// Get ccusage status
const ccusageProcess = spawn(["ccusage", "statusline"], {
stdin: "pipe",
stdout: "pipe",
stderr: "pipe",
});
ccusageProcess.stdin.write(enrichedInput);
ccusageProcess.stdin.end();
const [ccusageOutput, ccusageError] = await Promise.all([
new Response(ccusageProcess.stdout).text(),
new Response(ccusageProcess.stderr).text()
]);
// Wait for ccusage process to finish
await ccusageProcess.exited;
// Use stdout if available, otherwise try to extract something meaningful from stderr
let ccusage = ccusageOutput.trim();
if (!ccusage && ccusageError) {
// If ccusage failed, we could show a default message or try to parse the error
ccusage = "ccusage: error";
}
// Get Claude Code version
let claudeVersion = "";
try {
const versionProcess = spawn(["claude", "--version"], {
stdout: "pipe",
stderr: "pipe",
});
const versionText = await new Response(versionProcess.stdout).text();
await versionProcess.exited;
// Extract version number (e.g., "1.0.72" from "1.0.72 (Claude Code)")
const versionMatch = versionText.trim().match(/^([\d.]+)/);
if (versionMatch) {
claudeVersion = versionMatch[1];
}
} catch {
// Claude command not available, ignore
}
// Get git branch if in a git repo
let gitBranch = "";
try {
// Check if we're in a git repo
const gitCheckProcess = spawn(["git", "rev-parse", "--git-dir"], {
stdout: "pipe",
stderr: "pipe",
});
await new Response(gitCheckProcess.stdout).text();
const gitCheckExitCode = await gitCheckProcess.exited;
if (gitCheckExitCode === 0) {
// Get current branch
const branchProcess = spawn(["git", "branch", "--show-current"], {
stdout: "pipe",
stderr: "pipe",
});
const branchOutput = await new Response(branchProcess.stdout).text();
await branchProcess.exited;
gitBranch = branchOutput.trim();
}
} catch {
// Not in a git repo, ignore
}
// Get directory name (basename)
const dirName = currentDir.split("/").pop() || currentDir;
// Output the statusline
// Build the output parts
let output = `${ccusage} | 📁 ${dirName}`;
if (gitBranch) {
output += ` | 🌿 ${gitBranch}`;
}
if (claudeVersion) {
output += ` | ⚡ v${claudeVersion}`;
}
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment