-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatusline.js
More file actions
419 lines (368 loc) · 17 KB
/
Copy pathstatusline.js
File metadata and controls
419 lines (368 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env node
// Claude Code status line: repo | branch | context bar | usage | cost | velocity | model
// All colors are 24-bit truecolor ANSI escapes.
const { execSync } = require('child_process');
const path = require('path');
// Bump on every non-temporary change (semver: major.minor.patch). Since the
// live copy at ~/.claude/statusline.js and this repo copy must always be
// kept in sync, `node statusline.js --version` lets you confirm which
// version a given copy is running without reading its source.
const SCRIPT_VERSION = '1.2.0';
if (process.argv.includes('--version')) {
process.stdout.write(`${SCRIPT_VERSION}\n`);
process.exit(0);
}
let raw = '';
process.stdin.on('data', (chunk) => (raw += chunk));
process.stdin.on('end', () => {
let input = {};
try {
input = JSON.parse(raw);
} catch {
input = {};
}
const model = input?.model?.display_name || 'unknown';
const cwd = input?.workspace?.current_dir || input?.cwd || '.';
const repoNameFromInput = input?.workspace?.repo?.name;
const usedPct = Number(input?.context_window?.used_percentage ?? 0) || 0;
const totalInputTokens = Number(input?.context_window?.total_input_tokens ?? 0) || 0;
const maxContextTokens =
Number(input?.context_window?.max_tokens ?? input?.context_window?.context_window_size ?? 0) ||
(usedPct > 0 ? Math.round(totalInputTokens / (usedPct / 100)) : 0);
const linesAdded = Number(input?.cost?.total_lines_added ?? 0) || 0;
const linesRemoved = Number(input?.cost?.total_lines_removed ?? 0) || 0;
const rateLimitPct = input?.rate_limits?.five_hour?.used_percentage;
const weekLimitPct = input?.rate_limits?.seven_day?.used_percentage;
const rateLimitResetsAt = input?.rate_limits?.five_hour?.resets_at;
const weekLimitResetsAt = input?.rate_limits?.seven_day?.resets_at;
const effortLevel = input?.effort?.level || '';
const durationMs = Number(input?.cost?.total_duration_ms ?? 0) || 0;
const totalCostUsd = Number(input?.cost?.total_cost_usd ?? 0) || 0;
const repoName = repoNameFromInput || path.basename(cwd);
let branch = '';
let modified = 0;
let untracked = 0;
let added = 0;
let deleted = 0;
try {
execSync('git rev-parse --is-inside-work-tree', { cwd, stdio: 'ignore' });
branch = execSync('git rev-parse --abbrev-ref HEAD', { cwd, stdio: ['ignore', 'pipe', 'ignore'] })
.toString()
.trim();
const statusLines = execSync('git status --porcelain --untracked-files=all', {
cwd,
stdio: ['ignore', 'pipe', 'ignore'],
})
.toString()
.split('\n')
.filter(Boolean);
for (const line of statusLines) {
const x = line[0];
const y = line[1];
if (x === '?' && y === '?') untracked++;
else if (x === 'D' || y === 'D') deleted++;
else if (x === 'A' || y === 'A') added++;
else if ('MRC'.includes(x) || 'MRC'.includes(y)) modified++;
}
} catch {
branch = '';
}
const RESET = '\x1b[0m';
const fg24 = (r, g, b) => `\x1b[38;2;${r};${g};${b}m`;
const SEP = `${fg24(100, 100, 100)} | ${RESET}`;
function lerp(a, b, t) {
return Math.round(a + (b - a) * t);
}
// Color for the block at position `idx` out of `total`, along a fixed
// green -> yellow -> red gradient spanning the whole bar (position-based,
// not value-based), same idea as the AKCodez gradient progress bar gist.
function gradientBlockColor(idx, total) {
const t = total > 1 ? idx / (total - 1) : 0;
let r, g, b;
if (t <= 0.5) {
const tt = t / 0.5;
r = lerp(0, 220, tt);
g = lerp(200, 200, tt);
b = lerp(80, 0, tt);
} else {
const tt = (t - 0.5) / 0.5;
r = lerp(220, 220, tt);
g = lerp(200, 40, tt);
b = lerp(0, 20, tt);
}
return [r, g, b];
}
// `totalBlocks` controls how many of the (up to 20) gradient blocks are
// drawn — the bar shrinks by lowering this, while filled/empty and the
// gradient colors are recomputed from scratch so the redraw always looks
// right at any size, down to 0 (no bar, just the label/emoji/percentage).
function usageTrio(rawPct, label, totalBlocks = 20) {
const pct = Math.max(0, Math.min(100, rawPct));
const pctInt = Math.round(pct);
const filled = totalBlocks > 0 ? Math.max(0, Math.min(totalBlocks, Math.round((pct / 100) * totalBlocks))) : 0;
const empty = totalBlocks - filled;
let emoji, lr, lg, lb;
if (pctInt < 20) {
emoji = '🟢';
[lr, lg, lb] = [0, 200, 80];
} else if (pctInt < 70) {
emoji = '⚡️';
[lr, lg, lb] = [230, 180, 20];
} else if (pctInt < 90) {
emoji = '🔥';
[lr, lg, lb] = [230, 100, 20];
} else {
emoji = '🚨';
[lr, lg, lb] = [220, 40, 20];
}
let filledBar = '';
for (let i = 0; i < filled; i++) {
const [r, g, b] = gradientBlockColor(i, totalBlocks);
filledBar += `${fg24(r, g, b)}█`;
}
const emptyBar = empty > 0 ? `${fg24(60, 60, 60)}${'█'.repeat(empty)}` : '';
const bar = `${filledBar}${emptyBar}${RESET}`;
const labelPart = label ? `${fg24(150, 150, 150)}${label} ${RESET}` : '';
return totalBlocks > 0
? `${labelPart}${emoji} ${bar} ${fg24(lr, lg, lb)}${pctInt}%${RESET}`
: `${labelPart}${emoji} ${fg24(lr, lg, lb)}${pctInt}%${RESET}`;
}
function formatResetIn(resetsAtSec) {
if (resetsAtSec == null) return '';
const diffMs = resetsAtSec * 1000 - Date.now();
if (diffMs <= 0) return '0m';
const totalMin = Math.round(diffMs / 60000);
const days = Math.floor(totalMin / (60 * 24));
const hours = Math.floor((totalMin % (60 * 24)) / 60);
const mins = totalMin % 60;
if (days > 0) return `${days}d ${hours}h`;
if (hours > 0) return `${hours}h ${mins}m`;
return `${mins}m`;
}
function usagePct(rawPct, label) {
const pct = Math.max(0, Math.min(100, rawPct));
const pctInt = Math.round(pct);
let emoji, lr, lg, lb;
if (pctInt < 20) {
emoji = '🟢';
[lr, lg, lb] = [0, 200, 80];
} else if (pctInt < 70) {
emoji = '⚡️';
[lr, lg, lb] = [230, 180, 20];
} else if (pctInt < 90) {
emoji = '🔥';
[lr, lg, lb] = [230, 100, 20];
} else {
emoji = '🚨';
[lr, lg, lb] = [220, 40, 20];
}
const labelPart = label ? `${fg24(150, 150, 150)}${label} ${RESET}` : '';
return `${labelPart}${emoji} ${fg24(lr, lg, lb)}${pctInt}%${RESET}`;
}
function formatTokenCount(n) {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
return String(n);
}
function stripAnsi(str) {
return str.replace(/\x1b\[[0-9;]*m/g, '');
}
// Truncates an ANSI-colored string to at most `maxWidth` visible columns,
// preserving escape sequences (which contribute 0 width) intact. Used as a
// last-resort safety net so the whole line can never overflow the terminal,
// regardless of which segment's width estimate was wrong.
function truncateAnsiToWidth(str, maxWidth) {
if (maxWidth <= 0) return '';
let visible = 0;
let out = '';
let i = 0;
const ansiRe = /\x1b\[[0-9;]*m/y;
while (i < str.length) {
ansiRe.lastIndex = i;
const m = ansiRe.exec(str);
if (m) {
out += m[0];
i += m[0].length;
continue;
}
const code = str.charCodeAt(i);
const isHighSurrogate = code >= 0xd800 && code <= 0xdbff && i + 1 < str.length;
const chunk = isHighSurrogate ? str.slice(i, i + 2) : str[i];
if (visible + chunk.length > maxWidth) break;
out += chunk;
visible += chunk.length;
i += chunk.length;
}
return out;
}
function styledRepoName(name) {
return `\x1b[1m${fg24(230, 200, 50)}${name}${RESET}`;
}
const leafName = path.basename(cwd);
const leafPart = leafName && leafName !== repoName ? `${fg24(150, 150, 150)}/${leafName}${RESET}` : '';
const dirtyPart = branch
? [
modified > 0 ? `${fg24(230, 180, 20)}~${modified}${RESET}` : '',
untracked > 0 ? `${fg24(150, 150, 150)}?${untracked}${RESET}` : '',
added > 0 ? `${fg24(0, 200, 80)}+${added}${RESET}` : '',
deleted > 0 ? `${fg24(220, 40, 20)}-${deleted}${RESET}` : '',
]
.filter(Boolean)
.join(' ')
: '';
const branchPrefix = branch ? `\x1b[1m${fg24(0, 215, 215)}🌿 (` : '';
const branchSuffix = branch ? `)${RESET}` : '';
const worktreeName = input?.worktree?.name || input?.workspace?.git_worktree || '';
const hasWorktree = Boolean(worktreeName);
const worktreePart = hasWorktree ? `${fg24(80, 220, 120)}🌳 ${worktreeName}${RESET}` : '';
const contextLabel = maxContextTokens
? `🪟 ${formatTokenCount(totalInputTokens)}/${formatTokenCount(maxContextTokens)}`
: `🪟 ${formatTokenCount(totalInputTokens)}`;
const velocityPart = `${fg24(150, 150, 150)}lines ${RESET}${fg24(0, 200, 80)}+${linesAdded}${RESET} ${fg24(220, 40, 20)}-${linesRemoved}${RESET}`;
const DOT = ` ${fg24(100, 100, 100)}·${RESET} `;
function resetBracket(resetsAtSec) {
const resetIn = formatResetIn(resetsAtSec);
return resetIn ? ` ${fg24(150, 150, 150)}(reset ${resetIn})${RESET}` : '';
}
const rateLimitSegments = [];
if (rateLimitPct != null) {
rateLimitSegments.push(usagePct(Number(rateLimitPct), '5h') + resetBracket(rateLimitResetsAt));
}
if (weekLimitPct != null) {
rateLimitSegments.push(usagePct(Number(weekLimitPct), '7d') + resetBracket(weekLimitResetsAt));
}
const rateLimitsPart = rateLimitSegments.join(DOT);
const effortPart = effortLevel ? ` (${effortLevel})` : '';
const modelPart = `${fg24(200, 80, 220)}🤖 ${model}${effortPart}${RESET}`;
const durationSec = Math.floor(durationMs / 1000);
const durationMin = Math.floor(durationSec / 60);
const durationSecRem = durationSec % 60;
const durationHours = Math.floor(durationMin / 60);
const durationMinRem = durationMin % 60;
const durationStr =
durationHours > 0 ? `${durationHours}h ${durationMinRem}m` : `${durationMin}m ${durationSecRem}s`;
const clockPart = `${fg24(150, 150, 150)}⏱️ ${durationStr}${RESET}`;
const costPart = totalCostUsd > 0 ? `${fg24(150, 150, 150)}💵 $${totalCostUsd.toFixed(2)}${RESET}` : '';
// Determine how much room is left for the branch name so the WHOLE status
// line fits within the terminal width, instead of a fixed character cap.
// Claude Code captures this script's stdout rather than connecting it to the
// terminal, so process.stdout.columns is always undefined here — the real
// width normally comes via the COLUMNS env var Claude Code sets before
// invoking us, with a Windows console fallback below when that's missing.
function detectTerminalWidth() {
const envColumns = parseInt(process.env.COLUMNS, 10);
if (envColumns > 0) return envColumns;
// COLUMNS is a shell-exported variable (bash/zsh convention). It's not a
// standard Windows environment variable, so when this script is invoked
// through cmd.exe/PowerShell rather than a bash-like shell it may simply
// be absent. Ask the console directly in that case before giving up.
if (process.platform === 'win32') {
try {
const out = execSync('mode con', { stdio: ['ignore', 'pipe', 'ignore'] }).toString();
const match = out.match(/Columns:\s*(\d+)/i);
const cols = match ? parseInt(match[1], 10) : NaN;
if (cols > 0) return cols;
} catch {
// no attached console (e.g. output is fully redirected) — fall through
}
}
return 120;
}
const terminalWidth = detectTerminalWidth();
// Claude Code reserves its own chrome around the rendered statusline row
// (measured empirically: a 209-column terminal only rendered 205 columns
// of content before Claude Code applied its own cutoff) — this isn't a
// fudge factor for our own width-estimation error, it's content Claude
// Code claims for itself regardless of what we report as the line length.
const safetyMargin = 4;
// Below this width (e.g. a split/narrow pane), spread the line across two
// rows instead of shrinking everything to fit one — model/context/repo on
// row 1, velocity/rate-limits/clock/cost on row 2. Each row then gets the
// FULL terminal width to itself rather than splitting one shared budget.
const TWO_LINE_WIDTH_THRESHOLD = 160;
const twoLine = terminalWidth < TWO_LINE_WIDTH_THRESHOLD;
// Truncates `text` to at most `available` visible characters, appending a
// single-character ellipsis instead of the removed tail. Used for both the
// branch name and the repo name, whichever needs to shrink to make the
// whole line fit the terminal width.
function truncateToFit(text, available) {
if (available <= 0) return '…';
if (text.length <= available) return text;
const keep = Math.max(0, available - 1);
return keep > 0 ? `${text.slice(0, keep)}…` : '…';
}
// Context bar shrinks before anything else, by drawing fewer of its (up to
// 20) blocks — sized against the full label/percentage plus the full,
// untruncated branch and repo name, since those only shrink afterward if
// shrinking the bar down to nothing still isn't enough.
const MAX_BAR_BLOCKS = 20;
// In 2-line mode, velocity/rate-limits/clock/cost move to row 2, so they no
// longer compete with the bar/branch/repo for row 1's width budget.
const trailingLineOneParts = twoLine ? [] : [velocityPart, rateLimitsPart, clockPart, costPart];
const contextPartZeroBar = usageTrio(usedPct, contextLabel, 0);
const modelContextPartZeroBar = [modelPart, contextPartZeroBar].filter(Boolean).join(' ');
const branchPlaceholderForBar = branch ? `${branchPrefix}${branch}${branchSuffix}` : '';
const repoPartForBar = [styledRepoName(repoName), leafPart, branchPlaceholderForBar, worktreePart, dirtyPart]
.filter(Boolean)
.join(' ');
const otherPartsForBar = [modelContextPartZeroBar, repoPartForBar, ...trailingLineOneParts].filter(Boolean);
const lengthWithoutBar = stripAnsi(otherPartsForBar.join(SEP)).length;
// Drawing 1+ blocks (vs. none) reintroduces an extra separating space
// between the bar and the percentage, so N blocks cost N+1 chars relative
// to the zero-bar baseline, not N.
const barBudget = terminalWidth - safetyMargin - lengthWithoutBar - 1;
// A worktree indicator takes priority over the context bar's blocks: when
// shown, the bar collapses to just its percentage so the line has room.
const barBlocks = hasWorktree ? 0 : Math.max(0, Math.min(MAX_BAR_BLOCKS, barBudget));
const contextPart = usageTrio(usedPct, contextLabel, barBlocks);
const modelContextPart = [modelPart, contextPart].filter(Boolean).join(' ');
// Branch shrinks first, sized against the full (untruncated) repo name.
const branchPlaceholder = branch ? `${branchPrefix}${branchSuffix}` : '';
const repoPartWithBranchPlaceholder = [
styledRepoName(repoName),
leafPart,
branchPlaceholder,
worktreePart,
dirtyPart,
]
.filter(Boolean)
.join(' ');
const otherParts = [modelContextPart, repoPartWithBranchPlaceholder, ...trailingLineOneParts].filter(Boolean);
const baseLineVisibleLength = stripAnsi(otherParts.join(SEP)).length;
const truncatedBranch = branch
? truncateToFit(branch, terminalWidth - baseLineVisibleLength - safetyMargin)
: branch;
const branchPart = branch ? `${branchPrefix}${truncatedBranch}${branchSuffix}` : '';
// Repo name shrinks second, against the actual remaining budget once the
// branch above is already final — so a short repo name only gets
// truncated if shrinking the branch alone still wasn't enough.
const repoPartPlaceholder = [styledRepoName(''), leafPart, branchPart, worktreePart, dirtyPart]
.filter(Boolean)
.join(' ');
const partsWithRepoPlaceholder = [modelContextPart, repoPartPlaceholder, ...trailingLineOneParts].filter(Boolean);
const lengthWithoutRepoName = stripAnsi(partsWithRepoPlaceholder.join(SEP)).length;
const truncatedRepoName = truncateToFit(repoName, terminalWidth - lengthWithoutRepoName - safetyMargin);
const folderPart = styledRepoName(truncatedRepoName);
const repoPart = [folderPart, leafPart, branchPart, worktreePart, dirtyPart].filter(Boolean).join(' ');
// Final safety net: even if the branch/repo-name truncation above
// under-estimated (stale/unavailable terminal width, emoji-width quirks,
// etc.), make sure a rendered row can never overflow the terminal and get
// hard-cut mid-segment by it. Trims from the right as a last resort.
function hardTruncateToTerminal(line) {
const visibleLength = stripAnsi(line).length;
const hardBudget = terminalWidth - safetyMargin;
if (hardBudget > 0 && visibleLength > hardBudget) {
return truncateAnsiToWidth(line, Math.max(0, hardBudget - 1)) + RESET + '…';
}
return line;
}
const lineOneParts = [modelContextPart, repoPart, ...trailingLineOneParts].filter(Boolean);
let output = hardTruncateToTerminal(lineOneParts.join(SEP));
if (twoLine) {
const lineTwoParts = [velocityPart, rateLimitsPart, clockPart, costPart].filter(Boolean);
if (lineTwoParts.length > 0) {
output += '\n' + hardTruncateToTerminal(lineTwoParts.join(SEP));
}
}
process.stdout.write(output + '\n');
});