diff --git a/src/flag.rs b/src/flag.rs new file mode 100644 index 0000000..b2e1be3 --- /dev/null +++ b/src/flag.rs @@ -0,0 +1,6 @@ + +pub const EMPTY_CONTEXT: [&str;2] = [ "-E", "--empty"]; +pub const HELP: [&str;2] = [ "-h", "--help" ]; +pub const QUIET: [&str;2] = [ "-q", "--quiet" ]; +pub const VERSION: [&str;2] = [ "-V", "--version" ]; + diff --git a/src/main.rs b/src/main.rs index dba1c20..8c9da05 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use termion::{ style }; +mod flag; mod global; mod helper; mod util; @@ -28,49 +29,62 @@ mod util; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() { - let args = Arguments::from_env(); + let mut args = Arguments::from_env(); - // build eval context - let mut context = context_map! { - // globals - "c" => global::LIGHT_SPEED, - "e" => global::EULER, - "phi" => global::GOLDEN_RATIO, - "pi" => global::PI, - "√2" => global::ROOT_TWO, + // handle breaking flags + if args.contains(flag::HELP) { + help_text(); + return; + } + if args.contains(flag::VERSION) { + version_text(); + return; + } - // math functions - "fix" => Function::new(|arg| helper::fix(arg)), - "log" => Function::new(|arg| helper::logarithm(arg)), - "sqrt" => Function::new(|arg| helper::square_root(arg)), + // build context and handle empty flag + let mut context = + if !args.contains(flag::EMPTY_CONTEXT) { + context_map! { + // globals + "c" => global::LIGHT_SPEED, + "e" => global::EULER, + "phi" => global::GOLDEN_RATIO, + "pi" => global::PI, + "√2" => global::ROOT_TWO, - // data science functions - "avg" => Function::new(|arg| helper::average(arg)), + // math functions + "fix" => Function::new(|arg| helper::fix(arg)), + "log" => Function::new(|arg| helper::logarithm(arg)), + "sqrt" => Function::new(|arg| helper::square_root(arg)), - // radix functions - "bin" => Function::new(|arg| helper::binary(arg)), - "hex" => Function::new(|arg| helper::hexadecimal(arg)), - "oct" => Function::new(|arg| helper::octal(arg)), + // data science functions + "avg" => Function::new(|arg| helper::average(arg)), - // character aliases - "ϕ" => global::GOLDEN_RATIO, - "π" => global::PI, - "√" => Function::new(|arg| helper::square_root(arg)) - }.unwrap(); + // radix functions + "bin" => Function::new(|arg| helper::binary(arg)), + "hex" => Function::new(|arg| helper::hexadecimal(arg)), + "oct" => Function::new(|arg| helper::octal(arg)), + + // character aliases + "ϕ" => global::GOLDEN_RATIO, + "π" => global::PI, + "√" => Function::new(|arg| helper::square_root(arg)) + }.unwrap() + } else { + HashMapContext::new() + }; // collect args and evaluate if present let expressions = args.finish(); if expressions.len() > 0 { for expression in expressions { let expression: String = expression.to_string_lossy().into(); - match expression.as_str() { - "help" => help_text(), - _ => do_eval(expression, &mut context) - } + do_eval(expression, &mut context) } } else { // enter interactive mode if no args are given - println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset); + version_text(); + println!("{}Interactive Mode{}", style::Faint, style::Reset); loop { print!("> "); stdout().flush().unwrap(); @@ -135,16 +149,17 @@ fn reset() { stdout().flush().unwrap(); } +fn version_text() { + println!("qm v{VERSION}"); +} + fn help_text() { + version_text(); println!( - "{bold}quickmaths v{version}{reset} -Valerie Wolfe +"Valerie Wolfe A mathematical expression evaluator written in Rust. usage: - qm [EXPRESSION]...", - bold = style::Bold, - reset = style::Reset, - version = crate::VERSION + qm [EXPRESSION]..." ); }