cli_input/
lib.rs

1mod macros {
2    #![allow(unused_macros)]
3    #![allow(unused_imports)]
4    #![allow(dead_code)]
5    
6    #[macro_export]
7    /// Prompt the user for some input.
8    ///
9    /// # Examples
10    /// ```rust,ignore
11    /// use cli_input::prelude::*;
12    ///
13    /// let name: String = input!("What is your name?: ");
14    /// ```
15    macro_rules! input {
16        ($($arg:tt)*) => {
17            {
18                input(&format!($($arg)*))
19            }
20        };
21    }
22    
23    #[macro_export]
24    /// Prompt the user for some input, and hide the characters being typed out.
25    ///
26    /// # Examples
27    /// ```rust,ignore
28    /// use cli_input::prelude::*;
29    ///
30    /// let password: String = input_hidden!("What is your password?: ");
31    /// ```
32    macro_rules! input_hidden {
33        ($($arg:tt)*) => {
34            {
35                input_hidden(&format!($($arg)*))
36            }
37        };
38    }
39    
40    pub use input;
41    pub use input_hidden;
42}
43
44mod functions {
45    use std::io::Write;
46    use rpassword;
47
48    pub fn input(prefix: &str) -> String {
49        let mut prompt = String::new();
50    
51        print!("{}", prefix);
52    
53        std::io::stdout().flush().unwrap();
54        std::io::stdin().read_line(&mut prompt).unwrap();
55    
56        return prompt.trim().to_string();
57    }
58
59    pub fn input_hidden(prefix: &str) -> String {
60        let prompt = rpassword::prompt_password(prefix).unwrap();
61    
62        return prompt.trim().to_string();
63    }
64}
65
66pub mod prelude {
67    pub use super::macros::*;
68    pub use super::functions::*;
69}