xterm-js-rendering-pitfalls

xterm-js-rendering-pitfalls is a skill for Claude Code, Codex from jiushiwon/wg-skills. It costs 203 tokens per session (2,602 once invoked), scanned A, original, Apache-2.0.

A troubleshooting guide for xterm.js terminals in Electron apps. It explains four recurring problems involving canvas text rendering, CSS variables, React state, terminal sizing, and theme changes.

In plain words
What is it for?
Use it when fixing font settings, terminal size calculations, theme updates, or React integration in an Electron app with local or SSH terminals.
Why use it?
It helps explain why terminal text can be misaligned, why commands such as vi navigation appear not to work, and why a theme switch may leave the terminal unchanged.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it when fixing font settings, terminal size calculations, theme updates, or React integration in an Electron app with local or SSH terminals.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/jiushiwon/wg-skills/xterm-js-rendering-pitfalls
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add jiushiwon/wg-skills --skill xterm-js-rendering-pitfalls
Clone the repo
git clone --depth 1 https://github.com/jiushiwon/wg-skills

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for xterm-js-rendering-pitfalls

README.md
[![agentmods](https://agentmods.dev/badge/skills/jiushiwon/wg-skills/xterm-js-rendering-pitfalls/github.svg)](https://agentmods.dev/skills/jiushiwon/wg-skills/xterm-js-rendering-pitfalls)
Your own site
<a href="https://agentmods.dev/skills/jiushiwon/wg-skills/xterm-js-rendering-pitfalls"><img src="https://agentmods.dev/badge/skills/jiushiwon/wg-skills/xterm-js-rendering-pitfalls/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for xterm-js-rendering-pitfalls

Your own site · 80×15
<a href="https://agentmods.dev/skills/jiushiwon/wg-skills/xterm-js-rendering-pitfalls"><img src="https://agentmods.dev/badge/skills/jiushiwon/wg-skills/xterm-js-rendering-pitfalls.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 203 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,602 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00203 $0.02602
Opus 5.5 $0.00081 $0.01041
Sonnet 5.5 $0.00041 $0.00520
Haiku 4.5 $0.00020 $0.00260

Measured 24d ago against content hash 2b4f293d92f6, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

xterm-js-rendering-pitfalls scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 24d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

frontend/electron/xterm-js-rendering-pitfalls/SKILL.md · 234 lines

How it starts

The opening of the file, as written. The whole thing — 234 lines — stays where its author put it; the contents beside it link to each section on GitHub.

xterm.js 渲染坑合集

本类项目(Electron + SSH/本地终端、底层用 @xterm/xterm 6.x + @xterm/addon-fit)一整天的调试 最终归纳出 4 个反复出现的坑——都和"xterm 是 canvas 渲染"+"CSS 变量和 React state 的盲区"有关。


坑 1:fontFamily 传 var(--font-mono) → vi 越界、gg/G 失效

现象

  • 终端字符排到视口外
  • vi 里 gg(到头)/ G(到尾)看着"没反应"
  • 内容已经铺满屏幕却到不了头尾

根因

  • xterm 用 canvas 的 ctx.font 渲染文字,ctx.font 不支持 CSS 变量
  • 'var(--font-mono)' 对 canvas 是非法值 → canvas 回退到浏览器默认字体
  • 但 fit() 测量单元尺寸时走的是 DOM getComputedStyle,能解析 var(),按 JetBrains Mono 算
  • 两个字体字形/字距不一致 → 终端"逻辑 rows/cols"和"实际显示"对不上
  • 远端 vi 按 TIOCSWINSZ 拿到的窗口尺寸和真实可见区域错位 → vi 屏幕坐标算错

修复

function resolveMonoFont(): string {
  try {
    const v = getComputedStyle(document.documentElement)
      .getPropertyValue('--font-mono').trim()
    if (v) return v
  } catch { /* ignore */ }
  return "'JetBrains Mono', 'Courier New', monospace"
}

// 创建 Terminal 时
new Terminal({
  fontFamily: resolveMonoFont(),  // ← 运行期解析后的字面量字体栈
  ...
})

关键:传字面量字符串 'JetBrains Mono', monospace 让 canvas 与测量用同一字体,尺寸/对齐才一致。


坑 2:xterm 主题切换不生效——state 孤岛

现象

  • 全局 <html data-theme="dark"> 切了,其他 UI 区域都跟随深色
  • 但 xterm 画布仍然是白色(light 配色)

根因

最容易掉进的写法:

// ThemeToggle.tsx
export function useTheme() {
  const [theme, setThemeState] = useState<Theme>(readStoredTheme)
  ...
  return { theme, setTheme }
}

// SshTerminal.tsx
const { theme } = useTheme()
useEffect(() => {
  term.options.theme = getXtermTheme(theme)
}, [theme])

致命陷阱:useTheme() 每个调用方各自 useState,主题切换不会跨组件传播——

  • ThemeToggle 点 dark 时它自己变 dark,但 SshTerminal 自己的 useState 还是 light
  • <html data-theme> 切了(CSS 变量全局生效,其他区域跟随)
  • 但 SshTerminal 的 effect 没被触发(依赖的 theme state 没变)
  • term.options.theme 仍是 light,画布白底

修复(架构级 lift state up)

把主题 state 提升到全局 Context/Reducer,所有组件共享:

// store/appContext.tsx
interface AppState {
  ...,
  theme: Theme
}

// reducer
case 'SET_THEME': return { ...state, theme: action.payload }

// AppProvider 唯一持有
const toggleTheme = useCallback(() => {
  dispatch({ type: 'SET_THEME', payload: state.theme === 'light' ? 'dark' : 'light' })
}, [state.theme])

// 各组件用 useApp() 拿
const { theme, toggleTheme } = useApp()

Read the full file on GitHub · 234 lines

Files

What ships with it

1 file beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 24d ago Changed 2b4f293d92f6
  2. 25d ago Changed · -17 lines 5667e02c9be7
  3. 2mo ago First seen · 251 lines · 203 tokens per session scan A 6cc496305ff5

Subscribe to this mod's changes

xterm-js-rendering-pitfalls is a skill published in the GitHub repository jiushiwon/wg-skills (154 stars, last pushed 7d ago), licensed Apache-2.0. It adds 203 tokens to every session and 2,602 once invoked, about $0.0008 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-30.

Related

xterm-js-rendering-pitfalls compared with skills from other repositories

xterm-js-rendering-pitfalls next to 6 skills from other repositories
ModTypeStarsLast pushLicenceScanTokensWorks with
xterm-js-rendering-pitfallsjiushiwon/wg-skillsSkill1547d agoApache-2.0A203Claude Code, Codex
systematic-debuggingopen-metadata/OpenMetadataSkill16ktodayApache-2.0A37Claude Code
log-error-digestzebbern/claude-code-guideSkill4.7ktodayMITA71Claude Code, Codex
observability-audit-sessionsIBM/mcp-context-forgeSkill4.9ktodayApache-2.0A64Claude Code, Codex
cwv-optimizeradobe/skillsSkill342todayApache-2.0A112Claude Code
ci-fixersipyourdrink-ltd/bernsteinSkill1.2ktodayApache-2.0A15Claude Code, Codex
diagnosing-missing-recordingsPostHog/posthogSkill41ktodaynoneA76Claude Code, Codex