MCPcopy Index your code
hub / github.com/codeaashu/claude-code / parseCSI

Function parseCSI

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

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

Callers 1

processSequenceMethod · 0.85

Calls 1

parseCSIParamsFunction · 0.85

Tested by

no test coverage detected