mirror of
https://github.com/tiennm99/ccstatusline.git
synced 2026-10-14 04:15:51 +00:00
REFactor: Simplify getBlockMetrics function and improve conditional checks in renderMultipleLines (#102)
This commit is contained in:
1 parent
929b004c22
commit
8477dc5e6b
2 files changed
+14
-45
No files matched your search
+8
-4
@@ -59,6 +59,7 @@ async function renderMultipleLines(data: StatusJSON) {
|
||||
|
||||
// Set global chalk level based on settings
|
||||
chalk.level = settings.colorLevel;
|
||||
|
||||
// Update color map after setting chalk level
|
||||
updateColorMap();
|
||||
|
||||
@@ -75,16 +76,19 @@ async function renderMultipleLines(data: StatusJSON) {
|
||||
const hasBlockTimer = lines.some(line => line.some(item => item.type === 'block-timer'));
|
||||
|
||||
let tokenMetrics: TokenMetrics | null = null;
|
||||
if (hasTokenItems && data.transcript_path)
|
||||
if (hasTokenItems && data.transcript_path) {
|
||||
tokenMetrics = await getTokenMetrics(data.transcript_path);
|
||||
}
|
||||
|
||||
let sessionDuration: string | null = null;
|
||||
if (hasSessionClock && data.transcript_path)
|
||||
if (hasSessionClock && data.transcript_path) {
|
||||
sessionDuration = await getSessionDuration(data.transcript_path);
|
||||
}
|
||||
|
||||
let blockMetrics: BlockMetrics | null = null;
|
||||
if (hasBlockTimer && data.transcript_path)
|
||||
blockMetrics = getBlockMetrics(data.transcript_path);
|
||||
if (hasBlockTimer) {
|
||||
blockMetrics = getBlockMetrics();
|
||||
}
|
||||
|
||||
// Create render context
|
||||
const context: RenderContext = {
|
||||
|
||||
+6
-41
@@ -9,6 +9,8 @@ import type {
|
||||
TranscriptLine
|
||||
} from '../types';
|
||||
|
||||
import { getClaudeConfigDir } from './claude-settings';
|
||||
|
||||
// Ensure fs.promises compatibility for older Node versions
|
||||
const readFile = promisify(fs.readFile);
|
||||
const readFileSync = fs.readFileSync;
|
||||
@@ -147,51 +149,14 @@ export async function getTokenMetrics(transcriptPath: string): Promise<TokenMetr
|
||||
/**
|
||||
* Gets block metrics for the current 5-hour block from JSONL files
|
||||
*/
|
||||
export function getBlockMetrics(transcriptPath: string | undefined): BlockMetrics | null {
|
||||
if (!transcriptPath || typeof transcriptPath !== 'string') {
|
||||
return null;
|
||||
}
|
||||
export function getBlockMetrics(): BlockMetrics | null {
|
||||
const claudeDir: string | null = getClaudeConfigDir();
|
||||
|
||||
let claudePath: string | null = null;
|
||||
|
||||
// On Windows, always use the user's home directory .claude folder
|
||||
if (process.platform === 'win32') {
|
||||
const homeDir = process.env.USERPROFILE ?? process.env.HOME;
|
||||
if (homeDir) {
|
||||
claudePath = path.join(homeDir, '.claude');
|
||||
// Verify the .claude folder exists
|
||||
if (!fs.existsSync(claudePath)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// On other platforms, walk up the directory tree to find .claude folder
|
||||
let currentPath = path.dirname(transcriptPath);
|
||||
const visitedPaths = new Set<string>();
|
||||
|
||||
while (currentPath && !visitedPaths.has(currentPath)) {
|
||||
visitedPaths.add(currentPath);
|
||||
|
||||
const baseName = path.basename(currentPath);
|
||||
if (baseName === '.claude') {
|
||||
claudePath = currentPath;
|
||||
break;
|
||||
}
|
||||
|
||||
const parentPath = path.dirname(currentPath);
|
||||
// Stop if we've reached the root (parent is same as current)
|
||||
if (parentPath === currentPath) {
|
||||
break;
|
||||
}
|
||||
currentPath = parentPath;
|
||||
}
|
||||
}
|
||||
|
||||
if (!claudePath)
|
||||
if (!claudeDir)
|
||||
return null;
|
||||
|
||||
try {
|
||||
return findMostRecentBlockStartTime(claudePath);
|
||||
return findMostRecentBlockStartTime(claudeDir);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user