usage-mod
在输入框上方常驻显示上下文占用、5 小时额度、每周额度和重置时间,带一键压缩上下文的按钮
確かめやすいコードターミナル+デスクトップバンドトーストBoxButtonSvgText
何をするか
- プロンプトの上の帯を描く
- session.measure・session.start・turn.completeにフックする
良いところ
- ネットワークに出ない
- プロセスを起こさない
- TypeScript で書かれている
- デスクトップ専用の分岐がある
作り始めるためのコード
2 種類あります。上のデザインから modscode が書いた雛形と、その Mod 自身のコードのうち描いている部分です。
雛形(register.tsx)
register.tsx
import type { Register } from 'claude-code'
// Drawn like usage-mod by qingyashizi (band, toast), as modscode read it from the mod's code:
// https://modscode.com/gallery/qingyashizi-usage-mod/
// Written by modscode from that reading, not copied from the mod: use it as you like.
// Each value below stands for one the mod works out at run time; put in your own.
export const register: Register = on => {
// the band above the prompt; what else goes there is drawn under it
on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
const { Box, Text, Button, Svg } = $.ui.resolve(e)
const statsTitle = '…'
const label = '…'
const value = '…'
const label2 = '…'
const value2 = '…'
const label3 = '…'
const value3 = '…'
const turnsText = '…'
const turnsText2 = '…'
const seriesLabel = '…'
const chartDesktopOnly = '…'
const lineIconSvg = '…'
const tip = '…'
const text = '…'
const text2 = '…'
const text3 = '…'
const text4 = '…'
const text5 = '…'
const text6 = '…'
const statsTip = '…'
return (
<Box width="100%" flexDirection="column" gap={1}>
<Box flexDirection="column" width="100%" alignItems="center">
<Box width="100%" justifyContent="center">
<Box position="absolute" top={0} left={0}>
<Text bold>
{statsTitle}
</Text>
</Box>
<Box gap={3}>
<Box gap={1}>
<Text dimColor>
{label}
</Text>
<Text bold>
{value}
</Text>
</Box>
<Box gap={1}>
<Text dimColor>
{label2}
</Text>
<Text bold>
{value2}
</Text>
</Box>
<Box gap={1}>
<Text dimColor>
{label3}
</Text>
<Text bold>
{value3}
</Text>
</Box>
</Box>
<Box position="absolute" top={0} right={0}>
<Text bold>
{turnsText}
</Text>
</Box>
</Box>
<Box flexDirection="column" width="100%" alignItems="center">
<Box width="100%" justifyContent="center" gap={2}>
<Box position="absolute" top={0} right={0}>
<Text bold>
{turnsText2}
</Text>
</Box>
<Box gap={1}>
<Text>
●
</Text>
<Button label={`${seriesLabel}`} plain />
</Box>
<Box gap={1}>
<Text>
●
</Text>
<Button label={`${seriesLabel}`} plain />
</Box>
<Box gap={1}>
<Text>
●
</Text>
<Button label={`${seriesLabel}`} plain />
</Box>
</Box>
<Text dimColor>
{chartDesktopOnly}
</Text>
</Box>
</Box>
<Box width="100%" justifyContent="center" gap={1}>
<Box width={3} height={1} position="relative" justifyContent="center">
<Svg source={`${lineIconSvg}`} alt={`${tip}`} width={24} height={22} />
<Box position="absolute" top={0} left={0} width={3} height={1}>
<Button label=" " plain />
</Box>
<Button label="🗜" plain />
<Box position="absolute" top={0} left={3} height={1} justifyContent="center" backgroundColor="#D77757" display="none">
<Text color="#161616">
{tip}
</Text>
</Box>
</Box>
<Box height={1} backgroundColor="#3f3f46">
<Box position="absolute" top={0} left={0} height={1} backgroundColor="#D77757" overflow="hidden">
<Box position="absolute" top={0} left="0" height={1} justifyContent="center">
<Text color="#161616" wrap="truncate-end">
{text}
</Text>
</Box>
</Box>
<Box position="absolute" top={0} height={1} overflow="hidden">
<Box position="absolute" top={0} height={1} justifyContent="center">
<Text color="#ececec" wrap="truncate-end">
{text2}
</Text>
</Box>
</Box>
</Box>
<Box height={1} backgroundColor="#3f3f46">
<Box position="absolute" top={0} left={0} height={1} backgroundColor="#D77757" overflow="hidden">
<Box position="absolute" top={0} left="0" height={1} justifyContent="center">
<Text color="#161616" wrap="truncate-end">
{text3}
</Text>
</Box>
</Box>
<Box position="absolute" top={0} height={1} overflow="hidden">
<Box position="absolute" top={0} height={1} justifyContent="center">
<Text color="#ececec" wrap="truncate-end">
{text4}
</Text>
</Box>
</Box>
</Box>
<Box height={1} backgroundColor="#3f3f46">
<Box position="absolute" top={0} left={0} height={1} backgroundColor="#D77757" overflow="hidden">
<Box position="absolute" top={0} left="0" height={1} justifyContent="center">
<Text color="#161616" wrap="truncate-end">
{text5}
</Text>
</Box>
</Box>
<Box position="absolute" top={0} height={1} overflow="hidden">
<Box position="absolute" top={0} height={1} justifyContent="center">
<Text color="#ececec" wrap="truncate-end">
{text6}
</Text>
</Box>
</Box>
</Box>
<Box width={3} height={1} position="relative" justifyContent="center">
<Svg source={`${lineIconSvg}`} alt={`${tip}`} width={24} height={22} />
<Box position="absolute" top={0} left={0} width={3} height={1}>
<Button label=" " plain />
</Box>
<Button label="≡" plain />
<Box position="absolute" top={0} left={3} height={1} justifyContent="center" backgroundColor="#D77757" display="none">
<Text color="#161616">
{statsTip}
</Text>
</Box>
</Box>
</Box>
{await next(e)}
</Box>
)
})
}
Mod 自身のコード
Mod のライセンス(MIT)に従って載せています。全文は下にあります。写すときは著作権表示を残してください。
plugins/usage-mod/hooks/register.tsx 386〜545 行 · バンド
on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
if (e.props.hasSurvey) return next(e)
const data = await read($, usage)
const at = await read($, now)
const isOpen = await read($, statsOpen)
const { total, recent } = await read($, stats)
const hidden = await read($, hiddenSeries)
const five = find(data.limits, 'five_hour')
const week = find(data.limits, 'seven_day')
const elements = $.ui.resolve(e)
const { Box, Button, Text } = elements
// 折线图是 SVG,只有桌面端等远程界面有这个元素。
const Svg = 'Svg' in elements ? elements.Svg : undefined
const isTerminal = e.surface === 'terminal'
if (e.props.bodyColumns < 78) {
const short = [
data.context === undefined ? `${L.shortCtx} —` : `${L.shortCtx} ${Math.round(data.context)}%`,
five === undefined ? `${L.shortFive} —` : `${L.shortFive} ${Math.round(five.percentUsed)}%`,
week === undefined ? `${L.shortWeek} —` : `${L.shortWeek} ${Math.round(week.percentUsed)}%`,
].join(' · ')
return (
<Box width="100%" justifyContent="center">
<Text>{short}</Text>
</Box>
)
}
// 三块等宽,块之间各留一格;减去的是两个按钮和四处间隙占的格数,不再额外留边距。
const cells = Math.max(20, Math.floor((e.props.bodyColumns - 4 - 2 * BUTTON_CELLS) / 3))
// 几何全部交给 Box:底色块铺满整块,橙色填充按百分比取宽度。
// 文字画两层、各裁一半:填充内是深色字,填充外是浅色字,在填充边界处切开。
// 不用空格撑宽度,因为桌面端字体不是等宽的,空格撑出来的宽度和 Box 的宽度对不上。
const block = (key: string, label: string, percent: number | undefined, detail: string) => {
const text = percent === undefined ? `${label} —` : `${label} ${Math.round(percent)}%${detail === '' ? '' : ` · ${detail}`}`
const filled = percent === undefined ? 0 : percent <= 0 ? 0 : Math.min(cells, Math.max(1, Math.round((percent / 100) * cells)))
const layer = (color: string, shift: number) => (
<Box position="absolute" top={0} left={shift} width={cells} height={1} justifyContent="center">
<Text color={color} wrap="truncate-end">
{text}
</Text>
</Box>
)
return (
<Box key={key} width={cells} height={1} backgroundColor={WARM}>
{filled > 0 && (
<Box position="absolute" top={0} left={0} width={filled} height={1} backgroundColor={ORANGE} overflow="hidden">
{layer(DARK, 0)}
</Box>
)}
{filled < cells && (
<Box position="absolute" top={0} left={filled} width={cells - filled} height={1} overflow="hidden">
{layer(LIGHT, -filled)}
</Box>
)}
</Box>
)
}
const ctxDetail = data.tokens === undefined ? '' : `${tokensText(data.tokens)} / ${tokensText(data.window)}`
const fiveDetail = five === undefined ? '' : `${remaining(L, five.resetsAt, at)} · ${clock(L, five.resetsAt, at)}`
const weekDetail = week === undefined ? '' : `${remaining(L, week.resetsAt, at)} · ${clock(L, week.resetsAt, at)}`
const compact = async () => {
$.ui.toast(L.compacting)
try {
// 不用 $.session.compact():桌面应用的会话是 SDK 无头会话,引擎不让插件直接调它。
// 改成像用户输入 /compact 那样运行这条命令;引擎文档说回复进行中会排队到这一轮结束。
await $.command.run({ command: 'compact' })
$.ui.toast(L.compacted)
} catch (err) {
$.ui.toast(L.notCompacted(err instanceof Error ? err.message : String(err)))
}
}
const toggleStats = () => void update($, statsOpen, open => !open)
const toggleSeries = (key: SeriesKey) =>
void update($, hiddenSeries, old => (old.includes(key) ? old.filter(one => one !== key) : [...old, key]))
// 展开的明细:顶上是合计,中间是折线图,下面是曲线开关。画在横条里、用量条的上方,位置固定。
const stat = (key: string, label: string, value: string) => (
<Box key={key} gap={1}>
<Text dimColor>{label}</Text>
<Text bold>{value}</Text>
</Box>
)
// 盖在折线图上的一排透明感应区,每轮一个,宽度按比例(flexGrow)和图里的点对齐(图是固定像素宽,Box 的宽度是格数;
// 百分比宽度只收整数,量不准,所以按像素数当比例分)。鼠标移到某一轮上,同组(scope)的东西一起亮:这一列变成淡淡的高亮,图下面固定一行里显示那一轮的明细。
// 悬停是应用自己处理的,没有任何东西回到插件,所以不会触发横条重画,图也就不会闪。
// 横条的高度是应用定的(最多窗口的一半,放不下就滚动),所以图的高度跟着可用行数走,版面压紧:
// 标题、曲线开关、关闭按钮一行,合计一行,其余都给图。一行大约按 20 像素算。
const chartW = Math.round(Math.min(CHART_MAX_W, Math.max(CHART_MIN_W, e.props.bodyColumns * CHART_PX_PER_CELL)))
const chartH = Math.max(140, Math.min(CHART_MAX_H, (e.props.maxRows - 5) * 20))
// 第一行:合计在整行里居中,标题贴左、共几轮贴右(都不占位置)。放不下的东西按顺序让位:
// 先去掉标题,再把"共几轮"挪到第二行,还不够就去掉合计里的命中率、新增输入。英文、日文的文字比中文长得多,所以按实际文字量算。
const cols = e.props.bodyColumns
const GAP = 3
let items: Array<[string, string, string]> = [
['in', L.sIn, countText(total.input)],
['out', L.sOut, countText(total.output)],
['create', L.sCreate, countText(total.cacheCreate)],
['hit', L.sHit, countText(total.cacheRead)],
['rate', L.sRate, hitRate(total.input, total.cacheRead, total.cacheCreate)],
]
const rowCells = (list: typeof items) => list.reduce((n, [, label, value]) => n + cellsOf(label) + 1 + cellsOf(value), 0) + GAP * (list.length - 1)
for (const drop of ['rate', 'in']) if (rowCells(items) + 4 > cols) items = items.filter(([key]) => key !== drop)
const freeSide = (cols - rowCells(items)) / 2
const turnsText = L.statsTurns(total.turns)
const showTitle = total.turns === 0 || freeSide >= cellsOf(L.statsTitle) + 2
const showTurnsTop = freeSide >= cellsOf(turnsText) + 2
const panel = (
<Box key="stats-panel" flexDirection="column" width="100%" alignItems="center">
{/* 第一行:五项合计居中,标题贴左、共几轮贴右;第二行:四条曲线的开关,居中。没有关闭按钮,收起靠右下角的图标。放不下时的让位顺序见上面。 */}
<Box width="100%" justifyContent="center">
{showTitle && (
<Box position="absolute" top={0} left={0}>
<Text bold>{L.statsTitle}</Text>
</Box>
)}
{total.turns > 0 && (
<Box gap={GAP}>{items.map(([key, label, value]) => stat(key, label, value))}</Box>
)}
{total.turns > 0 && showTurnsTop && (
<Box position="absolute" top={0} right={0}>
<Text bold>{turnsText}</Text>
</Box>
)}
</Box>
{total.turns === 0 ? (
<Text dimColor>{L.statsEmpty}</Text>
) : (
<Box flexDirection="column" width="100%" alignItems="center">
{Svg !== undefined && (
<Box width="100%" justifyContent="center" gap={2}>
{!showTurnsTop && (
<Box position="absolute" top={0} right={0}>
<Text bold>{turnsText}</Text>
</Box>
)}
{SERIES.map(one => (
<Box key={one.key} gap={1}>
<Text color={one.color} dimColor={hidden.includes(one.key)}>
●
</Text>
<Button key={`toggle-${one.key}`} label={seriesLabel(L, one.key)} plain dimColor={hidden.includes(one.key)} onPress={() => toggleSeries(one.key)} />
</Box>
))}
</Box>
)}
{Svg === undefined ? (
<Text dimColor>{L.chartDesktopOnly}</Text>
) : (ライセンスの全文(MIT)
MIT License Copyright (c) 2026 qingyashizi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
コードが呼ぶもの
ファイル書き込み
コードから読めること
2026-10-06 に読んだコミット d114c7b07637 のソースから。最初のまとまりが載せる条件で、残りは自分で判断するための材料です。後のコミットは、次に読むまで見ていません。
| 載せる条件: 確かめやすいコード | |
|---|---|
| 入口を読んだ | hooks/hooks.json が名指すモジュールをすべて読んだ。plugins/usage-mod/hooks/register.tsx |
| 解析できる | すべてのファイルが TypeScript か JavaScript として解析できる。2 ファイルを解析 |
| コードが Mod の中で完結 | import は Mod 自身のファイルかエンジンのモジュールだけ。パッケージも require もない。コードはすべて Mod の中 |
| 読める | 圧縮した行・eval・new Function・計算した道の import()・符号化した塊がない。圧縮・eval・符号化した塊なし |
| 呼び出しが見える | 通信とプログラム起動はその場で $.http.fetch・$.process.run と書き、他へ渡さない。外への呼び出しはすべてその場の $ で |
| 入れる前に自分で判断する材料 | |
| 描く場所 | コードから読んだ、描く先。ターミナル・デスクトップアプリ・両方・何も描かない(フックだけ: 見張りやプロンプトの書き換え)のどれか。出すだけで、外す理由にはしない。ターミナルとデスクトップアプリに同じように描く |
| 入れる手順 | リポジトリ自身の .claude-plugin/marketplace.json にこの Mod が載っていて、ページに書いた手順でそのマーケットプレイスから入れられるか。リポジトリの .claude-plugin/marketplace.json に載っており、そこから入れられる |
| Windows のプログラム | $.process で起動するプログラムが Windows にあるか(tail・date・open・osascript・/usr/… などは無い)。Windows にないプログラムを使わない |
| 通信 | fetch の送り先(https のホスト)がコードに書いてあるか。通信なし |
| プログラム | 起動するプログラムの名前がコードに書いてあり、シェル・インタプリタ・通信用の道具でないか。プログラムを起動しない |
| 読んで送る | ファイルや環境変数を読み、かつ通信するか。通信しない |
| ソースの鍵 | ソースに API キーや秘密鍵らしきものがあるか。ソースに鍵なし |
| ライセンス | オープンなライセンス(MIT・Apache-2.0・BSD・ISC・Unlicense・0BSD・CC0)で全文があれば、modscode にコードを載せる。違えばリンクだけ。MIT |
コードの文面を読んだだけで、安全の審査ではありません。動かしてはおらず、起動するプログラムの中で何が起きるかも見えません。入れる前の確認はあなたの役目です。 載せる条件
入れる
入れる前に、必ず自分で確かめてください。Mod はサンドボックスなしで、あなたの権限で動きます。modscode はこのコードの安全を保証しません。下のコードを読み(またはフォルダーで claude plugin validate .)、触るものを見て、読んだコミットを入れてください。
リポジトリがそのままマーケットプレイスです。このマーケットプレイスを足し(ターミナルでは次のコマンド、デスクトップアプリではプラグインの設定)、そこから Mod を入れます。
/plugin marketplace add qingyashizi/claude-usage-mod/plugin install usage-mod@usage-mod入るのはマーケットプレイスの最新のコミットで、ここで読んだもの(d114c7b07637)と違うことがあります。入れる前に見比べてください。
Claude に聞く
modscode のコネクタを入れると、Claude がこのデザイン・コードが触るもの・コードを読んで、それをもとに作れます。たとえば:
modscode のデザイン「qingyashizi-usage-mod」をコードごと読んで、同じようなペインを作って似たデザイン
-
▴Token OptimizerCheckpointCheckpointCheckpointbuildStart freshbuildStart freshbuildStart freshbuildCheckpointCheckpointCheckpointCheckpointCheckpointCheckpointCheckpointCheckpointCheckpointbuildbuildbuildTokens savedSaved Clean up tokens in 30 days (chat 2 this session)Claude に頼む…
-
plan-progress is on. A bar appears when Claude starts a task with several steps.Claude に頼む…
-
Claude に頼む…Fix loginRemoveCopy markdownacme/app · mainCopy linkCopy markdownbuild2h 10mbuild2h 10mbuild2h 10mHosted on uploads.sh, not committed to the repo.They will appear in a comment on the pull request once opened.
-
何をするか
- プロンプトの上の帯・欄を描く
- /progress を足す
- ツールの呼び出しを見張る(止めることがある)
- classic.SessionStart・prompt.compose・prompt.submit・session.start ほか 2 個にフックする
-
Auto compact%System tools Compact▲Context is already at 42%, past Weekly%. Auto compact:NowAfter my next compactOnly in new chatsUsage limits show after the first reply.Auto compactToolsbuild42%Toolsbuild42%Toolsbuild42%Context12.4 free of 12.4k42% usedbuild12.4kbuild12.4kbuild12.4kClaude に頼む…claude-code-usage-quota: Compact
-
Claude に頼む…Skins0 Your promptadd rate limiting to the hub api0 Tool rows: finished, running, failedYour prompt Spinner0 Folded group of reads and searches●main · note 3 · note 3 · note 30 Edit diff cardDesktop only; the terminal keeps Claude Code's diff.0 Shell output cardDesktop only.0 Code block cardDesktop only; the terminal keeps Claude Code's markdown.0 TableAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentCopyAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentCopyAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentAccentCopy0 SpinnerAccent SpinnerSpinnerSpinner… 2h 10m0 Band above the prompt: normal, then nudging to compact█████░░░ 42% build█████░░░ 42% build█████░░░ 42% buildContext is 42% full0 Compact now0 Band above a questionTable BashBashBash0 Turn footer (terminal)3 build in 2h 10m · Grep · +3 −3