Refactored the main() function
[evpf.git] / src / main.rs
index 2154ba5..d237193 100644 (file)
@@ -1,8 +1,7 @@
 use std::io;
 use std::io::Write;
 use regex::Regex;
-use ansi_term::Colour;
-use ansi_term::Style;
+use colored::*;
 use std::env;
 
 const ILLEGAL_EXP : &'static str = "Illegal Expression";
@@ -12,13 +11,12 @@ const ERR_POSTFIX_INCOMPLETE : &'static str = "Postfix expression incomplete!";
 const ERR_FLUSHING : &'static str = "Error flushing!";
 const ERR_READING_LINE : &'static str = "Error reading line!";
 const HELP_TEXT : [&str ; 4] = 
-                               ["Type an expression in postfix style to evaluate.",
+                               ["Type a postfix expression to evaluate.",
                                 "Example: 4 5 + 12 -",
                                 "Supported operators: +, -, *, /",
                                 "Type q, Q to quit"
                                 ];
 
-const RESULT : &'static str = "Result";
 const ERROR : &'static str = "Error";
 const ERROR_HELP : &'static str = "Type ? or h or H for help";
 
@@ -134,87 +132,97 @@ fn evaluate (expr : &str, match_num : &regex::Regex) -> Result<f32,String> {
        }
 }
 
-fn main() {
+// Single command mode - command line arguments mode - evaluate the expression
+// given in the command line and quit
+fn run_command_line (args : &Vec<String>, match_num : &regex::Regex) {
+       let mut expr = String::new ();
+       let mut i = 0;
+       // create the expression string to evaluate
+       for arg in args.iter() {
+               if i > 0 {
+                       expr.push_str (&arg);
+                       expr.push_str (" ");
+               }
+               i += 1;
+       }
+       // evaluate the result 
+       let res = evaluate (&expr, &match_num);
+       // if Result is OK then print the result in green
+       if res.is_ok () {
+               let restxt = format! ("{}", res.unwrap());
+               println! ("{}", restxt.green ());
+       } else {
+       // print the error in purple
+               let errtxt = format! ("{}: {}", ERROR, 
+                                                                               res.unwrap_err());
+               eprintln! ("{}", errtxt.purple ());
+       }
+}
 
-       let args : Vec<String> = env::args().collect ();
-       // regular expression to match a number 
-       let match_num = Regex::new (r"^\d+?\.*?\d*?$").unwrap ();
-       
-       if args.len () > 1 {
-       // if arguments are provided run in command line mode - i.e. print the 
-       // result and exit      
-               let mut expr = String::new ();
-               let mut i = 0;
-               // create the expression string to evaluate
-               for arg in args.iter() {
-                       if i > 0 {
-                               expr.push_str (&arg);
-                               expr.push_str (" ");
+// Interactive mode - display the prompt and evaluate expressions entered into
+// the prompt - until user quits
+fn run_interactive_mode (match_num : &regex::Regex) {
+       // get a line from input and evaluate it 
+       let mut expr = String::new ();
+
+       // loop until a blank line is received  
+       loop {
+               expr.clear ();  
+               print!("{}", "evpf>".bold() );
+               io::stdout().flush ().expect (ERR_FLUSHING);
+               // read a line of text
+               io::stdin().read_line (&mut expr).expect (ERR_READING_LINE);
+               // trim the text
+               let expr = expr.trim ();
+
+               if expr == "q" || expr == "Q" {
+               // quit if the expression is q or Qs 
+                       break;
+               } else if expr == "?" || expr == "h" || expr == "H" {
+               // display help text
+                       for text in HELP_TEXT.iter() {
+                               println! ("{}", text.cyan() );
                        }
-                       i += 1;
+
+                       continue;
+               } else if expr == "" {
+               // continue without proceeding
+                       continue;
                }
-               // evaluate the result 
+               
+               // Evaluate result
                let res = evaluate (&expr, &match_num);
+               
                // if Result is OK then print the result in green
                if res.is_ok () {
-                       let restxt = format! ("{}: {}", RESULT, 
-                                                                                       res.unwrap());
-                       println! ("{}", Colour::Green.paint (restxt));
+                       let restxt = format! ("{}", res.unwrap());
+                       println! ("{}", restxt.green ());
                } else {
                // print the error in purple
                        let errtxt = format! ("{}: {}", ERROR, 
                                                                                        res.unwrap_err());
-                       eprintln! ("{}", Colour::Purple.paint (errtxt));
-               }               
+                       eprintln! ("{}", errtxt.purple());
+                       eprintln! ("{}", ERROR_HELP.purple());
+               }
+       }
+}
+
+fn main() {
+       // collect the command line arguments - if any 
+       let args : Vec<String> = env::args().collect ();
+       // regular expression to match a number 
+       let match_num = Regex::new (r"^\d+?\.*?\d*?$").unwrap ();
+       
+       if args.len () > 1 {
+       // if arguments are provided run in command line mode - i.e. print the 
+       // result and exit      
+               run_command_line (&args, &match_num);
                
        } else {
                // if arguments are not provided run in interactive mode - 
                // display a prompt and get the expression 
                // repeat until the user quits
-               
-               // get a line from input and evaluate it 
-               let mut expr = String::new ();
+               run_interactive_mode (&match_num);
 
-               // loop until a blank line is received  
-               loop {
-                       expr.clear ();  
-                       print!("{}", Style::new().bold().paint("evpf>"));
-                       io::stdout().flush ().expect (ERR_FLUSHING);
-                       // read a line of text
-                       io::stdin().read_line (&mut expr).expect (ERR_READING_LINE);
-                       // trim the text
-                       let expr = expr.trim ();
-
-                       if expr == "q" || expr == "Q" {
-                       // quit if the expression is q or Qs 
-                               break;
-                       } else if expr == "?" || expr == "h" || expr == "H" {
-                       // display help text
-                               for text in HELP_TEXT.iter() {
-                                       println! ("{}", Colour::Cyan.paint(*text));
-                               }
-
-                               continue;
-                       } else if expr == "" {
-                       // continue without proceeding
-                               continue;
-                       }
-                       
-                       // Evaluate result
-                       let res = evaluate (&expr, &match_num);
-                       
-                       // if Result is OK then print the result in green
-                       if res.is_ok () {
-                               let restxt = format! ("{}: {}", RESULT, 
-                                                                                               res.unwrap());
-                               println! ("{}", Colour::Green.paint (restxt));
-                       } else {
-                       // print the error in purple
-                               let errtxt = format! ("{}: {}", ERROR, 
-                                                                                               res.unwrap_err());
-                               eprintln! ("{}", Colour::Purple.paint (errtxt));
-                               eprintln! ("{}", Colour::Purple.paint (ERROR_HELP));
-                       }
-               }
        }
 }