diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-02-09 15:31:59 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-02-09 15:31:59 -0330 |
| commit | b20026939f17469715e16d6738eefad135eccfa1 (patch) | |
| tree | 41596223a5a17f43ba09beadf129f9c45ff8b4e8 | |
| parent | 1f6fa5a58030f4ff2adde61e7652d1956e407510 (diff) | |
| download | pfc-b20026939f17469715e16d6738eefad135eccfa1.zip | |
add comments
| -rw-r--r-- | src/input.rs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/input.rs b/src/input.rs index e7783fb..21edef7 100644 --- a/src/input.rs +++ b/src/input.rs @@ -12,10 +12,7 @@ impl Calculator { _ => {} }, KeyModifiers::SHIFT => match key.code { - KeyCode::Char('D') => { - self.input_buffer = String::new(); - self.stack = Vec::new(); - } + KeyCode::Char('D') => self.clear(), _ => {} }, KeyModifiers::NONE => match key.code { @@ -47,6 +44,8 @@ impl Calculator { return Signal::None; } + // Push a character into the input buffer. If the character is an operator, + // the particular operation is performed. fn push_to_buffer(&mut self, c: char) { if c == '.' && !self.input_buffer.contains('.') { if self.input_buffer.len() == 0 { @@ -62,10 +61,18 @@ impl Calculator { } } + // Swap the bottom of the stack and the input buffer. If the input buffer + // is empty, this simply pops from the stack into the input buffer. fn swap(&mut self) { if let Some(f) = self.stack.pop() { self.stack.push(self.input_buffer.parse::<f64>().unwrap()); self.input_buffer = format!("{}", f); } } + + // Clear stack and input buffer. + fn clear(&mut self) { + self.input_buffer = String::new(); + self.stack = Vec::new(); + } } |