refactored help flag and initial implementations of 'empty' and 'version' flags
This commit is contained in:
parent
e5d358477f
commit
6a526ba8a5
2 changed files with 56 additions and 35 deletions
6
src/flag.rs
Normal file
6
src/flag.rs
Normal 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" ];
|
||||
|
45
src/main.rs
45
src/main.rs
|
@ -21,6 +21,7 @@ use termion::{
|
|||
style
|
||||
};
|
||||
|
||||
mod flag;
|
||||
mod global;
|
||||
mod helper;
|
||||
mod util;
|
||||
|
@ -28,10 +29,22 @@ 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! {
|
||||
// handle breaking flags
|
||||
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
|
||||
"c" => global::LIGHT_SPEED,
|
||||
"e" => global::EULER,
|
||||
|
@ -56,21 +69,22 @@ fn main() {
|
|||
"ϕ" => global::GOLDEN_RATIO,
|
||||
"π" => global::PI,
|
||||
"√" => Function::new(|arg| helper::square_root(arg))
|
||||
}.unwrap();
|
||||
}.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]..."
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue