Feature/add show stack (#5)

* add -s flag and show command to display the last 5 entries
This commit is contained in:
T.v.Dein
2023-11-06 20:12:07 +01:00
committed by GitHub
parent 9441be35ef
commit bb49cb7626
2 changed files with 24 additions and 1 deletions

23
calc.go
View File

@@ -32,6 +32,7 @@ type Calc struct {
debug bool debug bool
batch bool batch bool
stdin bool stdin bool
showstack bool
stack *Stack stack *Stack
history []string history []string
completer readline.AutoCompleter completer readline.AutoCompleter
@@ -48,6 +49,7 @@ type Calc struct {
const Help string = `Available commands: const Help string = `Available commands:
batch toggle batch mode batch toggle batch mode
debug toggle debug output debug toggle debug output
show show the last 5 items of the stack
dump display the stack contents dump display the stack contents
clear clear the whole stack clear clear the whole stack
shift remove the last element of the stack shift remove the last element of the stack
@@ -81,7 +83,7 @@ median median of all values`
// commands, constants and operators, defined here to feed completion // commands, constants and operators, defined here to feed completion
// and our mode switch in Eval() dynamically // and our mode switch in Eval() dynamically
const ( 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` Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E`
) )
@@ -162,14 +164,21 @@ func (c *Calc) ToggleStdin() {
c.stdin = !c.stdin c.stdin = !c.stdin
} }
func (c *Calc) ToggleShow() {
c.showstack = !c.showstack
}
func (c *Calc) Prompt() string { func (c *Calc) Prompt() string {
p := "\033[31m»\033[0m " p := "\033[31m»\033[0m "
b := "" b := ""
if c.batch { if c.batch {
b = "->batch" b = "->batch"
} }
d := "" d := ""
v := "" v := ""
if c.debug { if c.debug {
d = "->debug" d = "->debug"
v = fmt.Sprintf("/rev%d", c.stack.rev) v = fmt.Sprintf("/rev%d", c.stack.rev)
@@ -271,6 +280,8 @@ func (c *Calc) Eval(line string) {
for _, entry := range c.history { for _, entry := range c.history {
fmt.Println(entry) fmt.Println(entry)
} }
case "show":
c.ToggleShow()
case "exit": case "exit":
fallthrough fallthrough
case "quit": case "quit":
@@ -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 // Execute a math function, check if it is defined just in case

View File

@@ -39,6 +39,7 @@ Usage: rpn [-bdvh] [<operator>]
Options: Options:
-b, --batchmode enable batch mode -b, --batchmode enable batch mode
-d, --debug enable debug mode -d, --debug enable debug mode
-s, --stack show last 5 items of the stack (off by default)
-m, --manual show manual -m, --manual show manual
-v, --version show version -v, --version show version
-h, --help show help -h, --help show help
@@ -58,6 +59,7 @@ func main() {
configfile := "" configfile := ""
flag.BoolVarP(&calc.batch, "batchmode", "b", false, "batch mode") 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(&enabledebug, "debug", "d", false, "debug mode")
flag.BoolVarP(&showversion, "version", "v", false, "show version") flag.BoolVarP(&showversion, "version", "v", false, "show version")
flag.BoolVarP(&showhelp, "help", "h", false, "show usage") flag.BoolVarP(&showhelp, "help", "h", false, "show usage")