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]
|
[package]
|
||||||
name = "quickmath"
|
name = "quickmath"
|
||||||
version = "0.2.3"
|
version = "0.2.4"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ]
|
authors = [ "Valerie Wolfe <sleeplessval@gmail.com>" ]
|
||||||
description = "A quick command-line math evaluator."
|
description = "A quick command-line math evaluator."
|
||||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -10,9 +10,8 @@ use std::{
|
||||||
|
|
||||||
use evalexpr::{
|
use evalexpr::{
|
||||||
context_map,
|
context_map,
|
||||||
|
|
||||||
eval_with_context_mut,
|
eval_with_context_mut,
|
||||||
|
ContextWithMutableVariables,
|
||||||
HashMapContext,
|
HashMapContext,
|
||||||
Value
|
Value
|
||||||
};
|
};
|
||||||
|
@ -89,16 +88,9 @@ fn main() {
|
||||||
|
|
||||||
fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
||||||
let expression = i_expression.as_str();
|
let expression = i_expression.as_str();
|
||||||
let i_result = eval_with_context_mut(expression, context);
|
let result = eval_with_context_mut(expression, context);
|
||||||
if i_result.is_err() {
|
if let Ok(result) = result {
|
||||||
println!(
|
// display empty result
|
||||||
"{red}✕ {bold}{expression}",
|
|
||||||
bold = style::Bold,
|
|
||||||
red = color::Fg(color::Red)
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let result = i_result.ok().unwrap();
|
|
||||||
if result.is_empty() {
|
if result.is_empty() {
|
||||||
println!(
|
println!(
|
||||||
"{green}✓ {bold}{expression}",
|
"{green}✓ {bold}{expression}",
|
||||||
|
@ -107,6 +99,8 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get appropriate symbol for result by type
|
||||||
let delimiter;
|
let delimiter;
|
||||||
match result {
|
match result {
|
||||||
Value::Boolean(_bool) => delimiter = "is",
|
Value::Boolean(_bool) => delimiter = "is",
|
||||||
|
@ -120,6 +114,16 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
||||||
italic = style::Italic,
|
italic = style::Italic,
|
||||||
reset = style::Reset
|
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() {
|
fn reset() {
|
||||||
|
|
Loading…
Reference in a new issue