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

Function parseCSI

source code/ink/termio/parser.ts:89–242  ·  view source on GitHub ↗

Parse a raw CSI sequence (e.g., "\x1b[31m") into an action

(rawSequence: string)

Source from the content-addressed store, hash-verified

87
88/** Parse a raw CSI sequence (e.g., "\x1b[31m") into an action */
89function parseCSI(rawSequence: string): Action | null {
90 const inner = rawSequence.slice(2)
91 if (inner.length === 0) return null
92
93 const finalByte = inner.charCodeAt(inner.length - 1)
94 const beforeFinal = inner.slice(0, -1)
95
96 let privateMode = ''
97 let paramStr = beforeFinal
98 let intermediate = ''
99
100 if (beforeFinal.length > 0 && '?>='.includes(beforeFinal[0]!)) {
101 privateMode = beforeFinal[0]!
102 paramStr = beforeFinal.slice(1)
103 }
104
105 const intermediateMatch = paramStr.match(/([^0-9;:]+)$/)
106 if (intermediateMatch) {
107 intermediate = intermediateMatch[1]!
108 paramStr = paramStr.slice(0, -intermediate.length)
109 }
110
111 const params = parseCSIParams(paramStr)
112 const p0 = params[0] ?? 1
113 const p1 = params[1] ?? 1
114
115 // SGR (Select Graphic Rendition)
116 if (finalByte === CSI.SGR && privateMode === '') {
117 return { type: 'sgr', params: paramStr }
118 }
119
120 // Cursor movement
121 if (finalByte === CSI.CUU) {
122 return {
123 type: 'cursor',
124 action: { type: 'move', direction: 'up', count: p0 },
125 }
126 }
127 if (finalByte === CSI.CUD) {
128 return {
129 type: 'cursor',
130 action: { type: 'move', direction: 'down', count: p0 },
131 }
132 }
133 if (finalByte === CSI.CUF) {
134 return {
135 type: 'cursor',
136 action: { type: 'move', direction: 'forward', count: p0 },
137 }
138 }
139 if (finalByte === CSI.CUB) {
140 return {
141 type: 'cursor',
142 action: { type: 'move', direction: 'back', count: p0 },
143 }
144 }
145 if (finalByte === CSI.CNL) {
146 return { type: 'cursor', action: { type: 'nextLine', count: p0 } }

Callers 1

processSequenceMethod · 0.85

Calls 1

parseCSIParamsFunction · 0.85

Tested by

no test coverage detected