Shows context window fill and plan usage limits in a band above the prompt.

Context window and plan limits, always in view, in a band above the Claude Code prompt.
<sub>Unofficial. This project is not affiliated with or endorsed by Anthropic. Claude is a trademark of Anthropic.</sub>
<img src="docs/band.png" alt="The usage band above the prompt in the desktop Code tab: the Context, Session and Weekly meters drawn as rings, the branch, and the cache countdown">
<sub>Desktop Code tab</sub>
<img src="docs/cli-band.webp" alt="The usage band above the prompt in the terminal">
<sub>Terminal</sub>
<img src="docs/vscode-band.png" alt="The usage band above the prompt in the VS Code extension: the meters drawn as rings, the shortened branch, and the cache countdown">
<sub>VS Code extension</sub>
In a Claude Code session, run:
/plugin install usage-band --marketplace benpaternostro/claude-usage-band
Confirm the marketplace source, choose an installation scope, and start a new session.
| Meter | Shows |
|---|---|
| Context | How full the context window is. The drawer breaks it down by category, coloured as /context shows it |
| Session | The 5-hour plan limit and the time until it resets |
| Weekly | The 7-day plan limit and the time until it resets |
| Credits | The spend limit, when your plan has one |
| Branch | The session's git branch (a short commit hash when detached), shown from its last /: feature/NEXT-1777 shows as NEXT-1777. Hover it for the full name. The desktop shows a branch icon (a fork icon in a linked worktree); the terminal shows git main, and · worktree in a linked worktree |
| Cache | Estimated prompt-cache time remaining, after a clock icon on the desktop (Cache in the terminal), to the left of the + toggle |
A meter's fill turns amber at 80% of its limit and red at 95%. For the context, the limit is the auto-compact point, or the full window when auto-compact is off.
The cache countdown starts after a main-conversation response uses or writes the cache. It counts whole minutes (59m), then seconds in the last minute. The countdown uses your cache TTL settings. Without an explicit setting, it estimates the TTL from the plan limits. — means no cache time is available. A model switch or compaction clears the estimate. See Claude Code cache lifetimes.
When the band is too narrow, the bars shrink first. On the desktop, bars that would be too short to read turn into small rings. Then the branch is shortened with …, and then it is dropped. If the band is still too narrow, meters drop whole from the right, so items never overlap or wrap. The branch refreshes when the session starts and after each turn.
Click a meter or the + toggle to open the drawer. It shows the full context breakdown, the space left before auto-compact, the session cost, and a Compact session button.
The context breakdown is a local estimate. It sends no API request.



A status line is one line of text below the prompt. The band sits above the prompt and is built from plugin UI, so it can do more:
statusLine setting, so a status line you already have keeps working.| Surface | Status |
|---|---|
| Terminal, including the VS Code integrated terminal | ✅ Supported. The bars are drawn as text. Click a meter's label or figures, or +, to open the drawer. |
| Desktop app, Code tab | ✅ Supported |
| VS Code extension | ✅ Supported. The band draws as on the desktop, with bars, rings and icons. VS Code draws every plugin button with a frame, so the meters are plain text there: click + to open the drawer. Hover a shortened branch for its full name. The drawer lays the context breakdown out in as many columns as fit. VS Code captions the band "From the usage-band plugin"; the mod cannot hide it. |
[!WARNING] The function-hook plugin API is in early access. It can change between Claude Code releases without notice.
From your shell, the install is two commands:
claude plugin marketplace add benpaternostro/claude-usage-band
claude plugin install usage-band@claude-usage-band
To get a new version, run:
claude plugin update usage-band@claude-usage-band
Clone the repository, then start Claude Code with the folder:
git clone https://github.com/benpaternostro/claude-usage-band.git
claude --plugin-dir ./claude-usage-band
Use this method when you change the mod yourself, or in the desktop app, where you cannot give a command-line flag. Add the absolute path of the cloned folder to the env block of ~/.claude/settings.json:
{
"env": {
"CLAUDE_CODE_PLUGIN_DIRS": "/Users/you/code/claude-usage-band"
}
}
To load more than one folder, separate the paths with : on macOS and Linux, or with ; on Windows.
[!IMPORTANT] Do not load the mod from a folder and also install it from the marketplace. Use one method only.
Claude Code writes the API type declarations into .claude-plugin/types/ when it loads the mod from a folder. These files are not in the repository.
claude plugin validate .
claude plugin test .
When you load the mod from a folder, it reloads in a running terminal session after you save hooks/register.tsx. In the desktop app, set CLAUDE_CODE_PLUGIN_DIR_WATCH=1 in the same env block to get this behaviour, or start a new session.
The CI workflow runs the unit tests, claude plugin validate . and claude plugin test . on every pull request and on main.
Merging to main releases the mod when the merge changes its files: hooks/, types/ or .claude-plugin/. The Release workflow runs the tests, bumps the version in .claude-plugin/plugin.json on main, tags usage-band--v<version>, and publishes the GitHub release with notes listing the merged PRs.
Label the PR to set the bump:
| Label | Release |
|---|---|
| none | Patch: 0.3.0 → 0.3.1 |
minor | Minor: 0.3.0 → 0.4.0 |
major | Major: 0.3.0 → 1.0.0 |
no-release | Nothing |
A PR that sets the version in plugin.json itself is released at that version. Merges that only change docs, tests or workflows release nothing.
| Path | Contents |
|---|---|
hooks/register.tsx | The hooks: usage refresh, band and drawer rendering |
hooks/cache.ts | Cache TTL estimate and countdown text |
hooks/git.ts | Branch and worktree detection |
hooks/fit.ts | Band width budget: bar widths, rings and branch shortening |
.claude-plugin/marketplace.json | The marketplace entry for /plugin install |
types/index.d.ts | Snapshot types and plugin state declarations |
tests/band.test.ts | Helper, git and render tests |
tests/cache.test.mjs | Cache countdown tests (node --test) |
tests/fit.test.mjs | Width budget tests (node --test) |
.github/workflows/ | CI, and the release on each merge to main |
.github/scripts/release-plan.sh | Decides what a merge releases |
hooks/register.tsx 664 lines1import { atom, read, update } from 'claude-code'
2import type { EngineInterface, Register, SessionContextUsage, SessionCost, SessionRateLimit } from 'claude-code'
3
4import type { GitHead, Limit, Segment, Snapshot } from '../types'
5import { cacheText, cacheTtl } from './cache'
6import { BAR_CELLS, BAR_PX, CACHE_WORD, DESKTOP, GIT_WORD, ICON_PX, OPEN_GLYPH, TERMINAL, VSCODE, WORKTREE_NOTE, fitBand, roomOf } from './fit'
7import { HEAD_ARGV, branchName, isDetached, parseHead } from './git'
8
9const snapshot = atom({ plugin: 'usage-band', key: 'snapshot' } as const, null)
10const isOpen = atom({ plugin: 'usage-band', key: 'isOpen' } as const, false)
11const cacheExpiresAt = atom({ plugin: 'usage-band', key: 'cacheExpiresAt' } as const, null)
12const git = atom({ plugin: 'usage-band', key: 'git' } as const, null)
13
14// Full-width glyphs: the same size as the app's close control, and the same
15// advance as each other, so the toggle never moves.
16export { OPEN_GLYPH }
17export const CLOSE_GLYPH = '-'
18
19const SHORT_LABELS: Record<string, string> = { five_hour: 'Session', seven_day: 'Weekly', spend_limit: 'Credits' }
20const LONG_LABELS: Record<string, string> = {
21 five_hour: 'Session limit',
22 seven_day: 'Weekly · all models',
23 spend_limit: 'Usage credits',
24}
25
26// The app's usage popover: blue fill on a dark track; context categories in theme colours.
27const BLUE = '#4a80e8'
28const AMBER = '#e0a526'
29const RED = '#e5484d'
30const TRACK = '#8888884d'
31const BUFFER = '#8888888c'
32const HOVER_BG = '#8888881f'
33// The whole branch name over the short one, on the desktop: the band's dark
34// grey as a meter under the pointer lights it, opaque so the name hides what
35// it covers.
36const TIP_BG = '#2e2e2e'
37const TIP_TEXT = '#f2f2f2'
38const THEME: Record<string, string> = {
39 permission: BLUE,
40 suggestion: BLUE,
41 claude: '#d97757',
42 success: '#3fae6a',
43 warning: AMBER,
44 error: RED,
45 inactive: '#b4b4b4',
46 promptBorder: '#b4b4b4',
47 remember: '#a48fd8',
48 purple_FOR_SUBAGENTS_ONLY: '#a48fd8',
49 cyan_FOR_SUBAGENTS_ONLY: '#4fb3c4',
50}
51const BY_RANK = [BLUE, '#d97757', '#3fae6a', AMBER, '#b4b4b4', '#a48fd8', '#4fb3c4']
52// Braille blanks: drawn empty, never trimmed as spaces are. About a cell's
53// worth for each cell of bar.
54const hitLabel = (px: number) => '⠀'.repeat(Math.max(1, Math.round((BAR_CELLS * px) / BAR_PX)))
55
56// Desktop icons, 24-unit strokes in a grey that reads on light and dark themes.
57const ICON_GREY = '#8c8c8c'
58const ICONS = {
59 branch: '<circle cx="6" cy="5" r="2"/><circle cx="6" cy="19" r="2"/><circle cx="18" cy="6" r="2"/><path d="M6 7v10M18 8v1a4 4 0 0 1-4 4h-4a4 4 0 0 0-4 4"/>',
60 worktree: '<circle cx="6" cy="5" r="2"/><circle cx="18" cy="5" r="2"/><circle cx="12" cy="19" r="2"/><path d="M6 7v1a4 4 0 0 0 4 4h4a4 4 0 0 0 4-4v-1M12 12v5"/>',
61 clock: '<circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 3"/>',
62}
63const svgIcon = (body: string) =>
64 `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="${ICON_GREY}" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${body}</svg>`
65const SVG_DIVIDER = `<svg xmlns="http://www.w3.org/2000/svg" width="1" height="14"><rect width="1" height="14" fill="${ICON_GREY}" fill-opacity="0.45"/></svg>`
66
67type Usage = { context: SessionContextUsage; rateLimits: SessionRateLimit[]; cost?: SessionCost }
68type Part = { color: string; part: number; title?: string; tokens?: number }
69// What the bar's empty track says under the pointer.
70type Bar = { parts: Part[]; rest?: string; restTokens?: number }
71
72const limitsOf = (u: Usage): Limit[] =>
73 u.rateLimits.map(r => ({ kind: r.kind, percentUsed: r.percentUsed, resetsAt: r.resetsAt }))
74
75// The breakdown is what /context and the popover show; the raw figure is the last
76// response's input, which can be a subagent's, so it is only the fallback.
77export const toSnapshot = (u: Usage, prev: Snapshot | null): Snapshot => {
78 const b = u.context.breakdown
79 if (!b) {
80 return {
81 tokens: u.context.tokens,
82 window: u.context.window,
83 percent: u.context.percent,
84 limits: limitsOf(u),
85 usd: u.cost?.usd,
86 segments: prev?.segments ?? [],
87 buffer: prev?.buffer,
88 autoCompactAt: prev?.autoCompactAt,
89 }
90 }
91 const segments: Segment[] = b.categories
92 .filter(c => c.kind === 'used' && c.tokens > 0)
93 .sort((x, y) => y.tokens - x.tokens)
94 .map((c, i) => ({ name: c.name, tokens: c.tokens, color: THEME[c.color] ?? BY_RANK[i % BY_RANK.length] ?? BLUE }))
95 const buffer = b.categories.filter(c => c.kind === 'buffer').reduce((a, c) => a + c.tokens, 0)
96 return {
97 tokens: b.totalTokens,
98 window: b.rawMaxTokens,
99 percent: b.percentage,
100 limits: limitsOf(u),
101 usd: u.cost?.usd,
102 segments,
103 buffer: buffer > 0 ? buffer : undefined,
104 autoCompactAt: b.isAutoCompactEnabled ? b.autoCompactThreshold : undefined,
105 }
106}
107
108export const kTokens = (n: number) =>
109 n >= 1_000_000 ? `${(n / 1_000_000).toFixed(1)}M` : n >= 1000 ? `${(n / 1000).toFixed(1).replace(/\.0$/, '')}k` : `${n}`
110
111export const relative = (ms: number, isLong = false) => {
112 const mins = Math.max(1, Math.round(ms / 60_000))
113 const d = Math.floor(mins / 1440)
114 const h = Math.floor((mins % 1440) / 60)
115 const m = mins % 60
116 if (isLong) return d > 0 ? `${d} d ${h} hr` : h > 0 ? `${h} hr ${m} min` : `${m} min`
117 return d > 0 ? `${d}d ${h}h` : h > 0 ? `${h}h ${m}m` : `${m}m`
118}
119
120export const resetIn = (l: Limit, now: number) => {
121 const at = l.resetsAt ? Date.parse(l.resetsAt) : NaN
122 return at > now ? relative(at - now) : ''
123}
124
125// Under a day: "Resets in 1 hr 8 min"; longer: "Resets Thu 7:00 AM".
126export const resetText = (l: Limit, now: number) => {
127 const at = l.resetsAt ? Date.parse(l.resetsAt) : NaN
128 if (!(at > now)) return ''
129 if (at - now < 86_400_000) return `Resets in ${relative(at - now, true)}`
130 try {
131 const when = new Date(at).toLocaleString('en-US', { weekday: 'short', hour: 'numeric', minute: '2-digit' })
132 return `Resets ${when.replace(',', '')}`
133 } catch {
134 return `Resets in ${relative(at - now, true)}`
135 }
136}
137
138const share = (tokens: number, window: number) => `${Math.round((tokens / window) * 100)}%`
139
140// Category segments scaled to the window's fill, then the buffer; the rest is track.
141export const contextBar = (s: Snapshot): Bar => {
142 if (s.window <= 0) return { parts: [] }
143 const tokens = s.tokens ?? 0
144 const fill = Math.min(1, tokens / s.window)
145 const total = s.segments.reduce((a, x) => a + x.tokens, 0)
146 const used: Part[] =
147 fill <= 0
148 ? []
149 : total <= 0
150 ? [{ color: BLUE, part: fill, tokens, title: `Used · ${kTokens(tokens)} (${share(tokens, s.window)})` }]
151 : s.segments.map(x => ({
152 color: x.color,
153 part: (x.tokens / total) * fill,
154 tokens: x.tokens,
155 title: `${x.name} · ${kTokens(x.tokens)} (${share(x.tokens, s.window)})`,
156 }))
157 const bufferPart = s.buffer ? Math.min(1 - fill, s.buffer / s.window) : 0
158 const parts =
159 bufferPart > 0
160 ? [...used, { color: BUFFER, part: bufferPart, tokens: s.buffer, title: `Autocompact buffer · ${kTokens(s.buffer ?? 0)} (${share(s.buffer ?? 0, s.window)})` }]
161 : used
162 const free = Math.max(0, s.window - tokens - (bufferPart > 0 ? (s.buffer ?? 0) : 0))
163 return { parts, restTokens: free, rest: `Free space · ${kTokens(free)} (${share(free, s.window)})` }
164}
165
166// A fill turns amber from 80% of the way to its limit, and red from 95%.
167export const fillColor = (used: number, limit = 100) => {
168 const way = limit > 0 ? used / limit : 0
169 return way >= 0.95 ? RED : way >= 0.8 ? AMBER : BLUE
170}
171
172// The context's limit is where it auto-compacts, or the window when it never does.
173export const contextColor = (s: Snapshot) => fillColor(s.tokens ?? 0, s.autoCompactAt ?? s.window)
174
175export const limitBar = (l: Limit, now: number): Bar => {
176 const percent = Math.min(100, Math.max(0, l.percentUsed))
177 const reset = resetText(l, now)
178 const name = LONG_LABELS[l.kind] ?? l.kind
179 return {
180 parts: [{ color: fillColor(percent), part: percent / 100, title: `${name} · ${percent}% used${reset ? ` · ${reset}` : ''}` }],
181 rest: `${100 - percent}% left${reset ? ` · ${reset}` : ''}`,
182 }
183}
184
185const svgBar = (bar: Bar, width: number, height: number, gap: number) => {
186 let x = 0
187 const rects = bar.parts
188 .map(p => {
189 const w = p.part * width
190 const isLast = x + w >= width - 0.5
191 const r = `<rect x="${x.toFixed(2)}" width="${Math.max(0, isLast ? w : w - gap).toFixed(2)}" height="${height}" fill="${p.color}"/>`
192 x += w
193 return r
194 })
195 .join('')
196 return (
197 `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" preserveAspectRatio="none">` +
198 `<clipPath id="c"><rect width="${width}" height="${height}" rx="${height / 2}"/></clipPath>` +
199 `<g clip-path="url(#c)"><rect width="${width}" height="${height}" fill="${TRACK}"/>${rects}</g></svg>`
200 )
201}
202
203// A bar wound into a ring, for a row with no room for bars: the fill as one
204// arc in its colour, clockwise from the top, on the same track.
205const svgRing = (part: number, size: number, color: string) => {
206 const c = size / 2
207 const r = c - 1.25
208 const around = 2 * Math.PI * r
209 const arc = Math.min(1, Math.max(0, part)) * around
210 return (
211 `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}" fill="none" stroke-width="2">` +
212 `<g transform="rotate(-90 ${c} ${c})"><circle cx="${c}" cy="${c}" r="${r}" stroke="${TRACK}"/>` +
213 (arc > 0
214 ? `<circle cx="${c}" cy="${c}" r="${r}" stroke="${color}" stroke-linecap="round" stroke-dasharray="${arc.toFixed(2)} ${around.toFixed(2)}"/>`
215 : '') +
216 `</g></svg>`
217 )
218}
219
220
221export const textCells = (parts: Part[], width: number) => {
222 const cells: { color: string; n: number }[] = []
223 let used = 0
224 for (const p of parts) {
225 const n = Math.min(width - used, Math.round(p.part * width))
226 if (n > 0) {
227 cells.push({ color: p.color, n })
228 used += n
229 }
230 }
231 return { cells, free: Math.max(0, width - used) }
232}
233
234// The summary breakdown is a local estimate: it sends no API request.
235async function refresh($: EngineInterface) {
236 const prev = await read($, snapshot)
237 let usage: Usage
238 try {
239 usage = await $.session.usage({ breakdown: 'summary' })
240 } catch {
241 usage = await $.session.usage()
242 }
243 await update($, snapshot, () => toSnapshot(usage, prev))
244}
245
246// Outside a repository, or with no commit yet, the band shows no branch.
247async function refreshGit($: EngineInterface) {
248 let head: GitHead | null = null
249 try {
250 const ran = await $.process.run(HEAD_ARGV, { timeoutMs: 5000 })
251 if (ran.exitCode === 0) {
252 const sha = isDetached(ran.stdout) ? (await $.process.run(['git', 'rev-parse', '--short', 'HEAD'], { timeoutMs: 5000 })).stdout : ''
253 head = parseHead(ran.stdout, sha)
254 }
255 } catch {
256 // No git on the host: no branch.
257 }
258 await update($, git, () => head)
259}
260
261async function toggle($: EngineInterface) {
262 await update($, isOpen, open => !open)
263}
264
265export const register: Register = on => {
266 let ticker: ReturnType<EngineInterface['clock']['every']> | undefined
267
268 on('session.start', async ($, e, next) => {
269 const result = await next(e)
270 await update($, isOpen, () => false)
271 await update($, cacheExpiresAt, () => null)
272 await Promise.all([refresh($), refreshGit($)])
273 ticker ??= $.clock.every(1000, () => $.ui.invalidate('ui.render'))
274 return result
275 })
276
277 on('session.end', async (_, e, next) => {
278 ticker?.cancel()
279 ticker = undefined
280 return next(e)
281 })
282
283 on('turn.step', async function* ($, e, next) {
284 const result = yield* next(e)
285 if (e.agentId || !result.usage) return result
286 const usage = result.usage
287 if (usage.cache_read_input_tokens + usage.cache_creation_input_tokens === 0) {
288 await update($, cacheExpiresAt, () => null)
289 return result
290 }
291 const now = await $.clock.now()
292 const [force5m, ttl, enable1h, settings, session] = await Promise.all([
293 $.env.get('FORCE_PROMPT_CACHING_5M'),
294 $.env.get('CLAUDE_CODE_PROMPT_CACHE_TTL'),
295 $.env.get('ENABLE_PROMPT_CACHING_1H'),
296 $.settings.read(),
297 $.session.usage(),
298 ])
299 const duration = cacheTtl({ force5m, ttl, enable1h, setting: settings.promptCacheTtl, limits: limitsOf(session) })
300 await update($, cacheExpiresAt, () => now + duration)
301 return result
302 })
303
304 // A turn may switch the branch or move into a worktree; so may the person,
305 // outside the session, between turns.
306 on('turn.complete', async ($, e, next) => {
307 const result = await next(e)
308 if (!e.agentId) await refreshGit($)
309 return result
310 })
311
312 on('classic.PostModelSwitch', async ($, e, next) => {
313 const result = await next(e)
314 await update($, cacheExpiresAt, () => null)
315 return result
316 })
317
318 on('session.compact', async ($, e, next) => {
319 const result = await next(e)
320 if (!e.agentId && e.trigger !== 'precompute' && result.skip === undefined) {
321 await update($, cacheExpiresAt, () => null)
322 }
323 return result
324 })
325
326 on('session.measure', async ($, e, next) => {
327 if (e.changed.includes('context')) {
328 await refresh($)
329 } else {
330 // Limits and cost only: keep the context figures the breakdown gave.
331 const limits = limitsOf(e)
332 await update($, snapshot, prev => (prev ? { ...prev, limits, usd: e.cost?.usd ?? prev.usd } : toSnapshot(e, null)))
333 }
334 return next(e)
335 })
336
337 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
338 const s = await read($, snapshot)
339 if (e.props.hasSurvey || s === null) return next(e)
340
341 const { Box, Button, Text } = $.ui.resolve(e)
342 const now = await $.clock.now()
343 const open = await read($, isOpen)
344 ticker ??= $.clock.every(1000, () => $.ui.invalidate('ui.render'))
345 const cacheLabel = cacheText(await read($, cacheExpiresAt), now)
346 const head = await read($, git)
347
348 // A wide bar spans `width` cells on the terminal: the drawer's inner width
349 // unless it sits in a narrower column. A narrow one is `width` cells, or px
350 // on the desktop.
351 const bar = (id: string, bar: Bar, label: string, isWide: boolean, width = e.props.bodyColumns - 2) => {
352 if (e.surface === 'desktop' || e.surface === 'vscode') {
353 const { Svg } = $.ui.resolve(e)
354 // A wide bar fills a row of its own, centred in it, so the text above
355 // and below sits the same distance away.
356 return isWide ? (
357 <Box key={id} height={1} flexDirection="column" justifyContent="center">
358 <Svg key={`${id}-svg`} source={svgBar(bar, 1000, 4, 4)} alt={label} height={4} />
359 </Box>
360 ) : (
361 <Svg key={id} source={svgBar(bar, width, 4, 1)} alt={label} width={width} height={4} />
362 )
363 }
364 const { cells, free } = textCells(bar.parts, isWide ? Math.max(10, width) : width)
365 return (
366 <Box key={id}>
367 {cells.map((c, i) => (
368 <Text key={`${id}-${i}`} color={c.color}>
369 {'━'.repeat(c.n)}
370 </Text>
371 ))}
372 <Text dimColor>{'━'.repeat(free)}</Text>
373 </Box>
374 )
375 }
376
377 const dot = (id: string, color: string) => (
378 <Text key={id} color={color}>
379 ●
380 </Text>
381 )
382
383 // The legend as a grid, as /context lays it out: each cell a dot and the
384 // name on the left, the size and share aligned on the right.
385 const cols = e.props.bodyColumns >= 128 ? 4 : e.props.bodyColumns >= 90 ? 3 : e.props.bodyColumns >= 60 ? 2 : 1
386 const legend = (id: string, b: Bar, maxRows = Infinity) => {
387 const items = [
388 ...b.parts.map(p => ({ color: p.color, title: p.title ?? '', tokens: p.tokens ?? 0 })),
389 ...(b.rest ? [{ color: TRACK, title: b.rest, tokens: b.restTokens ?? 0 }] : []),
390 ].sort((x, y) => y.tokens - x.tokens)
391 const cell = (key: string, it: (typeof items)[number], width: number) => {
392 const [name, ...rest] = it.title.split(' · ')
393 const value = rest.join(' · ').replace(/ \((\d+%)\)$/, ' $1')
394 return (
395 <Box key={key} width={width} flexShrink={0} flexDirection="row" columnGap={1}>
396 {dot(`${key}-dot`, it.color)}
397 <Box key={`${key}-n`} flexGrow={1} flexShrink={1}>
398 <Text dimColor wrap="truncate-end">
399 {name}
400 </Text>
401 </Box>
402 <Text dimColor>{value}</Text>
403 </Box>
404 )
405 }
406 // VS Code wraps a row too wide for it, and its band holds more cells
407 // than bodyColumns says: there the cells, each as wide as the longest,
408 // wrap as many to a row as fit.
409 if (e.surface === 'vscode') {
410 const width = Math.max(...items.map(it => 7 + it.title.replace(/ \(\d+%\)$/, '').length))
411 return (
412 <Box key={id} flexDirection="row" flexWrap="wrap" columnGap={3}>
413 {items.map((it, i) => cell(`${id}-${i}`, it, width))}
414 </Box>
415 )
416 }
417 const width = Math.floor((e.props.bodyColumns - 2 - 3 * (cols - 1)) / cols)
418 const rows = Array.from({ length: Math.max(1, Math.min(maxRows, Math.ceil(items.length / cols))) }, (_, r) => items.slice(r * cols, r * cols + cols))
419 return (
420 <Box key={id} flexDirection="column">
421 {rows.map((row, r) => (
422 <Box key={`${id}-r${r}`} flexDirection="row" columnGap={3}>
423 {row.map((it, c) => cell(`${id}-${r}-${c}`, it, width))}
424 </Box>
425 ))}
426 </Box>
427 )
428 }
429
430 // Its own keyed Box: the toggle brightens under the pointer, not the whole row.
431 const toggleButton = (
432 <Box key="details-box">
433 <Button
434 key="details"
435 plain
436 dimColor
437 hover={{ dimColor: false }}
438 label={open ? CLOSE_GLYPH : OPEN_GLYPH}
439 onPress={() => toggle($)}
440 />
441 </Box>
442 )
443
444 const ctx = contextBar(s)
445 const ctxRight =
446 s.tokens === undefined ? `— / ${kTokens(s.window)}` : `${kTokens(s.tokens)} / ${kTokens(s.window)} (${s.percent ?? 0}%)`
447 const toCompact =
448 s.autoCompactAt !== undefined && s.tokens !== undefined ? Math.max(0, s.autoCompactAt - s.tokens) : undefined
449 const compactLine =
450 toCompact === undefined
451 ? s.tokens === undefined
452 ? 'No reply yet in this window'
453 : `${kTokens(Math.max(0, s.window - s.tokens))} free`
454 : toCompact < 10_000
455 ? 'Auto-compacts soon'
456 : `${kTokens(toCompact)} until auto-compact`
457
458 if (open) {
459 // The drawer takes the band's place and must fit it whole: past maxRows
460 // the band scrolls. The limits share a row and the cost rides the compact
461 // row, so the breakdown keeps its room; gaps only when all of it fits.
462 const limitCols = e.props.bodyColumns >= 70 ? 2 : 1
463 // The columns share the inner width; the first ones take any odd cells.
464 const limitSpan = e.props.bodyColumns - 2 - 4 * (limitCols - 1)
465 const limitWidth = (c: number) => Math.floor(limitSpan / limitCols) + (c < limitSpan % limitCols ? 1 : 0)
466 const limitRows = Array.from({ length: Math.ceil(s.limits.length / limitCols) }, (_, r) =>
467 s.limits.slice(r * limitCols, r * limitCols + limitCols),
468 )
469 const legendRows = Math.ceil((ctx.parts.length + (ctx.rest ? 1 : 0)) / cols)
470 const base = 3 + 2 * limitRows.length
471 // VS Code's rows run taller than its band allows for: no gaps there.
472 const hasGaps = e.surface !== 'vscode' && base + legendRows + 1 + limitRows.length <= e.props.maxRows
473 return (
474 <Box flexDirection="column" rowGap={hasGaps ? 1 : 0} paddingX={1}>
475 <Box key="d-ctx" flexDirection="column">
476 <Box key="d-ctx-row" flexDirection="row" justifyContent="space-between" alignItems="center" columnGap={2}>
477 <Text dimColor>Context window</Text>
478 <Box key="d-ctx-right" flexDirection="row" alignItems="center" columnGap={2}>
479 <Text dimColor>{ctxRight}</Text>
480 {toggleButton}
481 </Box>
482 </Box>
483 {bar('d-ctx-bar', ctx, `Context ${s.percent ?? 0}%`, true)}
484 {legend('d-ctx-legend', ctx, e.props.maxRows - base)}
485 </Box>
486 <Box key="d-compact" flexDirection="row" justifyContent="space-between" alignItems="center" columnGap={2}>
487 <Box key="d-compact-left" flexDirection="row" alignItems="center" columnGap={1}>
488 <Text dimColor>{compactLine}</Text>
489 {s.usd !== undefined && <Text dimColor>· Session cost</Text>}
490 {s.usd !== undefined && <Text dimColor>{`$${s.usd.toFixed(2)}`}</Text>}
491 </Box>
492 <Button key="compact-now" variant="secondary" label="Compact session" onPress={() => $.session.compact()} />
493 </Box>
494 {limitRows.map((row, r) => (
495 <Box key={`d-lims-${r}`} flexDirection="row" flexWrap="nowrap" columnGap={4}>
496 {row.map((l, c) => {
497 const reset = resetText(l, now)
498 return (
499 <Box key={`d-lim-${l.kind}`} flexDirection="column" flexGrow={1} flexShrink={1} width={`${Math.floor(100 / limitCols)}%`} minWidth={0}>
500 <Box key={`d-lim-row-${l.kind}`} flexDirection="row" justifyContent="space-between" alignItems="center" columnGap={2}>
501 <Text wrap="truncate-end">{LONG_LABELS[l.kind] ?? l.kind}</Text>
502 <Text dimColor wrap="truncate-end">{`${reset}${reset ? ' ' : ''}${l.percentUsed}%`}</Text>
503 </Box>
504 {bar(`d-lim-bar-${l.kind}`, limitBar(l, now), `${l.kind} ${l.percentUsed}%`, true, limitWidth(c))}
505 </Box>
506 )
507 })}
508 </Box>
509 ))}
510 </Box>
511 )
512 }
513
514 // The desktop and VS Code draw in pixels, with icons, SVG bars and rings;
515 // a cell is about 8px wide there, and the band's inner gaps take half of
516 // one. Terminal gaps are whole cells. VS Code draws every Button with its
517 // chrome, so there the words are Text and the toggle alone opens the drawer.
518 const isDesktop = e.surface === 'desktop'
519 const isVscode = e.surface === 'vscode'
520 const isPixel = isDesktop || isVscode
521 const half = isPixel ? 0.5 : 1
522 const percentText = (percent: number | undefined) => (percent === undefined ? '—' : `${percent}%`)
523
524 const meters = [
525 // One fill, as the limits have: at this size the context's grey
526 // categories vanish into the track. The drawer breaks it down.
527 { id: 'ctx', label: 'Context', b: { parts: [{ color: contextColor(s), part: Math.min(1, (s.percent ?? 0) / 100) }] }, percent: s.percent, note: '' },
528 ...s.limits.map(l => ({
529 id: `lim-${l.kind}`,
530 label: SHORT_LABELS[l.kind] ?? l.kind,
531 b: limitBar(l, now),
532 percent: l.percentUsed,
533 note: resetIn(l, now),
534 })),
535 ]
536 // The bars narrow, then turn to rings on the desktop, then the branch
537 // shortens, before anything is dropped.
538 const model = isDesktop ? DESKTOP : isVscode ? VSCODE : TERMINAL
539 const fit = fitBand(
540 {
541 meters: meters.map(m => ({ label: m.label, percent: percentText(m.percent), note: m.note })),
542 branch: head ? branchName(head) : undefined,
543 worktree: head?.worktree !== undefined,
544 cache: cacheLabel,
545 },
546 roomOf(model, e.props.bodyColumns),
547 model,
548 )
549
550 const svg = (id: string, source: string, alt: string, width: number, height: number) => {
551 if (e.surface !== 'desktop' && e.surface !== 'vscode') return null
552 const { Svg } = $.ui.resolve(e)
553 return <Svg key={id} source={source} alt={alt} width={width} height={height} />
554 }
555 const icon = (id: string, body: string, alt: string) => svg(id, svgIcon(body), alt, ICON_PX, ICON_PX)
556
557 // VS Code's Text has no padding, as the desktop's plain Buttons do: whole
558 // cells part its words.
559 const gap = isVscode ? 1 : half
560 // A word of the band that opens the drawer, or in VS Code just the word.
561 const word = (key: string, label: string, dimColor = false) =>
562 isVscode ? (
563 <Text key={key} dimColor={dimColor}>
564 {label}
565 </Text>
566 ) : (
567 <Button key={key} plain dimColor={dimColor} label={label} onPress={() => toggle($)} />
568 )
569
570 const meter = ({ id, label, b, percent, note }: (typeof meters)[number]) => (
571 <Box
572 key={id}
573 flexDirection="row"
574 alignItems="center"
575 columnGap={gap}
576 paddingX={half}
577 flexShrink={0}
578 hover={isVscode ? undefined : { backgroundColor: HOVER_BG }}
579 >
580 {/* Only Buttons take a press, so each word of the meter is one: a click
581 anywhere on its text opens the drawer. */}
582 {word(`${id}-label`, label, true)}
583 {/* A bar or ring takes no press, so on the desktop a blank plain Button
584 lies over it. On a text surface that Button would paint over the bar's
585 cells, so there the label and figures take the press alone. */}
586 {/* A ring sits closer to its figure than to its label, so the two read
587 as one. */}
588 <Box key={`${id}-fill`} flexDirection="row" alignItems="center" columnGap={fit.isRing ? (isVscode ? half : 0) : gap}>
589 <Box flexDirection="row" alignItems="center">
590 {fit.isRing
591 ? svg(`${id}-bar`, svgRing((percent ?? 0) / 100, fit.bar, b.parts[0]?.color ?? BLUE), `${label} ${percent ?? 0}%`, fit.bar, fit.bar)
592 : bar(`${id}-bar`, b, `${label} ${percent ?? 0}%`, false, fit.bar)}
593 {isDesktop && (
594 <Box position="absolute" top={0} left={0}>
595 <Button key={`${id}-hit`} plain label={hitLabel(fit.bar)} hover={{ inverse: false }} onPress={() => toggle($)} />
596 </Box>
597 )}
598 </Box>
599 {word(`${id}-pct`, percentText(percent))}
600 </Box>
601 {note !== '' && word(`${id}-note`, note, true)}
602 </Box>
603 )
604
605 return (
606 <Box flexDirection="row" justifyContent="space-between" alignItems="center" columnGap={2 * half}>
607 {/* One row that wraps onto a hidden second one: whatever the fit
608 misjudges leaves whole, from the right, rather than overlapping or
609 being cut mid-word. The branch sits last and grows to push itself
610 right, so it is the first to go, with the divider that parts it
611 from the cache. On the desktop the meters' own padding spaces them. */}
612 <Box key="meters" flexDirection="row" flexWrap="wrap" alignItems="center" columnGap={isVscode ? 1 : model.gap} height={1} flexGrow={1} flexShrink={1} overflow="hidden">
613 {meters.slice(0, fit.meters ?? meters.length).map(meter)}
614 {head && fit.branch !== undefined && (
615 <Box key="git" flexDirection="row" justifyContent="flex-end" alignItems="center" columnGap={half} paddingLeft={isPixel ? 0 : 1} flexGrow={1} flexShrink={0}>
616 <Box key="git-name" flexDirection="row" alignItems="center" columnGap={half}>
617 {isPixel && icon('git-icon', head.worktree === undefined ? ICONS.branch : ICONS.worktree, head.worktree === undefined ? 'Branch' : 'Worktree branch')}
618 {/* The terminal has no icons: a label stands in, as "Cache" does. */}
619 {!isPixel && <Text dimColor>{GIT_WORD}</Text>}
620 <Text>{fit.branch}</Text>
621 {/* A name shown short is whole under the pointer, drawn over the
622 row and ending where the short one does. Unkeyed, so the
623 pointer on the name reveals it. An interactive Svg's
624 tooltip would sit in a frame that paints white each redraw.
625 Terminal cells cover what is under them; the desktop needs
626 a ground. */}
627 {fit.branch !== head.branch && (
628 <Box
629 position="absolute"
630 top={0}
631 right={0}
632 display="none"
633 hover={{ display: 'flex' }}
634 backgroundColor={isPixel ? TIP_BG : undefined}
635 paddingX={isPixel ? half : 0}
636 >
637 <Text color={isPixel ? TIP_TEXT : undefined}>{head.branch}</Text>
638 </Box>
639 )}
640 </Box>
641 {!isPixel && head.worktree !== undefined && <Text dimColor>{WORKTREE_NOTE}</Text>}
642 <Box key="git-divider" marginLeft={half} flexDirection="row" alignItems="center">
643 {/* With an empty alt the desktop drew nothing here. */}
644 {isPixel ? svg('divider', SVG_DIVIDER, 'Separator', 1, 14) : <Text dimColor>│</Text>}
645 </Box>
646 </Box>
647 )}
648 </Box>
649 <Box key="band-right" flexDirection="row" alignItems="center" columnGap={2 * half} flexShrink={0}>
650 {/* The time's Button pads it enough from the clock, so they sit as
651 close as the branch and its icon; VS Code's Text needs the half
652 cell, and the terminal's word keeps a whole one. */}
653 <Box key="cache" flexDirection="row" alignItems="center" columnGap={isDesktop ? 0 : half}>
654 {/* An icon or a dim word, then the value bright, as the branch is. */}
655 {isPixel ? icon('cache-icon', ICONS.clock, 'Prompt cache') : word('cache-label', CACHE_WORD, true)}
656 {word('cache-time', cacheLabel)}
657 </Box>
658 {toggleButton}
659 </Box>
660 </Box>
661 )
662 })
663}
664hooks/cache.ts 32 lines1import type { Limit } from '../types'
2
3type CacheOptions = {
4 force5m?: string
5 ttl?: string
6 setting?: unknown
7 enable1h?: string
8 limits: Limit[]
9}
10
11// Limits identify the subscription default. The host does not expose its
12// billing mode, so this remains an estimate when no TTL is configured.
13export const cacheTtl = ({ force5m, ttl, setting, enable1h, limits }: CacheOptions): number => {
14 if (force5m === '1') return 300_000
15 for (const value of [ttl, setting]) {
16 if (value === '5m') return 300_000
17 if (value === '1h') return 3_600_000
18 }
19 if (enable1h === '1') return 3_600_000
20 const plan = limits.filter(l => l.kind === 'five_hour' || l.kind === 'seven_day')
21 return plan.length > 0 && plan.every(l => l.percentUsed < 100) ? 3_600_000 : 300_000
22}
23
24// Whole minutes until the last one, then seconds: the label keeps its width
25// rather than changing every second. The band puts a clock or "Cache" before it.
26export const cacheText = (expiresAt: number | null, now: number): string => {
27 if (expiresAt === null) return '—'
28 const seconds = Math.max(0, Math.ceil((expiresAt - now) / 1000))
29 if (seconds === 0) return 'expired'
30 return seconds < 60 ? `${seconds}s` : `${Math.ceil(seconds / 60)}m`
31}
32hooks/fit.ts 177 lines1// The band's width budget. The meters' words, the cache and the toggle keep
2// their size; the bars give way first, down to a stub or on the desktop to a
3// ring, then the branch is cut short, then it goes, then meters go whole from
4// the right. The band's wrapping row still drops whatever this misjudges on
5// the terminal and the desktop, so nothing ever overlaps there.
6
7export type Meter = { label: string; percent: string; note: string }
8export type BandText = { meters: Meter[]; branch?: string; worktree: boolean; cache: string }
9// The bars' width (cells on the terminal, px on the desktop), whether rings
10// stand in for them, the branch as drawn: absent when it does not fit, and
11// how many meters are drawn, from the left: absent when all of them are.
12export type Fit = { bar: number; isRing?: boolean; branch?: string; meters?: number }
13
14export type Model = {
15 bar: { min: number; max: number }
16 // A ring `size` wide stands in for bars narrower than `min`, and sits
17 // `closer` to its figure than a bar does. Without one the bars stop at `min`.
18 ring?: { size: number; closer: number }
19 text: (s: string) => number
20 // Between the band's row items: the meters and the branch.
21 gap: number
22 // A meter with no bar.
23 meter: (m: Meter) => number
24 git: (branch: string, worktree: boolean) => number
25 // The cache and the toggle, with the gap before them.
26 right: (cache: string) => number
27}
28
29// The words the terminal spells out where the desktop draws icons.
30export const GIT_WORD = 'git'
31export const WORKTREE_NOTE = '· worktree'
32export const CACHE_WORD = 'Cache'
33export const OPEN_GLYPH = '+'
34
35export const BAR_CELLS = 8
36export const BAR_PX = 48
37export const ICON_PX = 13
38export const RING_PX = 14
39// A branch keeps this many characters, the ellipsis included, or goes.
40const BRANCH_MIN = 8
41
42const isWide = (c: number) =>
43 (c >= 0x1100 && c <= 0x115f) ||
44 (c >= 0x2e80 && c <= 0xa4cf) ||
45 (c >= 0xac00 && c <= 0xd7a3) ||
46 (c >= 0xf900 && c <= 0xfaff) ||
47 (c >= 0xfe30 && c <= 0xfe4f) ||
48 (c >= 0xff00 && c <= 0xff60) ||
49 (c >= 0xffe0 && c <= 0xffe6) ||
50 (c >= 0x1f300 && c <= 0x1faff) ||
51 c >= 0x20000
52
53export const cells = (s: string) => [...s].reduce((n, c) => n + (isWide(c.codePointAt(0) ?? 0) ? 2 : 1), 0)
54
55// Terminal cells are exact: the engine lays the band out in `bodyColumns`.
56// A meter is padded a cell each side, its words a cell apart; the branch is
57// "git main │" after a cell of padding, the divider a cell further off.
58export const TERMINAL: Model = {
59 bar: { min: 3, max: BAR_CELLS },
60 text: cells,
61 gap: 1,
62 meter: m => 2 + cells(m.label) + 2 + cells(m.percent) + (m.note ? 1 + cells(m.note) : 0),
63 git: (branch, worktree) => 1 + cells(GIT_WORD) + 1 + cells(branch) + (worktree ? 1 + cells(WORKTREE_NOTE) : 0) + 3,
64 right: cache => 2 + cells(CACHE_WORD) + 1 + cells(cache) + 2 + cells(OPEN_GLYPH),
65}
66
67// The desktop's text is proportional, so its widths are estimates, measured
68// from the Code tab at 14px and rounded up: a cell is about 8.1px, its gaps
69// half of one, and a plain Button pads its label 6px each side.
70export const CELL_PX = 8.1
71// The Code tab's column is about 768px wide at most; the band's padding
72// leaves this much inside it.
73const BAND_PX = 750
74const HALF = CELL_PX / 2
75const PAD = 6
76const PX: [RegExp, number][] = [
77 [/[ijl.,:;'|!`]/, 3.2],
78 [/[ftrI()[\]{}\\]/, 4.4],
79 [/[/-]/, 5.4],
80 [/ /, 3.6],
81 [/[scz]/, 5.8],
82 [/[mwMW%—]/, 10.5],
83 [/…/, 13],
84 [/1/, 5.4],
85 [/[0-9]/, 7.4],
86 [/[A-Z]/, 8.2],
87]
88export const px = (s: string) =>
89 [...s].reduce((n, c) => n + (isWide(c.codePointAt(0) ?? 0) ? 11 : (PX.find(([r]) => r.test(c))?.[1] ?? 7.2)), 0)
90const button = (s: string) => px(s) + 2 * PAD
91
92// A meter is padded half a cell each side, its words half a cell apart; the
93// meters touch. The branch is its icon, the name and the divider. A bar much
94// under half its width reads as a dash, so a ring takes its place.
95export const DESKTOP: Model = {
96 bar: { min: 28, max: BAR_PX },
97 ring: { size: RING_PX, closer: HALF },
98 text: px,
99 gap: 0,
100 meter: m => 2 * HALF + button(m.label) + 2 * HALF + button(m.percent) + (m.note ? HALF + button(m.note) : 0),
101 git: branch => ICON_PX + HALF + px(branch) + 2 * HALF + 1,
102 right: cache => 2 * HALF + ICON_PX + button(cache) + 2 * HALF + button(OPEN_GLYPH),
103}
104
105// VS Code draws every Button as a native button, plain or not, so the band's
106// words there are Text and the toggle is the one button. Its layout cell is
107// about 7.2px of the desktop's text measure, and Text carries no padding of
108// its own, so whole cells part the words, as the desktop's buttons do; a ring
109// sits half a cell from its figure.
110export const VSCODE_CELL_PX = 7.2
111const VSCODE_HALF = VSCODE_CELL_PX / 2
112const VSCODE_PAD = 9
113// The panel lays out about this many cells for each one bodyColumns counts,
114// measured off VS Code's own drawing.
115const VSCODE_COLUMN_CELLS = 1.3
116
117export const VSCODE: Model = {
118 bar: DESKTOP.bar,
119 ring: { size: RING_PX, closer: VSCODE_HALF },
120 text: px,
121 gap: VSCODE_CELL_PX,
122 meter: m => 2 * VSCODE_HALF + px(m.label) + 2 * VSCODE_CELL_PX + px(m.percent) + (m.note ? VSCODE_CELL_PX + px(m.note) : 0),
123 git: branch => ICON_PX + VSCODE_HALF + px(branch) + 2 * VSCODE_HALF + 1,
124 right: cache => 2 * VSCODE_HALF + ICON_PX + VSCODE_HALF + px(cache) + 2 * VSCODE_HALF + px(OPEN_GLYPH) + 2 * VSCODE_PAD,
125}
126
127// What the band may fill: the terminal's columns; on the desktop the pane's
128// width less a margin, within the column's own; in VS Code the panel's width,
129// less a cell each side for the frame it draws the band in.
130export const roomOf = (model: Model, bodyColumns: number) =>
131 model === DESKTOP
132 ? Math.min(BAND_PX, (bodyColumns - 2) * CELL_PX)
133 : model === VSCODE
134 ? (bodyColumns - 2) * VSCODE_COLUMN_CELLS * VSCODE_CELL_PX
135 : bodyColumns
136
137export const clip = (s: string, max: number) => ([...s].length > max ? `${[...s].slice(0, max - 1).join('')}…` : s)
138
139export const fitBand = (t: BandText, room: number, model: Model): Fit => {
140 const fit = fitAll(t, room, model)
141 if (t.meters.length === 0) return fit
142 // Even the narrowest meters overflow: the branch is gone already, and the
143 // meters go from the right until the rest fits. The first always stays.
144 const least = model.ring === undefined ? model.bar.min : model.ring.size - model.ring.closer
145 const takes = (k: number) =>
146 t.meters.slice(0, k).reduce((w, m) => w + model.meter(m) + least, 0) + Math.max(0, k - 1) * model.gap + model.right(t.cache)
147 if (takes(t.meters.length) <= room) return fit
148 let k = t.meters.length - 1
149 while (k > 1 && takes(k) > room) k--
150 return { ...fitAll({ ...t, meters: t.meters.slice(0, k), branch: undefined }, room, model), meters: k }
151}
152
153const fitAll = (t: BandText, room: number, model: Model): Fit => {
154 const n = t.meters.length
155 const fixed = t.meters.reduce((w, m) => w + model.meter(m), 0) + Math.max(0, n - 1) * model.gap + model.right(t.cache)
156 const barIn = (left: number) => (n === 0 ? model.bar.max : Math.min(model.bar.max, Math.floor(left / n)))
157 // The narrowest the meters draw: stub bars, or rings, and the room each
158 // takes from the row.
159 const least: Fit = model.ring === undefined ? { bar: model.bar.min } : { bar: model.ring.size, isRing: true }
160 const leastTakes = model.ring === undefined ? model.bar.min : model.ring.size - model.ring.closer
161 const sized = (bar: number): Fit => (bar >= model.bar.min ? { bar } : least)
162 const alone = sized(barIn(room - fixed))
163 if (t.branch === undefined) return alone
164 const withGit = (name: string) => fixed + (n > 0 ? model.gap : 0) + model.git(name, t.worktree)
165 const whole = barIn(room - withGit(t.branch))
166 if (whole >= model.bar.min) return { bar: whole, branch: t.branch }
167 // The narrowest meters, and the name whole or cut to what the row has left.
168 const left = room - withGit('') - n * leastTakes
169 const chars = [...t.branch].length
170 for (let k = chars; k >= Math.min(chars, BRANCH_MIN); k--) {
171 const cut = clip(t.branch, k)
172 if (model.text(cut) <= left) return { ...least, branch: cut }
173 }
174 // No room for the branch: it goes, and the bars take back its share.
175 return alone
176}
177hooks/git.ts 34 lines1import type { GitHead } from '../types'
2import { clip } from './fit'
3
4// One call answers all four: the git dir, the common dir, the working tree's
5// top and the branch ("HEAD" when detached).
6export const HEAD_ARGV = [
7 'git',
8 'rev-parse',
9 '--path-format=absolute',
10 '--git-dir',
11 '--git-common-dir',
12 '--show-toplevel',
13 '--abbrev-ref',
14 'HEAD',
15] as const
16
17export const isDetached = (stdout: string) => stdout.trim().split(/\r?\n/)[3] === 'HEAD'
18
19// A linked worktree keeps a git dir of its own under the common one; the main
20// working tree's are the same.
21export const parseHead = (stdout: string, sha = ''): GitHead | null => {
22 const [gitDir, commonDir, top, ref] = stdout.trim().split(/\r?\n/).map(l => l.trim())
23 if (!gitDir || !commonDir || !top || !ref) return null
24 const branch = ref === 'HEAD' ? sha.trim() : ref
25 if (!branch) return null
26 const norm = (p: string) => p.replace(/\\/g, '/').replace(/\/+$/, '')
27 return norm(gitDir) === norm(commonDir) ? { branch } : { branch, worktree: norm(top).split('/').pop() ?? top }
28}
29
30// The name after its last slash: "feature/NEXT-1777" is "NEXT-1777", and a
31// worktree's "claude/fix-band" is "fix-band". The band shortens it further
32// when the row is full, and shows the whole on hover.
33export const branchName = (head: GitHead, max = 48) => clip(head.branch.split('/').pop() || head.branch, max)
34types/index.d.ts 26 lines1export type Limit = { kind: string; percentUsed: number; resetsAt?: string }
2
3export type Segment = { name: string; tokens: number; color: string }
4
5export type Snapshot = {
6 tokens?: number
7 window: number
8 percent?: number
9 limits: Limit[]
10 usd?: number
11 /** Used categories, largest first, as /context and the usage popover colour them. */
12 segments: Segment[]
13 /** The auto-compact buffer, drawn after the used segments in a darker gray. */
14 buffer?: number
15 autoCompactAt?: number
16}
17
18/** The session's checkout: its branch (a short sha when detached) and, in a linked worktree, that worktree's folder name. */
19export type GitHead = { branch: string; worktree?: string }
20
21declare module 'claude-code' {
22 interface PluginState {
23 'usage-band': { snapshot: Snapshot | null; isOpen: boolean; cacheExpiresAt: number | null; git: GitHead | null }
24 }
25}
26