mirror of https://github.com/tidwall/tile38.git
155 lines
3.2 KiB
Go
155 lines
3.2 KiB
Go
// +build ignore
|
|
|
|
package core
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
clear = "\x1b[0m"
|
|
bright = "\x1b[1m"
|
|
gray = "\x1b[90m"
|
|
yellow = "\x1b[33m"
|
|
)
|
|
|
|
type Command struct {
|
|
Name string `json:"-"`
|
|
Summary string `json:"summary"`
|
|
Complexity string `json:"complexity"`
|
|
Arguments []Argument `json:"arguments"`
|
|
Since string `json:"since"`
|
|
Group string `json:"group"`
|
|
DevOnly bool `json:"dev"`
|
|
}
|
|
|
|
func (c Command) String() string {
|
|
var s = c.Name
|
|
for _, arg := range c.Arguments {
|
|
s += " " + arg.String()
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (c Command) TermOutput(indent string) string {
|
|
line := c.String()
|
|
var line1 string
|
|
if strings.HasPrefix(line, c.Name) {
|
|
line1 = bright + c.Name + clear + gray + line[len(c.Name):] + clear
|
|
} else {
|
|
line1 = bright + strings.Replace(c.String(), " ", " "+clear+gray, 1) + clear
|
|
}
|
|
line2 := yellow + "summary: " + clear + c.Summary
|
|
//line3 := yellow + "since: " + clear + c.Since
|
|
return indent + line1 + "\n" + indent + line2 + "\n" //+ indent + line3 + "\n"
|
|
}
|
|
|
|
type EnumArg struct {
|
|
Name string `json:"name"`
|
|
Arguments []Argument `json:"arguments"`
|
|
}
|
|
|
|
func (a EnumArg) String() string {
|
|
var s = a.Name
|
|
for _, arg := range a.Arguments {
|
|
s += " " + arg.String()
|
|
}
|
|
return s
|
|
}
|
|
|
|
type Argument struct {
|
|
Command string `json:"command"`
|
|
NameAny interface{} `json:"name"`
|
|
TypeAny interface{} `json:"type"`
|
|
Optional bool `json:"optional"`
|
|
Multiple bool `json:"multiple"`
|
|
Variadic bool `json:"variadic"`
|
|
Enum []string `json:"enum"`
|
|
EnumArgs []EnumArg `json:"enumargs"`
|
|
}
|
|
|
|
func (a Argument) String() string {
|
|
var s string
|
|
if a.Command != "" {
|
|
s += " " + a.Command
|
|
}
|
|
if len(a.EnumArgs) > 0 {
|
|
eargs := ""
|
|
for _, arg := range a.EnumArgs {
|
|
v := arg.String()
|
|
if strings.Contains(v, " ") {
|
|
v = "(" + v + ")"
|
|
}
|
|
eargs += v + "|"
|
|
}
|
|
if len(eargs) > 0 {
|
|
eargs = eargs[:len(eargs)-1]
|
|
}
|
|
s += " " + eargs
|
|
} else if len(a.Enum) > 0 {
|
|
s += " " + strings.Join(a.Enum, "|")
|
|
} else {
|
|
names, _ := a.NameTypes()
|
|
subs := ""
|
|
for _, name := range names {
|
|
subs += " " + name
|
|
}
|
|
subs = strings.TrimSpace(subs)
|
|
s += " " + subs
|
|
if a.Variadic {
|
|
s += " [" + subs + " ...]"
|
|
}
|
|
if a.Multiple {
|
|
s += " ..."
|
|
}
|
|
}
|
|
s = strings.TrimSpace(s)
|
|
if a.Optional {
|
|
s = "[" + s + "]"
|
|
}
|
|
return s
|
|
}
|
|
|
|
func parseAnyStringArray(any interface{}) []string {
|
|
if str, ok := any.(string); ok {
|
|
return []string{str}
|
|
} else if any, ok := any.([]interface{}); ok {
|
|
arr := []string{}
|
|
for _, any := range any {
|
|
if str, ok := any.(string); ok {
|
|
arr = append(arr, str)
|
|
}
|
|
}
|
|
return arr
|
|
}
|
|
return []string{}
|
|
}
|
|
|
|
func (a Argument) NameTypes() (names, types []string) {
|
|
names = parseAnyStringArray(a.NameAny)
|
|
types = parseAnyStringArray(a.TypeAny)
|
|
if len(types) > len(names) {
|
|
types = types[:len(names)]
|
|
} else {
|
|
for len(types) < len(names) {
|
|
types = append(types, "")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
var Commands = func() map[string]Command {
|
|
var commands map[string]Command
|
|
if err := json.Unmarshal([]byte(commandsJSON), &commands); err != nil {
|
|
panic(err.Error())
|
|
}
|
|
for name, command := range commands {
|
|
command.Name = strings.ToUpper(name)
|
|
commands[name] = command
|
|
}
|
|
return commands
|
|
}()
|
|
|
|
var commandsJSON = `{{.CommandsJSON}}`
|