From 6ffd55a6370afca51e73bdb591ecba0a1977df35 Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 13 Jun 2022 13:49:34 -0400 Subject: [PATCH] significant code clean up, better "keyword" matching, added help text --- .gitignore | 1 + Cargo.lock | 65 --------------------------------------------------- Cargo.toml | 2 +- README.md | 10 ++++++++ src/helper.rs | 2 ++ src/main.rs | 40 ++++++++++++++++++++++--------- 6 files changed, 43 insertions(+), 77 deletions(-) delete mode 100644 Cargo.lock create mode 100644 README.md diff --git a/.gitignore b/.gitignore index ea8c4bf..96ef6c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +Cargo.lock diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 42fdcc0..0000000 --- a/Cargo.lock +++ /dev/null @@ -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", -] diff --git a/Cargo.toml b/Cargo.toml index 79af3b4..64a4837 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..ed4501b --- /dev/null +++ b/README.md @@ -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. + + diff --git a/src/helper.rs b/src/helper.rs index 417a6b9..37b066c 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -6,6 +6,7 @@ use evalexpr::{ use crate::util; +// Data Science pub fn average(arg: &Value) -> Result { let arguments = arg.as_tuple()?; let count = arguments.len() as i64; @@ -45,6 +46,7 @@ pub fn average(arg: &Value) -> Result { } } +// Radix conversion pub fn binary(arg: &Value) -> Result { if !arg.is_string() { let num = arg.as_int()?; diff --git a/src/main.rs b/src/main.rs index 9526ce6..ceadcc1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) { +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 "); + println!("A mathematical expression evaluator written in Rust.\n"); + println!("USAGE:"); + println!("\tqm [EXPRESSION]...\n"); +}