@@ -47,6 +47,8 @@ impl Emulator {
4747 ) ?;
4848
4949 let resp = futures:: executor:: block_on ( Self :: read_until_da1 ( ) ) ;
50+ Mux :: tmux_drain ( ) ?;
51+
5052 let kind = if let Some ( brand) = Brand :: from_csi ( & resp) {
5153 Either :: Left ( brand)
5254 } else {
@@ -130,6 +132,31 @@ impl Emulator {
130132 String :: from_utf8_lossy ( & buf) . into_owned ( )
131133 }
132134
135+ pub async fn read_until_dsr ( ) -> String {
136+ let mut buf: Vec < u8 > = Vec :: with_capacity ( 200 ) ;
137+ let read = async {
138+ let mut stdin = BufReader :: new ( tokio:: io:: stdin ( ) ) ;
139+ loop {
140+ let mut c = [ 0 ; 1 ] ;
141+ if stdin. read ( & mut c) . await ? == 0 {
142+ bail ! ( "unexpected EOF" ) ;
143+ }
144+ buf. push ( c[ 0 ] ) ;
145+ if c[ 0 ] == b'n' && ( buf. ends_with ( b"\x1b [0n" ) || buf. ends_with ( b"\x1b [3n" ) ) {
146+ break ;
147+ }
148+ }
149+ Ok ( ( ) )
150+ } ;
151+
152+ match timeout ( Duration :: from_secs ( 10 ) , read) . await {
153+ Err ( e) => error ! ( "read_until_dsr timed out: {buf:?}, error: {e:?}" ) ,
154+ Ok ( Err ( e) ) => error ! ( "read_until_dsr failed: {buf:?}, error: {e:?}" ) ,
155+ Ok ( Ok ( ( ) ) ) => debug ! ( "read_until_dsr: {buf:?}" ) ,
156+ }
157+ String :: from_utf8_lossy ( & buf) . into_owned ( )
158+ }
159+
133160 fn detect_base ( ) -> Result < Self > {
134161 defer ! { disable_raw_mode( ) . ok( ) ; }
135162 enable_raw_mode ( ) ?;
@@ -142,6 +169,8 @@ impl Emulator {
142169 ) ?;
143170
144171 let resp = futures:: executor:: block_on ( Self :: read_until_da1 ( ) ) ;
172+ Mux :: tmux_drain ( ) ?;
173+
145174 Ok ( Self {
146175 light : Self :: light_bg ( & resp) . unwrap_or_default ( ) ,
147176 cell_size : Self :: cell_size ( & resp) ,
0 commit comments