Skip to content

Commit 076c4fa

Browse files
committed
Add default answer param
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
1 parent 09ca37e commit 076c4fa

3 files changed

Lines changed: 39 additions & 10 deletions

File tree

api.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ impl<T> core::borrow::BorrowMut<T> for ask_cli::Answer where T: core::marker::Si
4141
pub fn ask_cli::Answer::borrow_mut(&mut self) -> &mut T
4242
impl<T> core::convert::From<T> for ask_cli::Answer
4343
pub fn ask_cli::Answer::from(t: T) -> T
44-
pub fn ask_cli::ask<Q: core::convert::AsRef<[u8]>, In: std::io::Read, Out: std::io::Write>(question: Q, stdin: &mut In, stdout: &mut Out) -> core::result::Result<ask_cli::Answer, std::io::error::Error>
44+
pub fn ask_cli::ask<Q: core::convert::AsRef<[u8]>, In: std::io::Read, Out: std::io::Write>(question: Q, default: ask_cli::Answer, stdin: &mut In, stdout: &mut Out) -> core::result::Result<ask_cli::Answer, std::io::error::Error>

src/lib.rs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,57 @@ enum State {
6262
/// Ask the user a yes or no question on stdout, reading the reply from stdin.
6363
///
6464
/// Replies are delimited by newlines of any kind and must be one of '' (maps to
65-
/// yes), 'y', 'yes', 'n', 'no', case insensitive. If the reply fails to parse,
65+
/// yes), 'y', 'yes', 'n', 'no', case-insensitive. If the reply fails to parse,
6666
/// the question will be asked again ad infinitum.
6767
///
6868
/// # Examples
6969
///
7070
/// ```
71-
/// # use std::{io, io::Read, str::from_utf8};
71+
/// # use std::{io, str::from_utf8};
7272
/// use ask_cli::{ask, Answer};
7373
///
7474
/// assert!(matches!(
75-
/// ask("Continue? [Y/n] ", &mut "y\n".as_bytes(), &mut io::sink()),
75+
/// ask(
76+
/// "Continue? [Y/n] ",
77+
/// Answer::Yes,
78+
/// &mut "y\n".as_bytes(),
79+
/// &mut io::sink()
80+
/// ),
7681
/// Ok(Answer::Yes)
7782
/// ));
7883
/// assert!(matches!(
79-
/// ask("Continue? [Y/n] ", &mut "n\n".as_bytes(), &mut io::sink()),
84+
/// ask(
85+
/// "Continue? [Y/n] ",
86+
/// Answer::Yes,
87+
/// &mut "n\n".as_bytes(),
88+
/// &mut io::sink()
89+
/// ),
8090
/// Ok(Answer::No)
8191
/// ));
8292
/// assert!(matches!(
83-
/// ask("Continue? [Y/n] ", &mut "".as_bytes(), &mut io::sink()),
93+
/// ask(
94+
/// "Continue? [Y/n] ",
95+
/// Answer::Yes,
96+
/// &mut "".as_bytes(),
97+
/// &mut io::sink()
98+
/// ),
8499
/// Ok(Answer::Unknown)
85100
/// ));
101+
/// assert!(matches!(
102+
/// ask(
103+
/// "Continue? [y/N] ",
104+
/// Answer::No,
105+
/// &mut "\n".as_bytes(),
106+
/// &mut io::sink()
107+
/// ),
108+
/// Ok(Answer::No)
109+
/// ));
86110
///
87111
/// // Here we use 3 different kinds of line endings
88112
/// let mut stdout = Vec::new();
89-
/// ask(
113+
/// let answer = ask(
90114
/// "Continue? [Y/n] ",
115+
/// Answer::Yes,
91116
/// &mut "a\nb\rc\r\nyes\n".as_bytes(),
92117
/// &mut stdout,
93118
/// )
@@ -96,13 +121,15 @@ enum State {
96121
/// "Continue? [Y/n] Continue? [Y/n] Continue? [Y/n] Continue? [Y/n] ",
97122
/// from_utf8(&stdout).unwrap()
98123
/// );
124+
/// assert!(matches!(answer, Answer::Yes));
99125
/// ```
100126
///
101127
/// # Errors
102128
///
103129
/// Underlying I/O errors are bubbled up.
104130
pub fn ask<Q: AsRef<[u8]>, In: Read, Out: Write>(
105131
question: Q,
132+
default: Answer,
106133
stdin: &mut In,
107134
stdout: &mut Out,
108135
) -> Result<Answer, io::Error> {
@@ -197,7 +224,8 @@ pub fn ask<Q: AsRef<[u8]>, In: Read, Out: Write>(
197224
let reply = &mut buf.filled_mut()[..newline_index];
198225
reply.make_ascii_lowercase();
199226
match &*reply {
200-
b"" | b"y" | b"yes" => return Ok(Answer::Yes),
227+
b"" => return Ok(default),
228+
b"y" | b"yes" => return Ok(Answer::Yes),
201229
b"n" | b"no" => return Ok(Answer::No),
202230
_ => {
203231
consume_newline!(newline_index);

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{env, ffi::OsString, process::Termination};
22

3-
use ask_cli::ask;
3+
use ask_cli::{ask, Answer};
44

55
fn main() -> impl Termination {
66
let mut question = OsString::new();
@@ -17,7 +17,7 @@ fn main() -> impl Termination {
1717

1818
let mut stdin = ManuallyDrop::new(unsafe { File::from_raw_fd(0) });
1919
let mut stdout = ManuallyDrop::new(unsafe { File::from_raw_fd(1) });
20-
ask(question.as_bytes(), &mut *stdin, &mut *stdout)
20+
ask(question.as_bytes(), Answer::Yes, &mut *stdin, &mut *stdout)
2121
}
2222
#[cfg(not(unix))]
2323
{
@@ -27,6 +27,7 @@ fn main() -> impl Termination {
2727
let mut stdout = io::stdout().lock();
2828
ask(
2929
question.to_string_lossy().as_bytes(),
30+
Answer::Yes,
3031
&mut stdin,
3132
&mut stdout,
3233
)

0 commit comments

Comments
 (0)