aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--calc.go11
-rw-r--r--ui.go2
2 files changed, 13 insertions, 0 deletions
diff --git a/calc.go b/calc.go
index 47ece5b..67794aa 100644
--- a/calc.go
+++ b/calc.go
@@ -40,6 +40,17 @@ func (c *Calculator) swap() {
}
}
+// dup duplicates the value at the bottom of the stack.
+func (c *Calculator) dup() {
+ v, err := c.stack.pop()
+ if err != nil {
+ // empty
+ return
+ }
+ c.stack.push(v)
+ c.stack.push(v)
+}
+
// negate negates the number in the buffer, if any; or the bottom number on the
// stack, if any.
func (c *Calculator) negate() {
diff --git a/ui.go b/ui.go
index 4d121a7..cea51e7 100644
--- a/ui.go
+++ b/ui.go
@@ -64,6 +64,8 @@ func (ui UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
fn(&ui.calc)
} else if v, err := ui.calc.parseBuffer(); err == nil {
ui.calc.stack.push(v)
+ } else if ui.calc.buf == "" {
+ ui.calc.dup()
}
ui.calc.buf = ""
default: