diff --git a/main.rs b/main.rs index 7bc1211853ef8d6b7486f8a1b6b6c142d3261701..1cd2c45c8457a376ef6f37fd14ff3beac495322c 100644 --- a/main.rs +++ b/main.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeMap; use std::get_slice::GetSlice; use std::ops::DerefMut; use std::string::*; @@ -31,6 +32,7 @@ static mut application: *mut Application<'static> = 0 as *mut Application; /// ``` /// let my_command = Command { /// name: "my_command", +/// help: "Describe what my_command does followed by a newline showing usage", /// main: box|args: &Vec<String>| { /// println!("Say 'hello' to my command! :-D"); /// } @@ -52,12 +54,7 @@ impl<'a> Command<'a> { name: "cat", help: "To display a file in the output\n cat <your_file>", main: Box::new(|args: &Vec<String>| { - let path = { - match args.get(1) { - Some(arg) => arg.clone(), - None => String::new(), - } - }; + let path = args.get(1).map_or(String::new(), |arg| arg.clone()); if let Some(mut file) = File::open(&path) { let mut string = String::new(); @@ -152,12 +149,7 @@ impl<'a> Command<'a> { name: "ls", help: "To list the content of the current directory\n ls", main: Box::new(|args: &Vec<String>| { - let path = { - match args.get(1) { - Some(arg) => arg.clone(), - None => String::new(), - } - }; + let path = args.get(1).map_or(String::new(), |arg| arg.clone()); if let Some(dir) = read_dir(&path) { for entry in dir { @@ -241,21 +233,10 @@ impl<'a> Command<'a> { name: "sleep", help: "Make a sleep in the current session\n sleep <number_of_seconds>", main: Box::new(|args: &Vec<String>| { - let secs = { - match args.get(1) { - Some(arg) => arg.to_num() as i64, - None => 0, - } - }; - - let nanos = { - match args.get(2) { - Some(arg) => arg.to_num() as i32, - None => 0, - } - }; - + let secs = args.get(1).map_or(0, |arg| arg.to_num() as i64); + let nanos = args.get(2).map_or(0, |arg| arg.to_num() as i32); println!("Sleep: {} {}", secs, nanos); + let remaining = Duration::new(secs, nanos).sleep(); println!("Remaining: {} {}", remaining.secs, remaining.nanos); }), @@ -271,12 +252,7 @@ impl<'a> Command<'a> { return; } - let path = { - match args.get(1) { - Some(arg) => arg.clone(), - None => String::new(), - } - }; + let path = args.get(1).map_or(String::new(), |arg| arg.clone()); if let Some(mut file) = File::open(&path) { println!("URL: {:?}", file.path()); @@ -320,12 +296,7 @@ impl<'a> Command<'a> { name: "url_hex", help: "", main: Box::new(|args: &Vec<String>| { - let path = { - match args.get(1) { - Some(arg) => arg.clone(), - None => String::new(), - } - }; + let path = args.get(1).map_or(String::new(), |arg| arg.clone()); if let Some(mut file) = File::open(&path) { let mut vec: Vec<u8> = Vec::new(); @@ -369,19 +340,20 @@ impl<'a> Command<'a> { }), }); - let mut command_helper: HashMap<String, String> = HashMap::new(); - - for c in commands.iter() { - command_helper.insert(c.name.clone().to_string(), c.help.clone().to_string()); - } + // TODO: Someone should implement FromIterator for HashMap before + // changing the type back to HashMap + let mut command_helper: BTreeMap<String, String> = commands + .iter() + .map(|c| (c.name.to_string(), c.help.to_string())) + .collect(); commands.push(Command { name: "man", help: "Display a little helper for a given command\n man ls", main: Box::new(move |args: &Vec<String>| { if let Some(command) = args.get(1) { - if command_helper.contains_key(&command) { - match command_helper.get(&command) { + if command_helper.contains_key(command) { + match command_helper.get(command) { Some(help) => println!("{}", help), None => println!("Command helper not found [run 'help']..."), }