File size: 1,444 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /**
* ESC Sequence Parser
*
* Handles simple escape sequences: ESC + one or two characters
*/
import type { Action } from './types.js'
/**
* Parse a simple ESC sequence
*
* @param chars - Characters after ESC (not including ESC itself)
*/
export function parseEsc(chars: string): Action | null {
if (chars.length === 0) return null
const first = chars[0]!
// Full reset (RIS)
if (first === 'c') {
return { type: 'reset' }
}
// Cursor save (DECSC)
if (first === '7') {
return { type: 'cursor', action: { type: 'save' } }
}
// Cursor restore (DECRC)
if (first === '8') {
return { type: 'cursor', action: { type: 'restore' } }
}
// Index - move cursor down (IND)
if (first === 'D') {
return {
type: 'cursor',
action: { type: 'move', direction: 'down', count: 1 },
}
}
// Reverse index - move cursor up (RI)
if (first === 'M') {
return {
type: 'cursor',
action: { type: 'move', direction: 'up', count: 1 },
}
}
// Next line (NEL)
if (first === 'E') {
return { type: 'cursor', action: { type: 'nextLine', count: 1 } }
}
// Horizontal tab set (HTS)
if (first === 'H') {
return null // Tab stop, not commonly needed
}
// Charset selection (ESC ( X, ESC ) X, etc.) - silently ignore
if ('()'.includes(first) && chars.length >= 2) {
return null
}
// Unknown
return { type: 'unknown', sequence: `\x1b${chars}` }
}
|