@@ -89,38 +89,59 @@ public function handleData($data)
8989 }
9090
9191 // ESC is now at start of buffer
92-
9392 // check following byte to determine type
9493 if (!isset ($ this ->buffer [1 ])) {
9594 // type currently unknown, wait for next data chunk
9695 break ;
9796 }
9897
98+ $ found = false ;
99+
99100 if ($ this ->buffer [1 ] === '[ ' ) {
100101 // followed by "[" means it's CSI
101- } else {
102- $ data = substr ($ this ->buffer , 0 , 2 );
103- $ this ->buffer = (string )substr ($ this ->buffer , 2 );
104102
105- $ this -> emit ( ' data ' , array ( $ data ));
106- continue ;
107- }
103+ // CSI is now at the start of the buffer, search final character
104+ for ( $ i = 2 ; isset ( $ this -> buffer [ $ i ]); ++ $ i ) {
105+ $ code = ord ( $ this -> buffer [ $ i ]);
108106
107+ // final character between \x40-\x7E
108+ if ($ code >= 64 && $ code <= 126 ) {
109+ $ data = substr ($ this ->buffer , 0 , $ i + 1 );
110+ $ this ->buffer = (string )substr ($ this ->buffer , $ i + 1 );
109111
110- // CSI is now at the start of the buffer, search final character
111- $ found = false ;
112- for ($ i = 2 ; isset ($ this ->buffer [$ i ]); ++$ i ) {
113- $ code = ord ($ this ->buffer [$ i ]);
112+ $ this ->emit ('csi ' , array ($ data ));
113+ $ found = true ;
114+ break ;
115+ }
116+ }
117+ } elseif ($ this ->buffer [1 ] === '] ' ) {
118+ // followed by "]" means it's OSC (Operating System Controls)
119+
120+ // terminated by ST or BEL (whichever comes first)
121+ $ st = strpos ($ this ->buffer , "\x1B\\" );
122+ $ bel = strpos ($ this ->buffer , "\x07" );
114123
115- // final character between \x40-x7E
116- if ( $ code >= 64 && $ code <= 126 ) {
117- $ data = substr ($ this ->buffer , 0 , $ i + 1 );
118- $ this ->buffer = (string )substr ($ this ->buffer , $ i + 1 );
124+ if ( $ st !== false && ( $ bel === false || $ bel > $ st )) {
125+ // ST comes before BEL or no BEL found
126+ $ data = substr ($ this ->buffer , 0 , $ st + 2 );
127+ $ this ->buffer = (string )substr ($ this ->buffer , $ st + 2 );
119128
120- $ this ->emit ('csi ' , array ($ data ));
129+ $ this ->emit ('osc ' , array ($ data ));
130+ $ found = true ;
131+ } elseif ($ bel !== false ) {
132+ // BEL comes before ST or no ST found
133+ $ data = substr ($ this ->buffer , 0 , $ bel + 1 );
134+ $ this ->buffer = (string )substr ($ this ->buffer , $ bel + 1 );
135+
136+ $ this ->emit ('osc ' , array ($ data ));
121137 $ found = true ;
122- break ;
123138 }
139+ } else {
140+ $ data = substr ($ this ->buffer , 0 , 2 );
141+ $ this ->buffer = (string )substr ($ this ->buffer , 2 );
142+
143+ $ this ->emit ('data ' , array ($ data ));
144+ continue ;
124145 }
125146
126147 // no final character found => wait for next data chunk
0 commit comments