aboutsummaryrefslogtreecommitdiffstats
path: root/src/input.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/input.rs')
-rw-r--r--src/input.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/input.rs b/src/input.rs
new file mode 100644
index 0000000..5ffaec8
--- /dev/null
+++ b/src/input.rs
@@ -0,0 +1,45 @@
+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
+
+use crate::{Calculator, Operator, Signal};
+
+impl Calculator {
+ pub fn handle_input(&mut self, key: KeyEvent) -> Signal {
+ match key.modifiers {
+ KeyModifiers::CONTROL => match key.code {
+ KeyCode::Char('c') => {
+ return Signal::Exit;
+ }
+ _ => {}
+ },
+ KeyModifiers::NONE => match key.code {
+ KeyCode::Char('q') => {
+ return Signal::Exit;
+ }
+ KeyCode::Char(c) => {
+ if c.is_ascii_digit() {
+ self.input_buffer.push(c);
+ } else if c == '.' && !self.input_buffer.contains('.') {
+ if self.input_buffer.len() == 0 {
+ self.input_buffer.push('0');
+ }
+ self.input_buffer.push(c);
+ } else if let Ok(op) = Operator::parse(c) {
+ if self.input_buffer.len() > 0 {
+ self.push_buffer_to_stack();
+ }
+ self.perform_operation(op);
+ }
+ }
+ KeyCode::Enter if self.input_buffer.len() > 0 => {
+ self.push_buffer_to_stack();
+ }
+ KeyCode::Backspace => {
+ self.input_buffer.pop();
+ }
+ _ => {}
+ },
+ _ => {}
+ }
+ return Signal::None;
+ }
+}