diff --git a/calc.go b/calc.go index 8ce89f1..b32cc9f 100644 --- a/calc.go +++ b/calc.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "regexp" + "sort" "strconv" "strings" @@ -46,7 +47,12 @@ type Calc struct { Funcalls Funcalls BatchFuncalls Funcalls - Commands Commands + + // different kinds of commands, displays nicer in help output + StackCommands Commands + SettingsCommands Commands + ShowCommands Commands + Commands Commands Vars map[string]float64 } @@ -112,6 +118,18 @@ func (c *Calc) GetCompleteCustomFuncalls() func(string) []string { completions = append(completions, function) } + for command := range c.SettingsCommands { + completions = append(completions, command) + } + + for command := range c.ShowCommands { + completions = append(completions, command) + } + + for command := range c.StackCommands { + completions = append(completions, command) + } + for command := range c.Commands { completions = append(completions, command) } @@ -266,27 +284,32 @@ func (c *Calc) Eval(line string) { continue } - // management commands + // internal commands if _, ok := c.Commands[item]; ok { c.Commands[item].Func(c) continue } + if _, ok := c.ShowCommands[item]; ok { + c.ShowCommands[item].Func(c) + continue + } + + if _, ok := c.StackCommands[item]; ok { + c.StackCommands[item].Func(c) + continue + } + + if _, ok := c.SettingsCommands[item]; ok { + c.SettingsCommands[item].Func(c) + continue + } + switch item { case "?": fallthrough case "help": - fmt.Println("Available commands:") - for name, command := range c.Commands { - fmt.Printf("%-20s %s\n", name, command.Help) - } - fmt.Println(Help) - if len(LuaFuncs) > 0 { - fmt.Println("Lua functions:") - for name, function := range LuaFuncs { - fmt.Printf("%-20s %s\n", name, function.help) - } - } + c.PrintHelp() default: fmt.Println("unknown command or operator!") @@ -473,3 +496,51 @@ func (c *Calc) GetVar(name string) { fmt.Println("variable doesn't exist") } } + +func sortcommands(hash Commands) []string { + keys := make([]string, 0, len(hash)) + + for key := range hash { + keys = append(keys, key) + } + + sort.Strings(keys) + + return keys +} + +func (c *Calc) PrintHelp() { + fmt.Println("Available configuration commands:") + for _, name := range sortcommands(c.SettingsCommands) { + fmt.Printf("%-20s %s\n", name, c.SettingsCommands[name].Help) + } + fmt.Println() + + fmt.Println("Available show commands:") + for _, name := range sortcommands(c.ShowCommands) { + fmt.Printf("%-20s %s\n", name, c.ShowCommands[name].Help) + } + fmt.Println() + + fmt.Println("Available stack manipulation commands:") + for _, name := range sortcommands(c.StackCommands) { + fmt.Printf("%-20s %s\n", name, c.StackCommands[name].Help) + } + fmt.Println() + + fmt.Println("Other commands:") + for _, name := range sortcommands(c.Commands) { + fmt.Printf("%-20s %s\n", name, c.Commands[name].Help) + } + fmt.Println() + + fmt.Println(Help) + + // append lua functions, if any + if len(LuaFuncs) > 0 { + fmt.Println("Lua functions:") + for name, function := range LuaFuncs { + fmt.Printf("%-20s %s\n", name, function.help) + } + } +} diff --git a/command.go b/command.go index 078499f..69328c5 100644 --- a/command.go +++ b/command.go @@ -40,7 +40,7 @@ func NewCommand(help string, function CommandFunction) *Command { // define all management (that is: non calculation) commands func (c *Calc) SetCommands() { - f := Commands{ + c.SettingsCommands = Commands{ // Toggles "debug": NewCommand( "toggle debugging", @@ -84,7 +84,9 @@ func (c *Calc) SetCommands() { c.showstack = false }, ), + } + c.ShowCommands = Commands{ // Display commands "dump": NewCommand( "display the stack contents", @@ -115,8 +117,9 @@ func (c *Calc) SetCommands() { } }, ), + } - // stack manipulation commands + c.StackCommands = Commands{ "clear": NewCommand( "clear the whole stack", func(c *Calc) { @@ -172,8 +175,10 @@ func (c *Calc) SetCommands() { } }, ), + } - // general commands + // general commands + c.Commands = Commands{ "exit": NewCommand( "exit program", func(c *Calc) { @@ -190,9 +195,7 @@ func (c *Calc) SetCommands() { } // aliases - f["quit"] = f["exit"] - f["undebug"] = f["nodebug"] - f["show"] = f["showstack"] - - c.Commands = f + c.Commands["quit"] = c.Commands["exit"] + c.SettingsCommands["undebug"] = c.SettingsCommands["nodebug"] + c.SettingsCommands["show"] = c.SettingsCommands["showstack"] }