From 8e777253347ab088cb7283cbca64c4a912a74017 Mon Sep 17 00:00:00 2001 From: shy Date: Mon, 19 Apr 2021 13:34:44 +0200 Subject: [PATCH] Minor refactoring. --- src/cradle.rs | 2 +- src/lib.rs | 72 ++++++++++++++++++++++++--------------------------- src/utils.rs | 1 - 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/src/cradle.rs b/src/cradle.rs index 6eb87ce..815288a 100644 --- a/src/cradle.rs +++ b/src/cradle.rs @@ -114,7 +114,7 @@ impl Cradle { // Parse command line argument to --command into a vector of strings suitable // for process::Command::new(). - pub fn parse(input: &str) -> Vec { + pub fn parse(input: String) -> Vec { let mut command: Vec = Vec::new(); let mut segment: String = String::new(); let mut quoted = false; diff --git a/src/lib.rs b/src/lib.rs index 95e51a9..68becf4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ extern crate signal_hook; extern crate termion; +extern crate unicode_segmentation; mod alarm; mod buffer; mod clock; @@ -362,46 +363,41 @@ impl Config { }; let mut iter = args.skip(1); - loop { - if let Some(arg) = iter.next() { - match arg.as_str() { - "-h" | "--help" => { - // Print usage information and exit - println!("{}", USAGE); - process::exit(0); - } - "-v" | "--version" => { - println!("{} {}", NAME, VERSION); - process::exit(0); - } - "-p" | "--plain" => config.font = &font::PLAIN, - "-f" | "--fancy" => { - config.fancy = true; - config.font = &font::CHROME; - } - "-q" | "--quit" => config.quit = true, - "-e" | "--exec" => { - if let Some(cmd) = iter.next() { - //config.command = Some(Config::parse_to_command(&e)); - config.commands.add(Cradle::parse(&cmd)); - } else { - return Err(format!("Missing parameter to \"{}\".", arg)); - } - } - any if any.starts_with('-') => { - // Unrecognized flag. - return Err(format!("Unrecognized option: \"{}\"\nUse \"-h\" or \"--help\" for a list of valid command line options.", any)); - } - any => { - // Alarm to add. - if let Err(error) = alarm_roster.add(&String::from(any)) { - return Err(format!("Error adding \"{}\" as alarm. ({})", any, error)); - } + while let Some(arg) = iter.next() { + match arg.as_str() { + "-h" | "--help" => { + // Print usage information and exit + println!("{}", USAGE); + process::exit(0); + } + "-v" | "--version" => { + println!("{} {}", NAME, VERSION); + process::exit(0); + } + "-p" | "--plain" => config.font = &font::PLAIN, + "-f" | "--fancy" => { + config.fancy = true; + config.font = &font::CHROME; + } + "-q" | "--quit" => config.quit = true, + "-e" | "--exec" => { + if let Some(cmd) = iter.next() { + config.commands.add(Cradle::parse(cmd)); + } else { + return Err(format!("Missing parameter to \"{}\".", arg)); } } - } else { - break; - } // All command line parameters processed. + any if any.starts_with('-') => { + // Unrecognized flag. + return Err(format!("Unrecognized option: \"{}\"\nUse \"-h\" or \"--help\" for a list of valid command line options.", any)); + } + any => { + // Alarm to add. + if let Err(error) = alarm_roster.add(&String::from(any)) { + return Err(format!("Error adding \"{}\" as alarm. ({})", any, error)); + } + } + } } Ok(config) } diff --git a/src/utils.rs b/src/utils.rs index 2807cab..426f36d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,7 +15,6 @@ // You should have received a copy of the GNU General Public License // along with Kitchentimer. If not, see . -extern crate unicode_segmentation; use unicode_segmentation::UnicodeSegmentation; pub fn grapheme_truncate(input: &mut String, limit: usize, ellipse: char) {