diff options
-rw-r--r-- | src/error/mod.rs | 42 | ||||
-rw-r--r-- | src/lex/mod.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 4 | ||||
-rw-r--r-- | src/readline/mod.rs | 2 |
4 files changed, 50 insertions, 4 deletions
diff --git a/src/error/mod.rs b/src/error/mod.rs index ec2b555..307ca31 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs | |||
@@ -2,11 +2,16 @@ | |||
2 | * Refer to LICENCE for more information. | 2 | * Refer to LICENCE for more information. |
3 | * */ | 3 | * */ |
4 | 4 | ||
5 | use std::iter::ExactSizeIterator; | ||
6 | |||
7 | use crate::lex; | ||
8 | |||
5 | #[derive(Debug)] | 9 | #[derive(Debug)] |
6 | pub enum CalcError { | 10 | pub enum CalcError { |
7 | Math(Math), | 11 | Math(Math), |
8 | Syntax(String), | 12 | Syntax(String), |
9 | Parser(String), | 13 | Parser(String), |
14 | Help, | ||
10 | } | 15 | } |
11 | 16 | ||
12 | #[derive(Debug)] | 17 | #[derive(Debug)] |
@@ -25,5 +30,42 @@ pub fn handler(e: CalcError) -> String { | |||
25 | }, | 30 | }, |
26 | CalcError::Syntax(details) => format!("Syntax Error: {}", details), | 31 | CalcError::Syntax(details) => format!("Syntax Error: {}", details), |
27 | CalcError::Parser(details) => format!("Parser Error: {}", details), | 32 | CalcError::Parser(details) => format!("Parser Error: {}", details), |
33 | CalcError::Help => format!( | ||
34 | "Constants\n{}\nFunctions\n{}\nOperators\n{}\n", | ||
35 | blocks(lex::CONSTANTS.keys().cloned()), | ||
36 | blocks(lex::FUNCTIONS.keys().cloned()), | ||
37 | { | ||
38 | let l: Vec<_> = lex::OPERATORS.keys().map(|c| c.to_string()).collect(); | ||
39 | l.join(" ") | ||
40 | } | ||
41 | ), | ||
42 | } | ||
43 | } | ||
44 | |||
45 | /// Convert iterator into strings of chunks of 8 right padded with space. | ||
46 | fn blocks(mut iter: impl Iterator<Item = &'static str> + ExactSizeIterator) -> String { | ||
47 | // calculate max width but ideally this should be calculated once | ||
48 | let mut max_width = 79; // capped at 79 | ||
49 | if let Ok(s) = std::env::var("COLUMNS") { | ||
50 | if let Ok(n) = s.parse() { | ||
51 | if n < max_width { | ||
52 | max_width = n; | ||
53 | } | ||
54 | } | ||
55 | } | ||
56 | |||
57 | // multiply by eight since we are formatting it into chunks of 8 | ||
58 | let items_per_line = max_width / 8; | ||
59 | let full_bytes = (iter.len() - iter.len() % items_per_line) * 8; | ||
60 | let part_bytes = iter.len() % items_per_line * 8; // leftovers | ||
61 | let n_newlines = iter.len() / items_per_line + if part_bytes > 0 { 1 } else { 0 }; | ||
62 | let mut s = String::with_capacity(full_bytes + part_bytes + n_newlines); | ||
63 | for _ in 0..n_newlines { | ||
64 | for item in iter.by_ref().take(items_per_line) { | ||
65 | s.extend(format!("{:>8}", item).chars()); | ||
66 | } | ||
67 | s.push('\n'); | ||
28 | } | 68 | } |
69 | debug_assert_eq!(s.capacity(), s.len()); // check capacity calculation | ||
70 | s | ||
29 | } | 71 | } |
diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 3222c12..b3faff8 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs | |||
@@ -70,14 +70,14 @@ pub enum Token { | |||
70 | } | 70 | } |
71 | 71 | ||
72 | lazy_static! { | 72 | lazy_static! { |
73 | static ref CONSTANTS: HashMap<&'static str, Token> = { | 73 | pub static ref CONSTANTS: HashMap<&'static str, Token> = { |
74 | let mut m = HashMap::new(); | 74 | let mut m = HashMap::new(); |
75 | m.insert("e", Token::Num(std::f64::consts::E)); | 75 | m.insert("e", Token::Num(std::f64::consts::E)); |
76 | m.insert("pi", Token::Num(std::f64::consts::PI)); | 76 | m.insert("pi", Token::Num(std::f64::consts::PI)); |
77 | m | 77 | m |
78 | }; | 78 | }; |
79 | 79 | ||
80 | static ref FUNCTIONS: HashMap<&'static str, Token> = { | 80 | pub static ref FUNCTIONS: HashMap<&'static str, Token> = { |
81 | let mut m = HashMap::new(); | 81 | let mut m = HashMap::new(); |
82 | m.insert("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())); | 82 | m.insert("sin", Function::token_from_fn("sin".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).sin())); |
83 | m.insert("cos", Function::token_from_fn("cos".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos())); | 83 | m.insert("cos", Function::token_from_fn("cos".into(), |x| is_radian_mode(x, CONFIGURATION.radian_mode).cos())); |
@@ -106,7 +106,7 @@ lazy_static! { | |||
106 | m | 106 | m |
107 | }; | 107 | }; |
108 | 108 | ||
109 | static ref OPERATORS: HashMap<char, Token> = { | 109 | pub static ref OPERATORS: HashMap<char, Token> = { |
110 | let mut m = HashMap::new(); | 110 | let mut m = HashMap::new(); |
111 | m.insert('+', Operator::token_from_op('+', |x, y| x + y, 2, true)); | 111 | m.insert('+', Operator::token_from_op('+', |x, y| x + y, 2, true)); |
112 | m.insert('-', Operator::token_from_op('-', |x, y| x - y, 2, true)); | 112 | m.insert('-', Operator::token_from_op('-', |x, y| x - y, 2, true)); |
diff --git a/src/main.rs b/src/main.rs index bc22ce2..7086d15 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -193,7 +193,9 @@ fn parse_arguments() -> Configuration { | |||
193 | 193 | ||
194 | pub fn eval_math_expression(input: &str, prev_ans: Option<f64>) -> Result<f64, CalcError> { | 194 | pub fn eval_math_expression(input: &str, prev_ans: Option<f64>) -> Result<f64, CalcError> { |
195 | let input = input.trim(); | 195 | let input = input.trim(); |
196 | let input = input.replace(" ", ""); | 196 | if input == "help" { |
197 | return Err(CalcError::Help); | ||
198 | } | ||
197 | if input.is_empty() { | 199 | if input.is_empty() { |
198 | return Ok(0.); | 200 | return Ok(0.); |
199 | } | 201 | } |
diff --git a/src/readline/mod.rs b/src/readline/mod.rs index ea195ee..d689f95 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs | |||
@@ -12,6 +12,7 @@ use directories::ProjectDirs; | |||
12 | 12 | ||
13 | use regex::Regex; | 13 | use regex::Regex; |
14 | 14 | ||
15 | use crate::error::CalcError; | ||
15 | use crate::eval_math_expression; | 16 | use crate::eval_math_expression; |
16 | 17 | ||
17 | pub struct RLHelper { | 18 | pub struct RLHelper { |
@@ -67,6 +68,7 @@ impl Highlighter for LineHighlighter { | |||
67 | } | 68 | } |
68 | Owned(coloured) | 69 | Owned(coloured) |
69 | } | 70 | } |
71 | Err(CalcError::Help) => Owned(line.replace("help", "\x1b[36mhelp\x1b[0m")), | ||
70 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)), | 72 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)), |
71 | } | 73 | } |
72 | } | 74 | } |