Skip to content

Commit

Permalink
input support default
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett committed Mar 17, 2019
1 parent 4536602 commit bc3d46a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,18 @@ struct Args {
arg_addons: String,
}

fn input(text: &str) -> String {
fn input(text: &str, default: Option<String>) -> String {
let mut s = String::new();
print!("{}: ",text);
let ret = match default {
Some(v) => {
print!("{} ({}): ", text, &v);
v
}
None => {
print!("{}: ", text);
String::new()
}
};
stdout().flush().unwrap();
stdin().read_line(&mut s).expect("Did not enter a valid string");
if let Some('\n')=s.chars().next_back() {
Expand All @@ -114,6 +123,9 @@ fn input(text: &str) -> String {
if let Some('\r')=s.chars().next_back() {
s.pop();
}
if s == "".to_owned() {
return ret;
}
s
}

Expand Down Expand Up @@ -358,9 +370,9 @@ fn check(write: bool, force: bool) -> Result<(), Error> {
}

fn init() -> Result<crate::project::Project, Error> {
let name = input("Project Name (My Cool Mod)");
let prefix = input("Prefix (MCM)");
let author = input("Author");
let name = input("Project Name", Some("My Cool Mod".to_owned()));
let prefix = input("Prefix", Some("MCM".to_owned()));
let author = input("Author", Some("Me".to_owned()));
Ok(crate::project::init(name, prefix, author)?)
}

Expand Down

0 comments on commit bc3d46a

Please sign in to comment.