put commands into command groups for better help, print sorted

This commit is contained in:
2023-11-12 13:49:26 +01:00
parent 8c6095ea7a
commit 2fb1c7e7ad
2 changed files with 95 additions and 21 deletions

97
calc.go
View File

@@ -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)
}
}
}

View File

@@ -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"]
}