πŸ“„ File detail

ink/line-width-cache.ts

🧩 .tsπŸ“ 25 linesπŸ’Ύ 734 bytesπŸ“ text
← Back to All Files

🎯 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
}