Refactored to remove code duplication. Related to #32.

This commit is contained in:
scoobybejesus 2019-09-01 20:10:23 -04:00
parent cf400799bb
commit 486372af15
1 changed files with 17 additions and 21 deletions

View File

@ -22,12 +22,9 @@ pub fn choose_file_for_import() -> Result<PathBuf, Box<dyn Error>> {
println!("Please input a file (absolute or relative path) to import: "); println!("Please input a file (absolute or relative path) to import: ");
let file_string = _get_path()?; let (file_string, has_tilde) = _get_path()?;
let has_tilde = begins_with_tilde(&file_string);
if has_tilde { if has_tilde {
println!("Unfortunately, the tilde '~' cannot be used as a shortcut for your home directory.\n");
choose_file_for_import() choose_file_for_import()
} else { } else {
Ok( PathBuf::from(file_string) ) Ok( PathBuf::from(file_string) )
@ -38,27 +35,16 @@ pub fn choose_export_dir() -> Result<PathBuf, Box<dyn Error>> {
println!("Please input a file path for exports: "); println!("Please input a file path for exports: ");
let file_string = _get_path()?; let (file_string, has_tilde) = _get_path()?;
let has_tilde = begins_with_tilde(&file_string);
if has_tilde { if has_tilde {
println!("Unfortunately, the tilde '~' cannot be used as a shortcut for your home directory.\n");
choose_export_dir() choose_export_dir()
} else { } else {
Ok( PathBuf::from(file_string) ) Ok( PathBuf::from(file_string) )
} }
} }
pub fn begins_with_tilde(unchecked_path: &String) -> bool { fn _get_path() -> Result<(String, bool), Box<dyn Error>> {
match unchecked_path.find("~") {
Some(0) => return true,
_ => return false
}
}
fn _get_path() -> Result<(String), Box<dyn Error>> {
struct MyHelper { struct MyHelper {
completer: FilenameCompleter, completer: FilenameCompleter,
@ -102,11 +88,21 @@ fn _get_path() -> Result<(String), Box<dyn Error>> {
rl.helper_mut().unwrap().colored_prompt = format!("\x1b[1;32m{}\x1b[0m", p); rl.helper_mut().unwrap().colored_prompt = format!("\x1b[1;32m{}\x1b[0m", p);
let readline = rl.readline(">> "); let readline = rl.readline(">> ");
fn begins_with_tilde(unchecked_path: &String) -> bool {
match unchecked_path.find("~") {
Some(0) => return true,
_ => return false
}
}
match readline { match readline {
Ok(line) => { Ok(line) => {
rl.add_history_entry(line.as_str());
println!(""); println!("");
Ok(line) let has_tilde = begins_with_tilde(&line);
if has_tilde {
println!("Unfortunately, the tilde '~' cannot be used as a shortcut for your home directory.\n");
}
Ok((line, has_tilde))
}, },
Err(err) => { Err(err) => {
println!("Error during Rustyline: {:?}", err); println!("Error during Rustyline: {:?}", err);