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

Function wrapText

src/native-ts/color-diff/index.ts:665–726  ·  view source on GitHub ↗
(h: Highlight, width: number, theme: Theme)

Source from the content-addressed store, hash-verified

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