@@ -70,24 +70,28 @@ enum Input<T> {
70
70
File ( T ) ,
71
71
}
72
72
73
- fn unbuffered_input ( ) -> io:: Result < Input < fs:: File > > {
74
- let stdin = io:: stdin ( ) ;
75
- if is_a_terminal ( & stdin) {
76
- Ok ( Input :: Stdin ( stdin) )
77
- } else {
78
- let f = fs:: OpenOptions :: new ( )
79
- . read ( true )
80
- . write ( true )
81
- . open ( "/dev/tty" ) ?;
82
- Ok ( Input :: File ( f) )
73
+ impl Input < BufReader < fs:: File > > {
74
+ fn buffered ( ) -> io:: Result < Self > {
75
+ Ok ( match Input :: unbuffered ( ) ? {
76
+ Input :: Stdin ( s) => Input :: Stdin ( s) ,
77
+ Input :: File ( f) => Input :: File ( BufReader :: new ( f) ) ,
78
+ } )
83
79
}
84
80
}
85
81
86
- fn buffered_input ( ) -> io:: Result < Input < BufReader < fs:: File > > > {
87
- Ok ( match unbuffered_input ( ) ? {
88
- Input :: Stdin ( s) => Input :: Stdin ( s) ,
89
- Input :: File ( f) => Input :: File ( BufReader :: new ( f) ) ,
90
- } )
82
+ impl Input < fs:: File > {
83
+ fn unbuffered ( ) -> io:: Result < Self > {
84
+ let stdin = io:: stdin ( ) ;
85
+ if is_a_terminal ( & stdin) {
86
+ Ok ( Input :: Stdin ( stdin) )
87
+ } else {
88
+ let f = fs:: OpenOptions :: new ( )
89
+ . read ( true )
90
+ . write ( true )
91
+ . open ( "/dev/tty" ) ?;
92
+ Ok ( Input :: File ( f) )
93
+ }
94
+ }
91
95
}
92
96
93
97
// NB: this is not a full BufRead implementation because io::Stdin does not implement BufRead.
@@ -119,7 +123,7 @@ impl AsRawFd for Input<BufReader<fs::File>> {
119
123
}
120
124
121
125
pub ( crate ) fn read_secure ( ) -> io:: Result < String > {
122
- let mut input = buffered_input ( ) ?;
126
+ let mut input = Input :: buffered ( ) ?;
123
127
124
128
let mut termios = mem:: MaybeUninit :: uninit ( ) ;
125
129
c_result ( || unsafe { libc:: tcgetattr ( input. as_raw_fd ( ) , termios. as_mut_ptr ( ) ) } ) ?;
@@ -332,7 +336,7 @@ fn read_single_key_impl(fd: RawFd) -> Result<Key, io::Error> {
332
336
}
333
337
334
338
pub ( crate ) fn read_single_key ( ctrlc_key : bool ) -> io:: Result < Key > {
335
- let input = unbuffered_input ( ) ?;
339
+ let input = Input :: unbuffered ( ) ?;
336
340
337
341
let mut termios = core:: mem:: MaybeUninit :: uninit ( ) ;
338
342
c_result ( || unsafe { libc:: tcgetattr ( input. as_raw_fd ( ) , termios. as_mut_ptr ( ) ) } ) ?;
0 commit comments