|
| 1 | +/** |
| 2 | + * Compact one-line status formatter for Claude Code sessions |
| 3 | + * Hides empty values, shows only relevant metrics |
| 4 | + */ |
| 5 | + |
| 6 | +interface SessionMetrics { |
| 7 | + context?: { |
| 8 | + used: number; |
| 9 | + total: number; |
| 10 | + percentage: number; |
| 11 | + }; |
| 12 | + model?: string; |
| 13 | + tools?: Record<string, number>; |
| 14 | + agents?: Record<string, { elapsed: number; description: string }>; |
| 15 | + tasks?: { |
| 16 | + pending: number; |
| 17 | + inProgress: number; |
| 18 | + completed: number; |
| 19 | + total: number; |
| 20 | + }; |
| 21 | + duration?: string; |
| 22 | + systemPrompts?: string[]; |
| 23 | +} |
| 24 | + |
| 25 | +interface FormattedStatus { |
| 26 | + line: string; |
| 27 | + parts: string[]; |
| 28 | +} |
| 29 | + |
| 30 | +function formatContext(metrics: SessionMetrics): string | null { |
| 31 | + if (!metrics.context) return null; |
| 32 | + const { percentage, used, total } = metrics.context; |
| 33 | + |
| 34 | + let color = "🟢"; |
| 35 | + if (percentage > 85) color = "🔴"; |
| 36 | + else if (percentage > 60) color = "🟡"; |
| 37 | + |
| 38 | + return `${color} ${percentage}%`; |
| 39 | +} |
| 40 | + |
| 41 | +function formatTools(metrics: SessionMetrics): string | null { |
| 42 | + if (!metrics.tools || Object.keys(metrics.tools).length === 0) return null; |
| 43 | + |
| 44 | + const toolsStr = Object.entries(metrics.tools) |
| 45 | + .map(([name, count]) => `${name}×${count}`) |
| 46 | + .join(" "); |
| 47 | + |
| 48 | + return `Tools: ${toolsStr}`; |
| 49 | +} |
| 50 | + |
| 51 | +function formatAgents(metrics: SessionMetrics): string | null { |
| 52 | + if (!metrics.agents || Object.keys(metrics.agents).length === 0) return null; |
| 53 | + |
| 54 | + const agentsStr = Object.entries(metrics.agents) |
| 55 | + .map(([name, { elapsed }]) => `${name}(${elapsed}s)`) |
| 56 | + .join(" "); |
| 57 | + |
| 58 | + return `Agents: ${agentsStr}`; |
| 59 | +} |
| 60 | + |
| 61 | +function formatTasks(metrics: SessionMetrics): string | null { |
| 62 | + if (!metrics.tasks || metrics.tasks.total === 0) return null; |
| 63 | + |
| 64 | + const { pending, inProgress, completed, total } = metrics.tasks; |
| 65 | + const parts = []; |
| 66 | + |
| 67 | + if (inProgress > 0) parts.push(`🔄 ${inProgress}`); |
| 68 | + if (pending > 0) parts.push(`⏳ ${pending}`); |
| 69 | + if (completed > 0) parts.push(`✓ ${completed}`); |
| 70 | + |
| 71 | + return parts.length > 0 ? `Tasks: ${parts.join(" ")}` : null; |
| 72 | +} |
| 73 | + |
| 74 | +function formatContextDetails(systemPrompts?: string[]): string | null { |
| 75 | + if (!systemPrompts || systemPrompts.length === 0) return null; |
| 76 | + |
| 77 | + // Show count of included system prompts |
| 78 | + return `Context: ${systemPrompts.length} prompts`; |
| 79 | +} |
| 80 | + |
| 81 | +function formatModel(model?: string): string | null { |
| 82 | + if (!model) return null; |
| 83 | + // Don't show Claude Code version, just the model |
| 84 | + return `Model: ${model}`; |
| 85 | +} |
| 86 | + |
| 87 | +function formatDuration(duration?: string): string | null { |
| 88 | + if (!duration) return null; |
| 89 | + return `${duration}`; |
| 90 | +} |
| 91 | + |
| 92 | +export function formatStatus(metrics: SessionMetrics): FormattedStatus { |
| 93 | + const parts: string[] = []; |
| 94 | + |
| 95 | + // Add context health first (most important) |
| 96 | + const contextStr = formatContext(metrics); |
| 97 | + if (contextStr) parts.push(contextStr); |
| 98 | + |
| 99 | + // Add model (optional) |
| 100 | + const modelStr = formatModel(metrics.model); |
| 101 | + if (modelStr) parts.push(modelStr); |
| 102 | + |
| 103 | + // Add duration |
| 104 | + const durationStr = formatDuration(metrics.duration); |
| 105 | + if (durationStr) parts.push(durationStr); |
| 106 | + |
| 107 | + // Add tools |
| 108 | + const toolsStr = formatTools(metrics); |
| 109 | + if (toolsStr) parts.push(toolsStr); |
| 110 | + |
| 111 | + // Add agents |
| 112 | + const agentsStr = formatAgents(metrics); |
| 113 | + if (agentsStr) parts.push(agentsStr); |
| 114 | + |
| 115 | + // Add tasks |
| 116 | + const tasksStr = formatTasks(metrics); |
| 117 | + if (tasksStr) parts.push(tasksStr); |
| 118 | + |
| 119 | + // Add context details (system prompts + tools) |
| 120 | + const contextDetailsStr = formatContextDetails(metrics.systemPrompts); |
| 121 | + if (contextDetailsStr) parts.push(contextDetailsStr); |
| 122 | + |
| 123 | + const line = `📊 ${parts.join(" | ")}`; |
| 124 | + |
| 125 | + return { line, parts }; |
| 126 | +} |
| 127 | + |
| 128 | +export function isShouldDisplay(metrics: SessionMetrics): boolean { |
| 129 | + // Only display if we have meaningful metrics |
| 130 | + return !!( |
| 131 | + metrics.context || |
| 132 | + (metrics.tools && Object.keys(metrics.tools).length > 0) || |
| 133 | + (metrics.agents && Object.keys(metrics.agents).length > 0) || |
| 134 | + (metrics.tasks && metrics.tasks.total > 0) |
| 135 | + ); |
| 136 | +} |
0 commit comments