mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 04:21:01 +01:00
put commands into command groups for better help, print sorted
This commit is contained in:
97
calc.go
97
calc.go
@@ -21,6 +21,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -46,7 +47,12 @@ type Calc struct {
|
|||||||
|
|
||||||
Funcalls Funcalls
|
Funcalls Funcalls
|
||||||
BatchFuncalls 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
|
Vars map[string]float64
|
||||||
}
|
}
|
||||||
@@ -112,6 +118,18 @@ func (c *Calc) GetCompleteCustomFuncalls() func(string) []string {
|
|||||||
completions = append(completions, function)
|
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 {
|
for command := range c.Commands {
|
||||||
completions = append(completions, command)
|
completions = append(completions, command)
|
||||||
}
|
}
|
||||||
@@ -266,27 +284,32 @@ func (c *Calc) Eval(line string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// management commands
|
// internal commands
|
||||||
if _, ok := c.Commands[item]; ok {
|
if _, ok := c.Commands[item]; ok {
|
||||||
c.Commands[item].Func(c)
|
c.Commands[item].Func(c)
|
||||||
continue
|
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 {
|
switch item {
|
||||||
case "?":
|
case "?":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "help":
|
case "help":
|
||||||
fmt.Println("Available commands:")
|
c.PrintHelp()
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Println("unknown command or operator!")
|
fmt.Println("unknown command or operator!")
|
||||||
@@ -473,3 +496,51 @@ func (c *Calc) GetVar(name string) {
|
|||||||
fmt.Println("variable doesn't exist")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
19
command.go
19
command.go
@@ -40,7 +40,7 @@ func NewCommand(help string, function CommandFunction) *Command {
|
|||||||
|
|
||||||
// define all management (that is: non calculation) commands
|
// define all management (that is: non calculation) commands
|
||||||
func (c *Calc) SetCommands() {
|
func (c *Calc) SetCommands() {
|
||||||
f := Commands{
|
c.SettingsCommands = Commands{
|
||||||
// Toggles
|
// Toggles
|
||||||
"debug": NewCommand(
|
"debug": NewCommand(
|
||||||
"toggle debugging",
|
"toggle debugging",
|
||||||
@@ -84,7 +84,9 @@ func (c *Calc) SetCommands() {
|
|||||||
c.showstack = false
|
c.showstack = false
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
c.ShowCommands = Commands{
|
||||||
// Display commands
|
// Display commands
|
||||||
"dump": NewCommand(
|
"dump": NewCommand(
|
||||||
"display the stack contents",
|
"display the stack contents",
|
||||||
@@ -115,8 +117,9 @@ func (c *Calc) SetCommands() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
}
|
||||||
|
|
||||||
// stack manipulation commands
|
c.StackCommands = Commands{
|
||||||
"clear": NewCommand(
|
"clear": NewCommand(
|
||||||
"clear the whole stack",
|
"clear the whole stack",
|
||||||
func(c *Calc) {
|
func(c *Calc) {
|
||||||
@@ -172,8 +175,10 @@ func (c *Calc) SetCommands() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
}
|
||||||
|
|
||||||
// general commands
|
// general commands
|
||||||
|
c.Commands = Commands{
|
||||||
"exit": NewCommand(
|
"exit": NewCommand(
|
||||||
"exit program",
|
"exit program",
|
||||||
func(c *Calc) {
|
func(c *Calc) {
|
||||||
@@ -190,9 +195,7 @@ func (c *Calc) SetCommands() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// aliases
|
// aliases
|
||||||
f["quit"] = f["exit"]
|
c.Commands["quit"] = c.Commands["exit"]
|
||||||
f["undebug"] = f["nodebug"]
|
c.SettingsCommands["undebug"] = c.SettingsCommands["nodebug"]
|
||||||
f["show"] = f["showstack"]
|
c.SettingsCommands["show"] = c.SettingsCommands["showstack"]
|
||||||
|
|
||||||
c.Commands = f
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user