MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / writeLineToScreen

Function writeLineToScreen

source code/ink/output.ts:635–799  ·  view source on GitHub ↗

* Write a single line's characters into the screen buffer. * Extracted from Output.get() so JSC can optimize this tight, * monomorphic loop independently — better register allocation, * setCellAt inlining, and type feedback than when buried inside * a 300-line dispatch function. * * Returns th

(
  screen: Screen,
  line: string,
  x: number,
  y: number,
  screenWidth: number,
  stylePool: StylePool,
  charCache: Map<string, ClusteredChar[]>,
)

Source from the content-addressed store, hash-verified

633 * line via stringWidth(). Caller computes the debug cell-count as end-x.
634 */
635function writeLineToScreen(
636 screen: Screen,
637 line: string,
638 x: number,
639 y: number,
640 screenWidth: number,
641 stylePool: StylePool,
642 charCache: Map<string, ClusteredChar[]>,
643): number {
644 let characters = charCache.get(line)
645 if (!characters) {
646 characters = reorderBidi(
647 styledCharsWithGraphemeClustering(
648 styledCharsFromTokens(tokenize(line)),
649 stylePool,
650 ),
651 )
652 charCache.set(line, characters)
653 }
654
655 let offsetX = x
656
657 for (let charIdx = 0; charIdx < characters.length; charIdx++) {
658 const character = characters[charIdx]!
659 const codePoint = character.value.codePointAt(0)
660
661 // Handle C0 control characters (0x00-0x1F) that cause cursor movement
662 // mismatches. stringWidth treats these as width 0, but terminals may
663 // move the cursor differently.
664 if (codePoint !== undefined && codePoint <= 0x1f) {
665 // Tab (0x09): expand to spaces to reach next tab stop
666 if (codePoint === 0x09) {
667 const tabWidth = 8
668 const spacesToNextStop = tabWidth - (offsetX % tabWidth)
669 for (let i = 0; i < spacesToNextStop && offsetX < screenWidth; i++) {
670 setCellAt(screen, offsetX, y, {
671 char: ' ',
672 styleId: stylePool.none,
673 width: CellWidth.Narrow,
674 hyperlink: undefined,
675 })
676 offsetX++
677 }
678 }
679 // ESC (0x1B): skip incomplete escape sequences that ansi-tokenize
680 // didn't recognize. ansi-tokenize only parses SGR sequences (ESC[...m)
681 // and OSC 8 hyperlinks (ESC]8;;url BEL). Other sequences like cursor
682 // movement, screen clearing, or terminal title become individual char
683 // tokens that we need to skip here.
684 else if (codePoint === 0x1b) {
685 const nextChar = characters[charIdx + 1]?.value
686 const nextCode = nextChar?.codePointAt(0)
687 if (
688 nextChar === '(' ||
689 nextChar === ')' ||
690 nextChar === '*' ||
691 nextChar === '+'
692 ) {

Callers 1

getMethod · 0.85

Calls 6

reorderBidiFunction · 0.85
setCellAtFunction · 0.85
setMethod · 0.80
tokenizeFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected