added last result variable '$'

This commit is contained in:
Valerie Wolfe 2024-07-11 09:54:24 -04:00
parent cf67bf73eb
commit d144bd9245
2 changed files with 32 additions and 28 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "quickmath"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ]
description = "A quick command-line math evaluator."

View file

@ -10,9 +10,8 @@ use std::{
use evalexpr::{
context_map,
eval_with_context_mut,
ContextWithMutableVariables,
HashMapContext,
Value
};
@ -89,37 +88,42 @@ fn main() {
fn do_eval(i_expression: String, context: &mut HashMapContext) {
let expression = i_expression.as_str();
let i_result = eval_with_context_mut(expression, context);
if i_result.is_err() {
let result = eval_with_context_mut(expression, context);
if let Ok(result) = result {
// display empty result
if result.is_empty() {
println!(
"{green}✓ {bold}{expression}",
bold = style::Bold,
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 {
println!(
"{red}✕ {bold}{expression}",
bold = style::Bold,
red = color::Fg(color::Red)
);
return;
}
let result = i_result.ok().unwrap();
if result.is_empty() {
println!(
"{green}✓ {bold}{expression}",
bold = style::Bold,
green = color::Fg(color::Green)
);
return;
}
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
);
}
fn reset() {