added last result variable '$'
This commit is contained in:
parent
cf67bf73eb
commit
d144bd9245
2 changed files with 32 additions and 28 deletions
|
@ -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."
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -10,9 +10,8 @@ use std::{
|
|||
|
||||
use evalexpr::{
|
||||
context_map,
|
||||
|
||||
eval_with_context_mut,
|
||||
|
||||
ContextWithMutableVariables,
|
||||
HashMapContext,
|
||||
Value
|
||||
};
|
||||
|
@ -89,16 +88,9 @@ 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() {
|
||||
println!(
|
||||
"{red}✕ {bold}{expression}",
|
||||
bold = style::Bold,
|
||||
red = color::Fg(color::Red)
|
||||
);
|
||||
return;
|
||||
}
|
||||
let result = i_result.ok().unwrap();
|
||||
let result = eval_with_context_mut(expression, context);
|
||||
if let Ok(result) = result {
|
||||
// display empty result
|
||||
if result.is_empty() {
|
||||
println!(
|
||||
"{green}✓ {bold}{expression}",
|
||||
|
@ -107,6 +99,8 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
|||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// get appropriate symbol for result by type
|
||||
let delimiter;
|
||||
match result {
|
||||
Value::Boolean(_bool) => delimiter = "is",
|
||||
|
@ -120,6 +114,16 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
|||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn reset() {
|
||||
|
|
Loading…
Reference in a new issue