major main method cleanup and implemented quiet flag

This commit is contained in:
Valerie Wolfe 2024-08-28 13:46:11 -04:00
parent 2bae1475b6
commit ed0e572f14

View file

@ -5,12 +5,14 @@ use std::{
stdout, stdout,
Write Write
} },
process::exit
}; };
use evalexpr::{ use evalexpr::{
eval_with_context_mut, eval_with_context_mut,
ContextWithMutableVariables,
EvalexprError,
HashMapContext, HashMapContext,
Value Value
}; };
@ -21,7 +23,6 @@ use termion::{
}; };
mod context; mod context;
mod eval;
mod flag; mod flag;
mod util; mod util;
@ -41,13 +42,14 @@ fn main() {
} }
let mut context = util::build_context(&mut args); let mut context = util::build_context(&mut args);
let quiet = args.contains(flag::QUIET);
// 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();
do_eval(expression, &mut context) eval(&expression, &mut context, quiet);
} }
} else { } else {
// enter interactive mode if no args are given // enter interactive mode if no args are given
@ -65,34 +67,30 @@ fn main() {
match line.as_str() { match line.as_str() {
"" | "" |
"exit" => break, "exit" => break,
_ => do_eval(line, &mut context) _ => eval(&line, &mut context, quiet)
} }
reset(); reset();
} }
} }
} }
fn do_eval(i_expression: String, context: &mut HashMapContext) { fn eval(expression: &str, context: &mut HashMapContext, quiet: bool) {
let expression = i_expression.as_str();
let result = eval_with_context_mut(expression, context); let result = eval_with_context_mut(expression, context);
if let Ok(result) = result { if quiet {
// display empty result if let Ok(result) = result { println!("{result}") }
if result.is_empty() { else { exit(1) }
println!( } else { format(expression, result) }
"{green}✓ {bold}{expression}",
bold = style::Bold,
green = color::Fg(color::Green)
);
return;
} }
// get appropriate symbol for result by type fn format(expression: &str, result: Result<Value, EvalexprError>) {
let delimiter; if let Ok(result) = result {
if !result.is_empty() {
let delimiter =
match result { match result {
Value::Boolean(_bool) => delimiter = "is", Value::Boolean(_) => "is",
Value::String(ref _str) => delimiter = "=>", Value::String(_) => "=>",
_ => delimiter = "=" _ => "="
} };
println!( println!(
"{faint}{italic}{expression}{reset} {delimiter} {bold}{result}", "{faint}{italic}{expression}{reset} {delimiter} {bold}{result}",
bold = style::Bold, bold = style::Bold,
@ -100,9 +98,13 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) {
italic = style::Italic, italic = style::Italic,
reset = style::Reset reset = style::Reset
); );
} else {
// set "last" variable println!(
context.set_value("$".to_string(), result).ok(); "{green}✓ {bold}{expression}",
bold = style::Bold,
green = color::Fg(color::Green)
);
}
} else { } else {
println!( println!(
"{red}✕ {bold}{expression}", "{red}✕ {bold}{expression}",