MCPcopy Create free account
hub / github.com/Noumena-Network/code / parseCSI

Function parseCSI

src/ink/termio/parser.ts:133–286  ·  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

131
132/** Parse a raw CSI sequence (e.g., "\x1b[31m") into an action */
133function parseCSI(rawSequence: string): Action | null {
134 const inner = rawSequence.slice(2)
135 if (inner.length === 0) return null
136
137 const finalByte = inner.charCodeAt(inner.length - 1)
138 const beforeFinal = inner.slice(0, -1)
139
140 let privateMode = ''
141 let paramStr = beforeFinal
142 let intermediate = ''
143
144 if (beforeFinal.length > 0 && '?>='.includes(beforeFinal[0]!)) {
145 privateMode = beforeFinal[0]!
146 paramStr = beforeFinal.slice(1)
147 }
148
149 const intermediateMatch = paramStr.match(/([^0-9;:]+)$/)
150 if (intermediateMatch) {
151 intermediate = intermediateMatch[1]!
152 paramStr = paramStr.slice(0, -intermediate.length)
153 }
154
155 const params = parseCSIParams(paramStr)
156 const p0 = params[0] ?? 1
157 const p1 = params[1] ?? 1
158
159 // SGR (Select Graphic Rendition)
160 if (finalByte === CSI.SGR && privateMode === '') {
161 return { type: 'sgr', params: paramStr }
162 }
163
164 // Cursor movement
165 if (finalByte === CSI.CUU) {
166 return {
167 type: 'cursor',
168 action: { type: 'move', direction: 'up', count: p0 },
169 }
170 }
171 if (finalByte === CSI.CUD) {
172 return {
173 type: 'cursor',
174 action: { type: 'move', direction: 'down', count: p0 },
175 }
176 }
177 if (finalByte === CSI.CUF) {
178 return {
179 type: 'cursor',
180 action: { type: 'move', direction: 'forward', count: p0 },
181 }
182 }
183 if (finalByte === CSI.CUB) {
184 return {
185 type: 'cursor',
186 action: { type: 'move', direction: 'back', count: p0 },
187 }
188 }
189 if (finalByte === CSI.CNL) {
190 return { type: 'cursor', action: { type: 'nextLine', count: p0 } }

Callers 1

processSequenceMethod · 0.85

Calls 1

parseCSIParamsFunction · 0.85

Tested by

no test coverage detected