@@ -202,28 +202,8 @@ fn read_bytes(fd: i32, buf: &mut [u8], count: u8) -> io::Result<u8> {
202
202
}
203
203
}
204
204
205
- pub fn read_single_key ( ) -> io:: Result < Key > {
206
- let tty_f;
207
- let fd = unsafe {
208
- if libc:: isatty ( libc:: STDIN_FILENO ) == 1 {
209
- libc:: STDIN_FILENO
210
- } else {
211
- tty_f = fs:: OpenOptions :: new ( )
212
- . read ( true )
213
- . write ( true )
214
- . open ( "/dev/tty" ) ?;
215
- tty_f. as_raw_fd ( )
216
- }
217
- } ;
218
- let mut termios = core:: mem:: MaybeUninit :: uninit ( ) ;
219
- c_result ( || unsafe { libc:: tcgetattr ( fd, termios. as_mut_ptr ( ) ) } ) ?;
220
- let mut termios = unsafe { termios. assume_init ( ) } ;
221
- let original = termios;
222
- unsafe { libc:: cfmakeraw ( & mut termios) } ;
223
- termios. c_oflag = original. c_oflag ;
224
- c_result ( || unsafe { libc:: tcsetattr ( fd, libc:: TCSADRAIN , & termios) } ) ?;
225
-
226
- let rv: io:: Result < Key > = loop {
205
+ fn read_single_key_impl ( fd : i32 ) -> Result < Key , io:: Error > {
206
+ loop {
227
207
match read_single_char ( fd) ? {
228
208
Some ( '\x1b' ) => {
229
209
// Escape was read, keep reading in case we find a familiar key
@@ -312,8 +292,30 @@ pub fn read_single_key() -> io::Result<Key> {
312
292
}
313
293
}
314
294
}
315
- } ;
295
+ }
296
+ }
316
297
298
+ pub fn read_single_key ( ) -> io:: Result < Key > {
299
+ let tty_f;
300
+ let fd = unsafe {
301
+ if libc:: isatty ( libc:: STDIN_FILENO ) == 1 {
302
+ libc:: STDIN_FILENO
303
+ } else {
304
+ tty_f = fs:: OpenOptions :: new ( )
305
+ . read ( true )
306
+ . write ( true )
307
+ . open ( "/dev/tty" ) ?;
308
+ tty_f. as_raw_fd ( )
309
+ }
310
+ } ;
311
+ let mut termios = core:: mem:: MaybeUninit :: uninit ( ) ;
312
+ c_result ( || unsafe { libc:: tcgetattr ( fd, termios. as_mut_ptr ( ) ) } ) ?;
313
+ let mut termios = unsafe { termios. assume_init ( ) } ;
314
+ let original = termios;
315
+ unsafe { libc:: cfmakeraw ( & mut termios) } ;
316
+ termios. c_oflag = original. c_oflag ;
317
+ c_result ( || unsafe { libc:: tcsetattr ( fd, libc:: TCSADRAIN , & termios) } ) ?;
318
+ let rv: io:: Result < Key > = read_single_key_impl ( fd) ;
317
319
c_result ( || unsafe { libc:: tcsetattr ( fd, libc:: TCSADRAIN , & original) } ) ?;
318
320
319
321
// if the user hit ^C we want to signal SIGINT to outselves.
0 commit comments