major main method cleanup and implemented quiet flag
This commit is contained in:
parent
2bae1475b6
commit
ed0e572f14
1 changed files with 30 additions and 28 deletions
58
src/main.rs
58
src/main.rs
|
@ -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,44 +67,44 @@ 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 quiet {
|
||||||
|
if let Ok(result) = result { println!("{result}") }
|
||||||
|
else { exit(1) }
|
||||||
|
} else { format(expression, result) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format(expression: &str, result: Result<Value, EvalexprError>) {
|
||||||
if let Ok(result) = result {
|
if let Ok(result) = result {
|
||||||
// display empty result
|
if !result.is_empty() {
|
||||||
if result.is_empty() {
|
let delimiter =
|
||||||
|
match result {
|
||||||
|
Value::Boolean(_) => "is",
|
||||||
|
Value::String(_) => "=>",
|
||||||
|
_ => "="
|
||||||
|
};
|
||||||
|
println!(
|
||||||
|
"{faint}{italic}{expression}{reset} {delimiter} {bold}{result}",
|
||||||
|
bold = style::Bold,
|
||||||
|
faint = style::Faint,
|
||||||
|
italic = style::Italic,
|
||||||
|
reset = style::Reset
|
||||||
|
);
|
||||||
|
} else {
|
||||||
println!(
|
println!(
|
||||||
"{green}✓ {bold}{expression}",
|
"{green}✓ {bold}{expression}",
|
||||||
bold = style::Bold,
|
bold = style::Bold,
|
||||||
green = color::Fg(color::Green)
|
green = color::Fg(color::Green)
|
||||||
);
|
);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get appropriate symbol for result by type
|
|
||||||
let delimiter;
|
|
||||||
match result {
|
|
||||||
Value::Boolean(_bool) => delimiter = "is",
|
|
||||||
Value::String(ref _str) => delimiter = "=>",
|
|
||||||
_ => delimiter = "="
|
|
||||||
}
|
|
||||||
println!(
|
|
||||||
"{faint}{italic}{expression}{reset} {delimiter} {bold}{result}",
|
|
||||||
bold = style::Bold,
|
|
||||||
faint = style::Faint,
|
|
||||||
italic = style::Italic,
|
|
||||||
reset = style::Reset
|
|
||||||
);
|
|
||||||
|
|
||||||
// set "last" variable
|
|
||||||
context.set_value("$".to_string(), result).ok();
|
|
||||||
} else {
|
} else {
|
||||||
println!(
|
println!(
|
||||||
"{red}✕ {bold}{expression}",
|
"{red}✕ {bold}{expression}",
|
||||||
|
|
Loading…
Reference in a new issue