Skip to content

Commit

Permalink
Feature/add show stack (#5)
Browse files Browse the repository at this point in the history
* add -s flag and show command to display the last 5 entries
  • Loading branch information
TLINDEN authored Nov 6, 2023
1 parent 9441be3 commit bb49cb7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 22 additions & 1 deletion calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Calc struct {
debug bool
batch bool
stdin bool
showstack bool
stack *Stack
history []string
completer readline.AutoCompleter
Expand All @@ -48,6 +49,7 @@ type Calc struct {
const Help string = `Available commands:
batch toggle batch mode
debug toggle debug output
show show the last 5 items of the stack
dump display the stack contents
clear clear the whole stack
shift remove the last element of the stack
Expand Down Expand Up @@ -81,7 +83,7 @@ median median of all values`
// commands, constants and operators, defined here to feed completion
// and our mode switch in Eval() dynamically
const (
Commands string = `dump reverse debug undebug clear batch shift undo help history manual exit quit swap`
Commands string = `dump reverse debug undebug clear batch shift undo help history manual exit quit swap show`
Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E`
)

Expand Down Expand Up @@ -162,14 +164,21 @@ func (c *Calc) ToggleStdin() {
c.stdin = !c.stdin
}

func (c *Calc) ToggleShow() {
c.showstack = !c.showstack
}

func (c *Calc) Prompt() string {
p := "\033[31m»\033[0m "
b := ""

if c.batch {
b = "->batch"
}

d := ""
v := ""

if c.debug {
d = "->debug"
v = fmt.Sprintf("/rev%d", c.stack.rev)
Expand Down Expand Up @@ -271,6 +280,8 @@ func (c *Calc) Eval(line string) {
for _, entry := range c.history {
fmt.Println(entry)
}
case "show":
c.ToggleShow()
case "exit":
fallthrough
case "quit":
Expand All @@ -282,6 +293,16 @@ func (c *Calc) Eval(line string) {
}
}
}

if c.showstack && !c.stdin {
dots := ""

if c.stack.Len() > 5 {
dots = "... "
}
last := c.stack.Last(5)
fmt.Printf("stack: %s%s\n", dots, list2str(last))
}
}

// Execute a math function, check if it is defined just in case
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Usage: rpn [-bdvh] [<operator>]
Options:
-b, --batchmode enable batch mode
-d, --debug enable debug mode
-s, --stack show last 5 items of the stack (off by default)
-m, --manual show manual
-v, --version show version
-h, --help show help
Expand All @@ -58,6 +59,7 @@ func main() {
configfile := ""

flag.BoolVarP(&calc.batch, "batchmode", "b", false, "batch mode")
flag.BoolVarP(&calc.showstack, "showstack", "s", false, "show stack")
flag.BoolVarP(&enabledebug, "debug", "d", false, "debug mode")
flag.BoolVarP(&showversion, "version", "v", false, "show version")
flag.BoolVarP(&showhelp, "help", "h", false, "show usage")
Expand Down

0 comments on commit bb49cb7

Please sign in to comment.