Band above the prompt showing how full the context window is

hooks/register.tsx 51 lines1import type { Register } from 'claude-code'
2
3const WIDTH = 20
4
5const weather = (rawPercent: number) => {
6 const percent = Math.min(100, Math.max(0, rawPercent))
7 const filled = Math.round((percent / 100) * WIDTH)
8 const bar = '█'.repeat(filled) + '░'.repeat(WIDTH - filled)
9 const label = percent >= 85 ? 'stormy' : percent >= 60 ? 'cloudy' : 'clear'
10
11 return `Context ${bar} ${percent}% · ${label}`
12}
13
14export const register: Register = on => {
15 on('session.start', async ($, e, next) => {
16 await $.command.register({
17 name: 'token-weather',
18 description: 'Show how full the context window is',
19 })
20
21 return next(e)
22 })
23
24 on('command.run', { command: 'token-weather' }, async $ => {
25 const { context } = await $.session.usage()
26
27 return {
28 text:
29 context.percent === undefined
30 ? 'token-weather: no context reading yet (send a message first).'
31 : `token-weather: ${weather(context.percent)}`,
32 }
33 })
34
35 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
36 const { context } = await $.session.usage()
37
38 if (e.props.hasSurvey || context.percent === undefined) {
39 return next(e)
40 }
41
42 const { Box, Text } = $.ui.resolve(e)
43
44 return (
45 <Box>
46 <Text dimColor>{weather(context.percent)}</Text>
47 </Box>
48 )
49 })
50}
51