Skip to content

Commit d4089db

Browse files
committed
Make Input constructors associated methods
1 parent a184f92 commit d4089db

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/unix_term.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,28 @@ enum Input<T> {
7070
File(T),
7171
}
7272

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+
})
8379
}
8480
}
8581

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+
}
9195
}
9296

9397
// 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>> {
119123
}
120124

121125
pub(crate) fn read_secure() -> io::Result<String> {
122-
let mut input = buffered_input()?;
126+
let mut input = Input::buffered()?;
123127

124128
let mut termios = mem::MaybeUninit::uninit();
125129
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> {
332336
}
333337

334338
pub(crate) fn read_single_key(ctrlc_key: bool) -> io::Result<Key> {
335-
let input = unbuffered_input()?;
339+
let input = Input::unbuffered()?;
336340

337341
let mut termios = core::mem::MaybeUninit::uninit();
338342
c_result(|| unsafe { libc::tcgetattr(input.as_raw_fd(), termios.as_mut_ptr()) })?;

0 commit comments

Comments
 (0)