π File detail
ink/line-width-cache.ts
π― Use case
This file lives under βink/β, which covers Ink terminal UI (layouts, TTY IO, keyboard, renderer components). On the API surface it exposes lineWidth β mainly functions, hooks, or classes. It composes internal code from stringWidth (relative imports). What the file header says: During streaming, text grows but completed lines are immutable. Caching stringWidth per-line avoids re-measuring hundreds of unchanged lines on every token (~50x reduction in stringWidth calls).
Generated from folder role, exports, dependency roots, and inline comments β not hand-reviewed for every path.
π§ Inline summary
During streaming, text grows but completed lines are immutable. Caching stringWidth per-line avoids re-measuring hundreds of unchanged lines on every token (~50x reduction in stringWidth calls).
π€ Exports (heuristic)
lineWidth
π₯οΈ Source preview
import { stringWidth } from './stringWidth.js'
// During streaming, text grows but completed lines are immutable.
// Caching stringWidth per-line avoids re-measuring hundreds of
// unchanged lines on every token (~50x reduction in stringWidth calls).
const cache = new Map<string, number>()
const MAX_CACHE_SIZE = 4096
export function lineWidth(line: string): number {
const cached = cache.get(line)
if (cached !== undefined) return cached
const width = stringWidth(line)
// Evict when cache grows too large (e.g. after many different responses).
// Simple full-clear is fine β the cache repopulates in one frame.
if (cache.size >= MAX_CACHE_SIZE) {
cache.clear()
}
cache.set(line, width)
return width
}