significant code clean up, better "keyword" matching, added help text

This commit is contained in:
Valerie Wolfe 2022-06-13 13:49:34 -04:00
parent e8bec6e32d
commit 6ffd55a637
6 changed files with 43 additions and 77 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
Cargo.lock

65
Cargo.lock generated
View file

@ -1,65 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "evalexpr"
version = "7.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d4fd7bd9e32c1205549decf6f36772d7b606a579b26afaffa335ae148151a5d"
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
[[package]]
name = "quickmaths"
version = "0.1.3"
dependencies = [
"evalexpr",
"termion",
]
[[package]]
name = "redox_syscall"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_termios"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
dependencies = [
"redox_syscall",
]
[[package]]
name = "termion"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e"
dependencies = [
"libc",
"numtoa",
"redox_syscall",
"redox_termios",
]

View file

@ -1,6 +1,6 @@
[package]
name = "quickmaths"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

10
README.md Normal file
View file

@ -0,0 +1,10 @@
# quickmaths
A small Rust program that can do common math from the command line.
I got VERY tired of opening a calculator or a full Python interpreter to do simple math, so I initially created `quickmaths` in Python to do math from the command line.
As the features I wanted changed, such as variable assignment, interactive mode, and output status and formatting, I decided to rebuild the utility in Rust.

View file

@ -6,6 +6,7 @@ use evalexpr::{
use crate::util;
// Data Science
pub fn average(arg: &Value) -> Result<Value, EvalexprError> {
let arguments = arg.as_tuple()?;
let count = arguments.len() as i64;
@ -45,6 +46,7 @@ pub fn average(arg: &Value) -> Result<Value, EvalexprError> {
}
}
// Radix conversion
pub fn binary(arg: &Value) -> Result<Value, EvalexprError> {
if !arg.is_string() {
let num = arg.as_int()?;

View file

@ -24,10 +24,11 @@ use termion::{
mod helper;
mod util;
pub const VERSION: &str = "0.1.4";
pub const VERSION: &str = "0.1.5";
fn main() {
let mut context = context_map! {
// data science functions
"avg" => Function::new(|arg| helper::average(arg)),
// radix functions
@ -47,29 +48,34 @@ fn main() {
break;
}
let line = i_line.trim().to_string();
if line.is_empty() || line == "exit" {
break;
match line.as_str() {
"" |
"exit" => break,
_ => do_eval(line, &mut context)
}
let result = do_eval(line, &mut context);
println!("{}{}{}", result.0, color::Fg(color::Reset), style::Reset);
reset();
}
} else {
for expression in expressions {
let result = do_eval(expression, &mut context);
println!("{}{}{}", result.0, color::Fg(color::Reset), style::Reset);
match expression.as_str() {
"help" => help_text(),
_ => do_eval(expression, &mut context)
}
}
}
}
fn do_eval(i_expression: String, context: &mut HashMapContext) -> (String, Option<Value>) {
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() {
return (format!("{}{}{}", color::Fg(color::Red), style::Bold, expression), None);
println!("{}{}{}", color::Fg(color::Red), style::Bold, expression);
return;
}
let result = i_result.ok().unwrap();
if result.is_empty() {
return (format!("{}{}{}", color::Fg(color::Green), style::Bold, expression), None);
println!("{}{}{}", color::Fg(color::Green), style::Bold, expression);
return;
}
let delimiter;
match result {
@ -77,6 +83,18 @@ fn do_eval(i_expression: String, context: &mut HashMapContext) -> (String, Optio
Value::String(ref _str) => delimiter = "=>",
_ => delimiter = "="
}
return (format!("{}{}{}{} {} {}{}", style::Faint, style::Italic, expression, style::Reset, delimiter, style::Bold, result), Some(result));
println!("{}{}{}{} {} {}{}", style::Faint, style::Italic, expression, style::Reset, delimiter, style::Bold, result);
}
fn reset() {
print!("{}{}", style::Reset, color::Fg(color::Reset));
stdout().flush().unwrap();
}
fn help_text() {
println!("{}quickmaths v{}{}", style::Bold, crate::VERSION, style::Reset);
println!("Valerie Wolfe <sleeplessval@gmail.com>");
println!("A mathematical expression evaluator written in Rust.\n");
println!("USAGE:");
println!("\tqm [EXPRESSION]...\n");
}