MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / extractCompleteSequences

Function extractCompleteSequences

packages/pi-tui/src/stdin-buffer.ts:192–255  ·  view source on GitHub ↗
(buffer: string)

Source from the content-addressed store, hash-verified

190}
191
192function extractCompleteSequences(buffer: string): { sequences: string[]; remainder: string } {
193 const sequences: string[] = [];
194 let pos = 0;
195
196 while (pos < buffer.length) {
197 const remaining = buffer.slice(pos);
198
199 // Try to extract a sequence starting at this position
200 if (remaining.startsWith(ESC)) {
201 // Find the end of this escape sequence
202 let seqEnd = 1;
203 while (seqEnd <= remaining.length) {
204 const candidate = remaining.slice(0, seqEnd);
205 const status = isCompleteSequence(candidate);
206
207 if (status === "complete") {
208 // WezTerm with enable_kitty_keyboard sends the Escape key press as a
209 // raw '\x1b' byte (simple text path in encode_kitty, ignoring
210 // DISAMBIGUATE_ESCAPE_CODES) and the release as a full Kitty CSI-u
211 // sequence. These arrive concatenated as '\x1b\x1b[27;...u'.
212 // The buffer would normally treat '\x1b\x1b' as a complete meta-key
213 // sequence (ESC + single char), leaving '[27;...u' to be typed as
214 // plain text. If the character immediately following '\x1b\x1b'
215 // would begin a new escape sequence, emit only the first ESC and
216 // restart from the second.
217 if (candidate === "\x1b\x1b") {
218 const nextChar = remaining[seqEnd];
219 if (
220 nextChar === "[" || // CSI
221 nextChar === "]" || // OSC
222 nextChar === "O" || // SS3
223 nextChar === "P" || // DCS
224 nextChar === "_" // APC
225 ) {
226 sequences.push(ESC);
227 pos += 1;
228 break;
229 }
230 }
231 sequences.push(candidate);
232 pos += seqEnd;
233 break;
234 } else if (status === "incomplete") {
235 seqEnd++;
236 } else {
237 // Should not happen when starting with ESC
238 sequences.push(candidate);
239 pos += seqEnd;
240 break;
241 }
242 }
243
244 if (seqEnd > remaining.length) {
245 return { sequences, remainder: remaining };
246 }
247 } else {
248 // Not an escape sequence - take a single character
249 sequences.push(remaining[0]!);

Callers 1

processMethod · 0.85

Calls 2

isCompleteSequenceFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected