Skip to content

Commit

Permalink
Simplify input getting
Browse files Browse the repository at this point in the history
  • Loading branch information
omekina committed Nov 26, 2023
1 parent 7834294 commit 7ac42e2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/config/maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ pub fn init(filepath: &String, arguments: &Vec<String>) -> bool {
// Get information from user
let user_input = match io::input::read_items(
vec![
vec!["SMTP server hostname", ""],
vec!["Your username", ""],
vec!["Your password", "pass"]
io::input::InputItem::Normal("SMTP server hostname"),
io::input::InputItem::Normal("Your username"),
io::input::InputItem::Hidden("Your password"),
]
) {
Some(value) => value,
Expand Down
44 changes: 28 additions & 16 deletions src/io/input.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
extern crate rpassword;



use rpassword::read_password;
use std::io::{stdin, stdout, Write};

pub fn read_items(prompts: Vec<Vec<&str>>) -> Option<Vec<String>> {

/**
Input item for command line inputs with prompts.
*/
pub enum InputItem {
Normal(&'static str),
Hidden(&'static str),
}


/**
Read specified items to the command line.
*/
pub fn read_items(prompts: Vec<InputItem>) -> Option<Vec<String>> {
let mut result = Vec::new();
for prompt in prompts {
let mut line = String::new();

if prompt[1] == "pass" {
print!("{} (will be hidden): ", prompt[0]);
stdout().flush().unwrap();
for current_input in prompts {
let mut line = String::new();

line = read_password().unwrap();
} else {
print!("{}: ", prompt[0]);
stdout().flush().unwrap();
print!("{}: ", match current_input {
InputItem::Normal(value) => value,
InputItem::Hidden(value) => value,
});
stdout().flush().unwrap();

match stdin().read_line(&mut line) {
Ok(_) => {},
Err(_) => return None,
};
}
match current_input {
InputItem::Normal(_) => { let _ = stdin().read_line(&mut line).ok()?; },
InputItem::Hidden(_) => line = read_password().ok()?,
};

result.push(line);
}

return Some(result);
}

0 comments on commit 7ac42e2

Please sign in to comment.