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,10 +29,22 @@ 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) {
help_text();
return;
}
if args.contains(flag::VERSION) {
version_text();
return;
}
// build context and handle empty flag
let mut context =
if !args.contains(flag::EMPTY_CONTEXT) {
context_map! {
// globals // globals
"c" => global::LIGHT_SPEED, "c" => global::LIGHT_SPEED,
"e" => global::EULER, "e" => global::EULER,
@ -56,21 +69,22 @@ fn main() {
"ϕ" => global::GOLDEN_RATIO, "ϕ" => global::GOLDEN_RATIO,
"π" => global::PI, "π" => global::PI,
"" => Function::new(|arg| helper::square_root(arg)) "" => Function::new(|arg| helper::square_root(arg))
}.unwrap(); }.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
); );
} }