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
};
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 <sleeplessval@gmail.com>
"Valerie Wolfe <sleeplessval@gmail.com>
A mathematical expression evaluator written in Rust.
usage:
qm [EXPRESSION]...",
bold = style::Bold,
reset = style::Reset,
version = crate::VERSION
qm [EXPRESSION]..."
);
}