mirror of
https://github.com/jarrodwatts/claude-hud.git
synced 2026-04-16 14:52:41 +00:00
114 lines
3.9 KiB
JavaScript
114 lines
3.9 KiB
JavaScript
import { readStdin, getUsageFromStdin } from "./stdin.js";
|
|
import { parseTranscript } from "./transcript.js";
|
|
import { render } from "./render/index.js";
|
|
import { countConfigs } from "./config-reader.js";
|
|
import { getGitStatus } from "./git.js";
|
|
import { loadConfig } from "./config.js";
|
|
import { parseExtraCmdArg, runExtraCmd } from "./extra-cmd.js";
|
|
import { getClaudeCodeVersion } from "./version.js";
|
|
import { getMemoryUsage } from "./memory.js";
|
|
import { setLanguage, t } from "./i18n/index.js";
|
|
import { fileURLToPath } from "node:url";
|
|
import { realpathSync } from "node:fs";
|
|
export async function main(overrides = {}) {
|
|
const deps = {
|
|
readStdin,
|
|
getUsageFromStdin,
|
|
parseTranscript,
|
|
countConfigs,
|
|
getGitStatus,
|
|
loadConfig,
|
|
parseExtraCmdArg,
|
|
runExtraCmd,
|
|
getClaudeCodeVersion,
|
|
getMemoryUsage,
|
|
render,
|
|
now: () => Date.now(),
|
|
log: console.log,
|
|
...overrides,
|
|
};
|
|
try {
|
|
const stdin = await deps.readStdin();
|
|
if (!stdin) {
|
|
// Running without stdin - this happens during setup verification
|
|
const config = await deps.loadConfig();
|
|
setLanguage(config.language);
|
|
const isMacOS = process.platform === "darwin";
|
|
deps.log(t("init.initializing"));
|
|
if (isMacOS) {
|
|
deps.log(t("init.macosNote"));
|
|
}
|
|
return;
|
|
}
|
|
const transcriptPath = stdin.transcript_path ?? "";
|
|
const transcript = await deps.parseTranscript(transcriptPath);
|
|
const { claudeMdCount, rulesCount, mcpCount, hooksCount, outputStyle } = await deps.countConfigs(stdin.cwd);
|
|
const config = await deps.loadConfig();
|
|
setLanguage(config.language);
|
|
const gitStatus = config.gitStatus.enabled
|
|
? await deps.getGitStatus(stdin.cwd)
|
|
: null;
|
|
// Usage comes only from Claude Code's official stdin rate_limits fields.
|
|
let usageData = null;
|
|
if (config.display.showUsage !== false) {
|
|
usageData = deps.getUsageFromStdin(stdin);
|
|
}
|
|
const extraCmd = deps.parseExtraCmdArg();
|
|
const extraLabel = extraCmd ? await deps.runExtraCmd(extraCmd) : null;
|
|
const sessionDuration = formatSessionDuration(transcript.sessionStart, deps.now);
|
|
const claudeCodeVersion = config.display.showClaudeCodeVersion
|
|
? await deps.getClaudeCodeVersion()
|
|
: undefined;
|
|
const memoryUsage = config.display.showMemoryUsage && config.lineLayout === "expanded"
|
|
? await deps.getMemoryUsage()
|
|
: null;
|
|
const ctx = {
|
|
stdin,
|
|
transcript,
|
|
claudeMdCount,
|
|
rulesCount,
|
|
mcpCount,
|
|
hooksCount,
|
|
sessionDuration,
|
|
gitStatus,
|
|
usageData,
|
|
memoryUsage,
|
|
config,
|
|
extraLabel,
|
|
outputStyle,
|
|
claudeCodeVersion,
|
|
};
|
|
deps.render(ctx);
|
|
}
|
|
catch (error) {
|
|
deps.log("[claude-hud] Error:", error instanceof Error ? error.message : "Unknown error");
|
|
}
|
|
}
|
|
export function formatSessionDuration(sessionStart, now = () => Date.now()) {
|
|
if (!sessionStart) {
|
|
return "";
|
|
}
|
|
const ms = now() - sessionStart.getTime();
|
|
const mins = Math.floor(ms / 60000);
|
|
if (mins < 1)
|
|
return "<1m";
|
|
if (mins < 60)
|
|
return `${mins}m`;
|
|
const hours = Math.floor(mins / 60);
|
|
const remainingMins = mins % 60;
|
|
return `${hours}h ${remainingMins}m`;
|
|
}
|
|
const scriptPath = fileURLToPath(import.meta.url);
|
|
const argvPath = process.argv[1];
|
|
const isSamePath = (a, b) => {
|
|
try {
|
|
return realpathSync(a) === realpathSync(b);
|
|
}
|
|
catch {
|
|
return a === b;
|
|
}
|
|
};
|
|
if (argvPath && isSamePath(argvPath, scriptPath)) {
|
|
void main();
|
|
}
|
|
//# sourceMappingURL=index.js.map
|