refactored help flag and initial implementations of 'empty' and 'version' flags

This commit is contained in:
Valerie Wolfe 2024-08-22 13:19:33 -04:00
parent e5d358477f
commit 6a526ba8a5
2 changed files with 56 additions and 35 deletions

6
src/flag.rs Normal file
View file

@ -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" ];

View file

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