modscode EN

modscode / 図鑑 / context-meter

context-meter

A context window meter at the bottom right of the prompt; click it for the full breakdown

確かめやすいコードターミナル+デスクトップペインBoxButtonText

何をするか

  • 欄を描く
  • /ctx を足す
  • session.compact・session.measure・session.startにフックする

良いところ

  • ネットワークに出ない
  • プロセスを起こさない
  • 小さい(383 行)
  • TypeScript で書かれている

作り始めるためのコード

2 種類あります。上のデザインから modscode が書いた雛形と、その Mod 自身のコードのうち描いている部分です。

雛形(register.tsx)

register.tsx
import type { Register } from 'claude-code'

// Drawn like context-meter by Nafees S (pane), as modscode read it from the mod's code:
// https://modscode.com/gallery/lucenity0-context-meter/
// 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 pane: open it with $.ui.open({ id: 'my-pane', title: "Context" })
  on('ui.render', { component: 'Pane', requestId: 'my-pane' }, async ($, e) => {
    const { Box, Text, Button } = $.ui.resolve(e)
    const percentage = '…'
    const formatTokens = '…'
    const model = '…'
    const compactNote = '…'
    const name = '…'
    const formatTokens2 = '…'
    const percentOf = '…'
    const formatTokens3 = '…'
    const percentOf2 = '…'
    const formatTokens4 = '…'
    const percentOf3 = '…'
    const value = '…'
    const formatTokens5 = '…'
    const map = '…'
    const formatTokens6 = '…'
    const note = '…'
    const formatTokens7 = '…'
    const note2 = '…'
    const formatTokens8 = '…'
    const note3 = '…'
    const formatTokens9 = '…'
    const note4 = '…'
    const formatTokens10 = '…'
    const note5 = '…'
    const formatTokens11 = '…'
    const note6 = '…'
    const formatTokens12 = '…'
    const note7 = '…'
    const formatTokens13 = '…'
    const note8 = '…'
    const formatTokens14 = '…'
    const note9 = '…'
    const formatTokens15 = '…'
    const note10 = '…'
    const formatTokens16 = '…'
    const note11 = '…'
    const formatTokens17 = '…'
    const note12 = '…'
    const formatTokens18 = '…'
    return (
      <Box flexDirection="column">
        <Box flexDirection="row" justifyContent="space-between" columnGap={2}>
          <Text>
            <Text bold>
              {percentage}
              %
            </Text>
            <Text>
              {`  ${formatTokens}`}
            </Text>
            <Text dimColor>
              {` of ${formatTokens}`}
            </Text>
          </Text>
          <Text dimColor wrap="truncate-start">
            {model}
            {" · counting…"}
          </Text>
        </Box>
        <Text>
          <Text dimColor>
            ░░░░░░░░
          </Text>
          <Text dimColor>
            ░░░░░░░░
          </Text>
          <Text dimColor>
            ░░░░░░░░
          </Text>
        </Text>
        <Text dimColor>
          {formatTokens}
          {" free · "}
          {compactNote}
        </Text>
        <Box flexDirection="column" marginTop={1}>
          <Text>
            <Text>
              ●
            </Text>
            {` ${name} `}
            <Text dimColor>
              {formatTokens2}
            </Text>
            {"  "}
            <Text>
              ━━━━━━━━
            </Text>
            {"        "}
            <Text dimColor>
              {percentOf}
            </Text>
          </Text>
          <Text>
            <Text>
              ●
            </Text>
            {` ${name} `}
            <Text dimColor>
              {formatTokens3}
            </Text>
            {"  "}
            <Text>
              ━━━━━━━━
            </Text>
            {"        "}
            <Text dimColor>
              {percentOf2}
            </Text>
          </Text>
          <Text>
            <Text>
              ●
            </Text>
            {` ${name} `}
            <Text dimColor>
              {formatTokens4}
            </Text>
            {"  "}
            <Text>
              ━━━━━━━━
            </Text>
            {"        "}
            <Text dimColor>
              {percentOf3}
            </Text>
          </Text>
          <Text dimColor wrap="truncate-end">
            {`○ ${value} `}
            {formatTokens5}
            {`  ${map}, not in the window`}
          </Text>
        </Box>
        <Box marginTop={1}>
          <Text dimColor wrap="truncate-end">
            {"last request "}
            {formatTokens6}
            {" in"}
          </Text>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Text dimColor>
            memory
          </Text>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens7}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note2}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens8}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note3}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens9}
              </Text>
            </Box>
          </Box>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Text dimColor>
            mcp servers
          </Text>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note4}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens10}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note5}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens11}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note6}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens12}
              </Text>
            </Box>
          </Box>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Text dimColor>
            skills
          </Text>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note7}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens13}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note8}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens14}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note9}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens15}
              </Text>
            </Box>
          </Box>
        </Box>
        <Box flexDirection="column" marginTop={1}>
          <Text dimColor>
            agents
          </Text>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note10}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens16}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note11}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens17}
              </Text>
            </Box>
          </Box>
          <Box flexDirection="row" columnGap={1}>
            <Box flexShrink={0}>
              <Text>
                {`  ${name}`}
              </Text>
            </Box>
            <Box flexGrow={1} flexShrink={1}>
              <Text dimColor wrap="truncate-start">
                {note12}
              </Text>
            </Box>
            <Box flexShrink={0}>
              <Text>
                {formatTokens18}
              </Text>
            </Box>
          </Box>
        </Box>
        <Box marginTop={1} columnGap={1}>
          <Button label="Count exactly" hotkey="e" />
          <Button label="Refresh" hotkey="r" />
          <Button label="Compact" hotkey="c" />
          <Button label="Close" hotkey="q" role="dismiss" />
        </Box>
      </Box>
    )
  })
}

Mod 自身のコード

Mod のライセンス(MIT)に従って載せています。全文は下にあります。写すときは著作権表示を残してください。

context-meter/hooks/register.tsx 100〜240 行 · ペイン
  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
    const { Box, Text, Button } = $.ui.resolve(e)
    const current = await read($, details)
    const loading = await read($, isLoading)
    const live = await read($, fill)
    const width = Math.max(32, e.props.bodyColumns)

    if (current === null) {
      return <Text dimColor>{loading ? 'Counting the context window…' : 'No breakdown yet.'}</Text>
    }

    const used = current.categories.filter(category => category.kind === 'used').sort((a, b) => b.tokens - a.tokens)
    const deferred = current.categories.filter(category => category.kind === 'deferred')
    const free = current.categories.find(category => category.kind === 'free')?.tokens ?? 0
    const usedTotal = used.reduce((sum, category) => sum + category.tokens, 0)
    const largest = used[0]?.tokens ?? 1

    const nameWidth = Math.min(20, Math.max(8, ...used.map(category => category.name.length)))
    const barWidth = Math.max(4, width - nameWidth - 18)
    const accent = levelColor(current.percentage)

    const compactNote = !current.isAutoCompactEnabled || current.autoCompactThreshold === undefined
      ? 'auto-compact off'
      : current.autoCompactThreshold > current.totalTokens
        ? `compacts in ${formatTokens(current.autoCompactThreshold - current.totalTokens)}`
        : 'compacts next turn'

    const section = (title: string, lines: Line[]) =>
      lines.length > 0 && (
        <Box flexDirection="column" marginTop={1}>
          <Text dimColor>{title}</Text>
          {lines.map(line => (
            <Box flexDirection="row" columnGap={1}>
              <Box flexShrink={0}>
                <Text>{`  ${line.name}`}</Text>
              </Box>
              <Box flexGrow={1} flexShrink={1}>
                {line.note !== undefined && (
                  <Text dimColor wrap="truncate-start">
                    {line.note}
                  </Text>
                )}
              </Box>
              <Box flexShrink={0}>
                <Text>{formatTokens(line.tokens).padStart(6)}</Text>
              </Box>
            </Box>
          ))}
        </Box>
      )

    const memory = current.memoryFiles.map(file => {
      const path = file.name.replace(/^\/(Users|home)\/[^/]+/, '~')
      const cut = path.lastIndexOf('/')
      return { name: path.slice(cut + 1), tokens: file.tokens, note: path.slice(0, cut) }
    })

    return (
      <Box flexDirection="column">
        <Box flexDirection="row" justifyContent="space-between" columnGap={2}>
          <Text>
            <Text bold {...(accent === undefined ? {} : { color: accent })}>
              {current.percentage}%
            </Text>
            <Text>{`  ${formatTokens(current.totalTokens)}`}</Text>
            <Text dimColor>{` of ${formatTokens(current.maxTokens)}`}</Text>
          </Text>
          <Text dimColor wrap="truncate-start">
            {current.model}
            {loading ? ' · counting…' : current.isExact ? ' · exact' : ' · estimated'}
          </Text>
        </Box>

        <Text>
          {segments(current.categories, current.maxTokens, width).map(part =>
            part.kind === 'free' ? (
              <Text dimColor>{'░'.repeat(part.cells)}</Text>
            ) : (
              <Text color={part.color}>{(part.kind === 'buffer' ? '▒' : '█').repeat(part.cells)}</Text>
            ),
          )}
        </Text>
        <Text dimColor>
          {formatTokens(free)} free · {compactNote}
        </Text>

        <Box flexDirection="column" marginTop={1}>
          {used.map(category => (
            <Text>
              <Text color={category.color}>●</Text>
              {` ${fit(category.name, nameWidth)} `}
              <Text dimColor>{formatTokens(category.tokens).padStart(6)}</Text>
              {'  '}
              <Text color={category.color}>
                {'━'.repeat(Math.max(1, Math.round((category.tokens / largest) * barWidth)))}
              </Text>
              {' '.repeat(barWidth - Math.max(1, Math.round((category.tokens / largest) * barWidth)))}
              <Text dimColor>{percentOf(category.tokens, usedTotal).padStart(5)}</Text>
            </Text>
          ))}
          {deferred.length > 0 && (
            <Text dimColor wrap="truncate-end">
              {`○ ${fit('on demand', nameWidth)} `}
              {formatTokens(deferred.reduce((sum, category) => sum + category.tokens, 0)).padStart(6)}
              {`  ${deferred.map(category => category.name).join(', ')}, not in the window`}
            </Text>
          )}
        </Box>

        {live.tokens !== undefined && (
          <Box marginTop={1}>
            <Text dimColor wrap="truncate-end">
              last request {formatTokens(live.tokens)} in
              {current.lastResponse === null
                ? ''
                : ` · ${formatTokens(current.lastResponse.cacheRead)} cached · ${formatTokens(
                    current.lastResponse.output,
                  )} out`}
            </Text>
          </Box>
        )}

        {section('memory', memory)}
        {section('mcp servers', current.mcpServers)}
        {section(
          'skills',
          current.skills === undefined || current.skills.count === 0
            ? []
            : [{ name: `${current.skills.count} listed`, tokens: current.skills.tokens }],
        )}
        {section('agents', current.agents)}

        <Box marginTop={1} columnGap={1}>
          <Button key="exact" label="Count exactly" hotkey="e" onPress={() => loadDetails($, 'full')} />
          <Button key="refresh" label="Refresh" hotkey="r" onPress={() => loadDetails($, 'summary')} />
          <Button key="compact" label="Compact" hotkey="c" onPress={() => $.session.compact()} />
          <Button key="close" label="Close" hotkey="q" role="dismiss" onPress={() => $.ui.close({ id: PANE })} />
        </Box>
      </Box>
    )
  })
ライセンスの全文(MIT)
MIT License

Copyright (c) 2026 Nafees S

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.

コードが呼ぶもの

見つからず

コードから読み取ったデザイン

読み取ったコミットのソースから描いたもので、動かした結果ではありません。一覧は 3 回描き、条件はデスクトップアプリで描く最初の分岐を取り、動かしたときにだけ決まる値は仮の例(点線の下線)にするか省きます。色と書体はこのサイトのものです。

コードから読めること

2026-10-06 に読んだコミット 4a3ed32908c2 のソースから。最初のまとまりが載せる条件で、残りは自分で判断するための材料です。後のコミットは、次に読むまで見ていません。

載せる条件: 確かめやすいコード
入口を読んだhooks/hooks.json が名指すモジュールをすべて読んだ。context-meter/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 .)、触るものを見て、読んだコミットを入れてください。

リポジトリの .claude-plugin/marketplace.json にこの Mod が載っていないため、入れる手順は出せません。コードとして読むか、作者の README に従ってください。

Claude に聞く

modscode のコネクタを入れると、Claude がこのデザイン・コードが触るもの・コードを読んで、それをもとに作れます。たとえば:

modscode のデザイン「lucenity0-context-meter」をコードごと読んで、同じようなペインを作って

Claude から探す: MCP コネクタ

似たデザイン