From d144bd92454cf5ec4d0d04eab7ddcd3fb218be9c Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 11 Jul 2024 09:54:24 -0400 Subject: [PATCH] added last result variable '$' --- Cargo.toml | 2 +- src/main.rs | 58 ++++++++++++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ee56b44..9f38498 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "quickmath" -version = "0.2.3" +version = "0.2.4" edition = "2021" authors = [ "Valerie Wolfe " ] description = "A quick command-line math evaluator." diff --git a/src/main.rs b/src/main.rs index 8fb110a..334f03c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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() {