Compare commits
No commits in common. "f1970a657aa4df877a14fd586cb4a8b96d01d01a" and "f9e8696d72a556fed33c95e214411b5c9cf64c39" have entirely different histories.
f1970a657a
...
f9e8696d72
3 changed files with 19 additions and 14 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "quickmath"
|
name = "quickmath"
|
||||||
version = "0.3.0"
|
version = "0.2.3"
|
||||||
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."
|
||||||
|
@ -14,7 +14,6 @@ path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
evalexpr = "11.0.0"
|
evalexpr = "11.0.0"
|
||||||
rustyline = "14.0.0"
|
|
||||||
termion = "1.5.6"
|
termion = "1.5.6"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub fn fix(arg: &Value) -> EvalResult {
|
||||||
|
|
||||||
let count = args.len();
|
let count = args.len();
|
||||||
if count != 2 {
|
if count != 2 {
|
||||||
return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2..=2, actual: count });
|
return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2, actual: count });
|
||||||
}
|
}
|
||||||
|
|
||||||
let float = args[0].as_float()?;
|
let float = args[0].as_float()?;
|
||||||
|
@ -63,7 +63,7 @@ pub fn logarithm(arg: &Value) -> EvalResult {
|
||||||
output = value.log(base).into();
|
output = value.log(base).into();
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2..=2, actual: count });
|
return Err(EvalexprError::WrongFunctionArgumentAmount { expected: 2, actual: count });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok(output);
|
return Ok(output);
|
||||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -1,6 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
io::{
|
io::{
|
||||||
|
stdin,
|
||||||
stdout,
|
stdout,
|
||||||
|
|
||||||
Write
|
Write
|
||||||
|
@ -15,7 +16,6 @@ use evalexpr::{
|
||||||
HashMapContext,
|
HashMapContext,
|
||||||
Value
|
Value
|
||||||
};
|
};
|
||||||
use rustyline::DefaultEditor;
|
|
||||||
use termion::{
|
use termion::{
|
||||||
color,
|
color,
|
||||||
style
|
style
|
||||||
|
@ -62,27 +62,33 @@ fn main() {
|
||||||
for expression in expressions {
|
for expression in expressions {
|
||||||
match expression.as_str() {
|
match expression.as_str() {
|
||||||
"help" => help_text(),
|
"help" => help_text(),
|
||||||
_ => do_eval(&expression, &mut context)
|
_ => do_eval(expression, &mut context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut rl = DefaultEditor::new().unwrap();
|
|
||||||
// enter interactive mode if no args are given
|
// enter interactive mode if no args are given
|
||||||
println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset);
|
println!("{}quickmaths v{}{}\n{}Interactive Mode{}", style::Bold, VERSION, style::Reset, style::Faint, style::Reset);
|
||||||
loop {
|
loop {
|
||||||
match rl.readline("> ").as_deref() {
|
print!("> ");
|
||||||
Ok("") |
|
stdout().flush().unwrap();
|
||||||
Ok("exit") |
|
let mut i_line = String::new();
|
||||||
Err(_) => break,
|
let line_result = stdin().read_line(&mut i_line);
|
||||||
|
if line_result.is_err() {
|
||||||
Ok(line) => do_eval(line, &mut context),
|
break;
|
||||||
|
}
|
||||||
|
let line = i_line.trim().to_string();
|
||||||
|
match line.as_str() {
|
||||||
|
"" |
|
||||||
|
"exit" => break,
|
||||||
|
_ => do_eval(line, &mut context)
|
||||||
}
|
}
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_eval(expression: &str, context: &mut HashMapContext) {
|
fn do_eval(i_expression: String, context: &mut HashMapContext) {
|
||||||
|
let expression = i_expression.as_str();
|
||||||
let i_result = eval_with_context_mut(expression, context);
|
let i_result = eval_with_context_mut(expression, context);
|
||||||
if i_result.is_err() {
|
if i_result.is_err() {
|
||||||
println!(
|
println!(
|
||||||
|
|
Loading…
Reference in a new issue