aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/input.rs4
-rw-r--r--src/lib.rs30
-rw-r--r--src/ui.rs17
3 files changed, 43 insertions, 8 deletions
diff --git a/src/input.rs b/src/input.rs
index 2f9d820..462e43c 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -1,6 +1,6 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
-use crate::{Calculator, Function, Operator, Signal};
+use crate::{Calculator, Constant, Function, Operator, Signal};
impl Calculator {
pub fn handle_input(&mut self, key: KeyEvent) -> Signal {
@@ -32,6 +32,8 @@ impl Calculator {
if let Some(st) = self.stack.pop() {
self.stack.push(func.call_on(st));
}
+ } else if let Ok(c) = Constant::parse(&self.input_buffer) {
+ self.stack.push(c.value());
} else if let Ok(bu) = self.input_buffer.parse::<f64>() {
self.stack.push(bu);
}
diff --git a/src/lib.rs b/src/lib.rs
index d8509d5..00163b1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,5 @@
+use std::f64::consts::{E, PI};
+
mod input;
pub mod ui;
@@ -61,8 +63,8 @@ enum Function {
RSin, // Sine (radians)
RCos, // Cosing (radians)
RTan, // Tangent (radians)
- Deg, // Convert from radians to degrees.
- Rad, // Convert from degrees to radians.
+ Deg, // Convert from radians to degrees
+ Rad, // Convert from degrees to radians
}
impl Function {
@@ -96,6 +98,30 @@ impl Function {
struct ParseFunctionError(String);
+enum Constant {
+ Pi, // Archimedes’ constant (π)
+ E, // Euler's number (e)
+}
+
+impl Constant {
+ fn parse(s: &str) -> Result<Self, ParseConstantError> {
+ match s {
+ "pi" => Ok(Self::Pi),
+ "e" => Ok(Self::E),
+ _ => Err(ParseConstantError(s.to_string())),
+ }
+ }
+
+ fn value(&self) -> f64 {
+ match self {
+ Self::Pi => PI,
+ Self::E => E,
+ }
+ }
+}
+
+struct ParseConstantError(String);
+
pub enum Signal {
None,
Exit,
diff --git a/src/ui.rs b/src/ui.rs
index 1b50fb5..3d09221 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -12,7 +12,7 @@ use tui::{
Frame, Terminal,
};
-use crate::{Calculator, Function};
+use crate::{Calculator, Constant, Function};
const WIDTH: u16 = 32;
@@ -78,10 +78,7 @@ fn input_buffer_widget(input_buffer: &str) -> impl Widget {
Span::raw(">"),
Span::styled(
format!(" {}", input_buffer),
- match Function::parse(&input_buffer) {
- Ok(_) => Style::default().add_modifier(Modifier::BOLD),
- Err(_) => Style::default(),
- },
+ input_buffer_style(input_buffer),
),
]))
.block(Block::default().borders(Borders::ALL))
@@ -91,3 +88,13 @@ fn version_number_widget() -> impl Widget {
Paragraph::new(format!("pfc-{}", option_env!("CARGO_PKG_VERSION").unwrap()))
.alignment(Alignment::Center)
}
+
+fn input_buffer_style(input_buffer: &str) -> Style {
+ if let Ok(_) = Function::parse(&input_buffer) {
+ Style::default().add_modifier(Modifier::BOLD)
+ } else if let Ok(_) = Constant::parse(&input_buffer) {
+ Style::default().add_modifier(Modifier::BOLD)
+ } else {
+ Style::default()
+ }
+}