MCPcopy Create free account
hub / github.com/WJX20/claude-code / wrapText

Function wrapText

packages/color-diff-napi/src/index.ts:670–731  ·  view source on GitHub ↗
(h: Highlight, width: number, theme: Theme)

Source from the content-addressed store, hash-verified

668}
669
670function wrapText(h: Highlight, width: number, theme: Theme): void {
671 const newLines: Block[][] = []
672 for (const line of h.lines) {
673 const queue: Block[] = line.slice()
674 let cur: Block[] = []
675 let curW = 0
676 while (queue.length > 0) {
677 const [style, text] = queue.shift()!
678 const tw = stringWidth(text)
679 if (curW + tw <= width) {
680 cur.push([style, text])
681 curW += tw
682 } else {
683 const remaining = width - curW
684 let bytePos = 0
685 let accW = 0
686 // iterate by codepoint
687 for (const ch of text) {
688 const cw = charWidth(ch)
689 if (accW + cw > remaining) break
690 accW += cw
691 bytePos += ch.length
692 }
693 if (bytePos === 0) {
694 if (curW === 0) {
695 // Fresh line and first char still doesn't fit — force one codepoint
696 // to guarantee forward progress (overflows, but prevents infinite loop)
697 const firstCp = text.codePointAt(0)!
698 bytePos = firstCp > 0xffff ? 2 : 1
699 } else {
700 // Line has content and next char doesn't fit — finish this line,
701 // re-queue the whole block for a fresh line
702 newLines.push(cur)
703 queue.unshift([style, text])
704 cur = []
705 curW = 0
706 continue
707 }
708 }
709 cur.push([style, text.slice(0, bytePos)])
710 newLines.push(cur)
711 queue.unshift([style, text.slice(bytePos)])
712 cur = []
713 curW = 0
714 }
715 }
716 newLines.push(cur)
717 }
718 h.lines = newLines
719
720 // Pad changed lines so background extends to edge
721 if (h.marker && h.marker !== ' ') {
722 const bg = lineBackground(h.marker, theme)
723 const padStyle: Style = { foreground: theme.foreground, background: bg }
724 for (const line of h.lines) {
725 const curW = line.reduce((s, [, t]) => s + stringWidth(t), 0)
726 if (curW < width) {
727 line.push([padStyle, ' '.repeat(width - curW)])

Callers 2

renderMethod · 0.70
renderMethod · 0.70

Calls 3

charWidthFunction · 0.85
lineBackgroundFunction · 0.85
shiftMethod · 0.80

Tested by

no test coverage detected