diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2023-02-02 13:16:54 -0330 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2023-02-02 13:16:54 -0330 |
| commit | 4a07f41766e8cc8966d7166daa5e1f90c582b1fb (patch) | |
| tree | 1e53e4e540e561c67ab322fd7cc86946a2c2855d | |
| parent | e4e4eb72e31620c7d032c9653047fd625575997b (diff) | |
| download | pfc-4a07f41766e8cc8966d7166daa5e1f90c582b1fb.zip | |
swap function
| -rw-r--r-- | src/input.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/input.rs b/src/input.rs index cc93415..ecb4b3f 100644 --- a/src/input.rs +++ b/src/input.rs @@ -15,6 +15,9 @@ impl Calculator { KeyCode::Char('q') => { return Signal::Exit; } + KeyCode::Char('j' | 'k') => { + self.swap(); + } KeyCode::Char(c) => { self.push_to_buffer(c); } @@ -51,4 +54,13 @@ impl Calculator { self.stack.push(self.input_buffer.parse::<f64>().unwrap()); self.input_buffer = String::new(); } + + fn swap(&mut self) { + if let Some(f) = self.stack.pop() { + if self.input_buffer.len() > 0 { + self.push_buffer_to_stack(); + } + self.input_buffer = format!("{}", f); + } + } } |