@@ -367,10 +367,11 @@ function applySgr(attributes: Attributes, params: ReadonlyArray<number>): void {
367367}
368368
369369const STRING_SEQUENCE_INTRODUCERS = new Set ( [ "]" , "P" , "X" , "_" , "^" ] ) ;
370+ // C1 DCS, SOS, CSI, OSC, PM and APC, each equal to ESC followed by its code minus 0x40.
371+ const C1_INTRODUCERS = new Set ( [ 0x90 , 0x98 , 0x9b , 0x9d , 0x9e , 0x9f ] ) ;
370372
371373/** Length of a complete escape sequence whose introducer follows `index`, or -1 if unfinished. */
372- function escapeLength ( input : string , index : number ) : number {
373- const next = input [ index + 1 ] ;
374+ function escapeLength ( input : string , index : number , next = input [ index + 1 ] ) : number {
374375 if ( next === undefined ) return - 1 ;
375376 // A second ESC restarts the sequence.
376377 if ( next === ESC ) return 1 ;
@@ -398,7 +399,7 @@ function escapeLength(input: string, index: number): number {
398399 // ST (and BEL for OSC) ends string sequences; also stop at newlines so stray binary introducers cannot hide later output.
399400 for ( let cursor = index + 2 ; cursor < input . length ; cursor += 1 ) {
400401 if ( next === "]" && input [ cursor ] === "\u0007" ) return cursor - index + 1 ;
401- if ( input [ cursor ] === "\u0018" || input [ cursor ] === "\u001a" ) return cursor - index + 1 ;
402+ if ( "\u0018\u001a\u009c" . includes ( input [ cursor ] ! ) ) return cursor - index + 1 ;
402403 if ( input [ cursor ] === "\n" ) return cursor - index ;
403404 // Like xterm.js, an ESC that does not start ST ends the string and begins the next sequence.
404405 if ( input [ cursor ] === ESC ) {
@@ -677,12 +678,18 @@ class Terminal {
677678 }
678679 flush ( index ) ;
679680 printable = index + 1 ;
680- if ( code === 0x1b ) {
681- let start = index + 1 ;
682- while ( this . control ( input . charCodeAt ( start ) ) ) start += 1 ;
683- const length = escapeLength ( input , start - 1 ) ;
681+ const c1 = C1_INTRODUCERS . has ( code ) ? String . fromCharCode ( code - 0x40 ) : undefined ;
682+ if ( code === 0x1b || c1 !== undefined ) {
683+ // A C1 introducer stands in for ESC plus its 7-bit introducer, so parse it as that pair.
684+ let start = c1 === undefined ? index + 1 : index ;
685+ while ( c1 === undefined && this . control ( input . charCodeAt ( start ) ) ) start += 1 ;
686+ const length = escapeLength ( input , start - 1 , c1 ) ;
684687 const end = length === - 1 ? - 1 : start - 1 + length ;
685- let sequence = ESC + input . slice ( start , end === - 1 ? input . length : end ) ;
688+ const rest = input . slice (
689+ c1 === undefined ? start : start + 1 ,
690+ end === - 1 ? input . length : end ,
691+ ) ;
692+ let sequence = ESC + ( c1 ?? "" ) + rest ;
686693 if ( sequence [ 1 ] === "[" ) {
687694 // Apply controls now and leave them out of the CSI, including any pending fragment.
688695 sequence = sequence . replace ( / [ \u0007 \b \n \r ] / gu, ( control ) => {
0 commit comments