3 Commits

Author SHA1 Message Date
T.v.Dein
cb774b3b80 added commandline and stdin tests using testscript (#28)
* added commandline and stdin tests using testscript

---------

Co-authored-by: Thomas von Dein <tom@vondein.org>
2023-12-07 14:09:42 +01:00
T.v.Dein
846b3e63fc don't show shortcuts in help (clutters it) (#27)
* don't show shortcuts in help (clutters it)

* bump version

---------

Co-authored-by: Thomas von Dein <tom@vondein.org>
2023-12-07 13:47:32 +01:00
T.v.Dein
5557ad5f99 use generics for contains() and add generic exists() (#29)
Co-authored-by: Thomas von Dein <tom@vondein.org>
2023-12-07 13:47:04 +01:00
3 changed files with 31 additions and 23 deletions

28
calc.go
View File

@@ -261,7 +261,7 @@ func (c *Calc) Eval(line string) {
continue
}
if _, ok := c.Funcalls[item]; ok {
if exists(c.Funcalls, item) {
if err := c.DoFuncall(item); err != nil {
fmt.Println(err)
} else {
@@ -270,8 +270,12 @@ func (c *Calc) Eval(line string) {
continue
}
if c.batch {
if _, ok := c.BatchFuncalls[item]; ok {
if exists(c.BatchFuncalls, item) {
if !c.batch {
fmt.Println("only supported in batch mode")
continue
}
if err := c.DoFuncall(item); err != nil {
fmt.Println(err)
} else {
@@ -279,12 +283,6 @@ func (c *Calc) Eval(line string) {
}
continue
}
} else {
if _, ok := c.BatchFuncalls[item]; ok {
fmt.Println("only supported in batch mode")
continue
}
}
if contains(c.LuaFunctions, item) {
// user provided custom lua functions
@@ -304,22 +302,22 @@ func (c *Calc) Eval(line string) {
}
// internal commands
if _, ok := c.Commands[item]; ok {
if exists(c.Commands, item) {
c.Commands[item].Func(c)
continue
}
if _, ok := c.ShowCommands[item]; ok {
if exists(c.ShowCommands, item) {
c.ShowCommands[item].Func(c)
continue
}
if _, ok := c.StackCommands[item]; ok {
if exists(c.StackCommands, item) {
c.StackCommands[item].Func(c)
continue
}
if _, ok := c.SettingsCommands[item]; ok {
if exists(c.SettingsCommands, item) {
c.SettingsCommands[item].Func(c)
continue
}
@@ -507,7 +505,7 @@ func (c *Calc) PutVar(name string) {
}
func (c *Calc) GetVar(name string) {
if _, ok := c.Vars[name]; ok {
if exists(c.Vars, name) {
c.Debug(fmt.Sprintf("retrieve %.2f from %s", c.Vars[name], name))
c.stack.Backup()
c.stack.Push(c.Vars[name])
@@ -520,8 +518,10 @@ func sortcommands(hash Commands) []string {
keys := make([]string, 0, len(hash))
for key := range hash {
if len(key) > 1 {
keys = append(keys, key)
}
}
sort.Strings(keys)

View File

@@ -30,7 +30,7 @@ import (
lua "github.com/yuin/gopher-lua"
)
const VERSION string = "2.0.11"
const VERSION string = "2.0.12"
const Usage string = `This is rpn, a reverse polish notation calculator cli.

16
util.go
View File

@@ -23,16 +23,24 @@ import (
"strings"
)
// find an item in a list
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
// find an item in a list, generic variant
func contains[E comparable](s []E, v E) bool {
for _, vs := range s {
if v == vs {
return true
}
}
return false
}
// look if a key in a map exists, generic variant
func exists[K comparable, V any](m map[K]V, v K) bool {
if _, ok := m[v]; ok {
return true
}
return false
}
func const2num(name string) float64 {
switch name {
case "Pi":