Current session (5h) and weekly usage as progress bars above the prompt

新裝置:
git clone git@github.com:felixchen-tw/claude-config.git ~/.claude # ~/.claude 已存在就先移開,再把憑證搬回來
外部 skill(ai-level-check、speak-human-tw):跑一次 setup(會先 git pull 這個 repo),會 clone 到固定位置再 symlink/junction 進 skills/,可重複執行,已 clone 的會 pull 更新。symlink 不進 git,所以每台各跑一次。
sh ~/.claude/setup.sh # Ubuntu / macOS
powershell -ExecutionPolicy Bypass -File $HOME\.claude\setup.ps1 # Windows 11,junction 不需管理員
要加新的外部 skill,就在兩支腳本各加一行 skill 呼叫。要更新外部 skill,重跑 setup 即可。
啟動 Claude Code 後,settings.json 裡的 plugin 會自動安裝。 felix-mods 的 marketplace 路徑寫成 ~/.claude/mods(Claude Code 會展開 ~),Ubuntu/macOS/Windows 共用同一份 settings.json,不用改。 iTerm2 的狀態 hook 只在 ~/.config/iterm2/cc-status 存在時執行,其他機器自動略過。 skills/ 裡的 caveman 系列與 find-skills 是從 npx skills 複製進來的實體檔(不再 symlink 到 ~/.agents),要更新就重跑 npx skills add JuliusBrussee/caveman -g 後再複製進來。 claude.ai 內建的 skills(skills/synced/)跟著帳號同步,不用處理。
同步:git -C ~/.claude pull / git -C ~/.claude commit -am ... && git push(新檔要先 git add)
Claude Code 會把本機專屬的 autoMode 區塊(含 /Users/felix 路徑)寫進 settings.json,而 autoMode 只能放 user 或 managed 設定,沒有 user 層級的 local 檔可以放,所以這台用 git update-index --skip-worktree settings.json 讓 git 忽略本機改動。副作用:
git status 看起來乾淨,但遠端改過 settings.json 時 git pull(和 setup.sh)會報 Your local changes ... would be overwritten。commit -am 不會帶到 settings.json;這台改的共用設定要照下面流程手動處理。這台改用 sh ~/.claude/sync-pull.sh 取代 git pull(setup.sh 也是呼叫它):pull 被擋時會備份到 backups/settings.json.pre-pull、取遠端版本、把本機 autoMode 合回去、再設回 skip-worktree;第二次 pull 仍失敗就還原本機檔。需要 jq。
要從這台推 settings.json 的改動:先 --no-skip-worktree,用 jq 'del(.autoMode)' 產生要 commit 的版本,commit 推完再把 autoMode 合回去、設回 skip-worktree。
hooks/register.tsx 72 lines1import { atom, read, update } from 'claude-code'
2import type { Register, SessionRateLimit } from 'claude-code'
3
4import type { Usage } from '../types'
5
6const usage = atom({ plugin: 'usage-meter', key: 'usage' } as const, null)
7
8export const bar = (pct: number, width = 15) => {
9 const n = Math.round((Math.min(Math.max(pct, 0), 100) / 100) * width)
10 return '█'.repeat(n) + '░'.repeat(width - n)
11}
12
13const fmtDate = (iso: string) => {
14 const d = new Date(iso)
15 return `${d.getMonth() + 1}/${d.getDate()} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`
16}
17
18// Green while plenty is left, yellow past half used, red past 80%.
19export const tone = (pct: number) => (pct >= 80 ? 'red' : pct >= 50 ? 'yellow' : 'green')
20
21// Account-wide windows, so a new conversation does not reset them.
22const toUsage = (rateLimits: SessionRateLimit[]): Usage => {
23 const five = rateLimits.find(r => r.kind === 'five_hour')
24 const week = rateLimits.find(r => r.kind === 'seven_day')
25 return {
26 sessionPercent: five?.percentUsed,
27 sessionResetsAt: five?.resetsAt,
28 weekPercent: week?.percentUsed,
29 weekResetsAt: week?.resetsAt,
30 }
31}
32
33export const register: Register = on => {
34 on('session.start', async ($, e, next) => {
35 const { rateLimits } = await $.session.usage()
36 await update($, usage, () => toUsage(rateLimits))
37 return next(e)
38 })
39
40 on('session.measure', async ($, e, next) => {
41 await update($, usage, () => toUsage(e.rateLimits))
42 return next(e)
43 })
44
45 on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
46 const u = await read($, usage)
47 if (e.props.hasSurvey || u === null) return next(e)
48
49 const below = await next(e)
50 const { Box, Text } = $.ui.resolve(e)
51 const meter = (pct?: number, resetsAt?: string) =>
52 pct === undefined
53 ? <Text dimColor>(尚無資料)</Text>
54 : <Text>
55 <Text color={tone(pct)}>{bar(pct)} {pct.toFixed(0)}%</Text>
56 <Text dimColor>{resetsAt ? ` 重置 ${fmtDate(resetsAt)}` : ''}</Text>
57 </Text>
58
59 return (
60 <Box flexDirection="column">
61 <Text>
62 <Text dimColor>Session </Text>
63 {meter(u.sessionPercent, u.sessionResetsAt)}
64 <Text dimColor> 本週 </Text>
65 {meter(u.weekPercent, u.weekResetsAt)}
66 </Text>
67 {below}
68 </Box>
69 )
70 })
71}
72types/index.d.ts 13 lines1export type Usage = {
2 sessionPercent?: number
3 sessionResetsAt?: string
4 weekPercent?: number
5 weekResetsAt?: string
6}
7
8declare module 'claude-code' {
9 interface PluginState {
10 'usage-meter': { usage: Usage | null }
11 }
12}
13