tile38/core/commands.go

168 lines
3.8 KiB
Go
Raw Permalink Normal View History

2016-03-08 15:32:39 +03:00
// +build ignore
2016-03-05 02:08:16 +03:00
package core
import (
"encoding/json"
"strings"
)
const (
clear = "\x1b[0m"
bright = "\x1b[1m"
gray = "\x1b[90m"
yellow = "\x1b[33m"
)
2016-04-03 05:16:36 +03:00
// Command represents a Tile38 command.
2016-03-05 02:08:16 +03:00
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"`
}
2016-04-03 05:16:36 +03:00
// String returns a string representation of the command.
2016-03-05 02:08:16 +03:00
func (c Command) String() string {
var s = c.Name
for _, arg := range c.Arguments {
s += " " + arg.String()
}
return s
}
2016-04-03 05:16:36 +03:00
// TermOutput returns a string representation of the command suitable for displaying in a terminal.
2016-03-05 02:08:16 +03:00
func (c Command) TermOutput(indent string) string {
2016-03-08 15:32:39 +03:00
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
}
2016-03-05 02:08:16 +03:00
line2 := yellow + "summary: " + clear + c.Summary
2016-03-08 03:37:39 +03:00
//line3 := yellow + "since: " + clear + c.Since
return indent + line1 + "\n" + indent + line2 + "\n" //+ indent + line3 + "\n"
2016-03-05 02:08:16 +03:00
}
2016-04-03 05:16:36 +03:00
// EnumArg represents a enum arguments.
2016-03-05 02:08:16 +03:00
type EnumArg struct {
Name string `json:"name"`
2016-03-08 03:37:39 +03:00
Arguments []Argument `json:"arguments"`
2016-03-05 02:08:16 +03:00
}
2016-04-03 05:16:36 +03:00
// String returns a string representation of an EnumArg.
2016-03-05 02:08:16 +03:00
func (a EnumArg) String() string {
var s = a.Name
for _, arg := range a.Arguments {
s += " " + arg.String()
}
return s
}
2016-04-03 05:16:36 +03:00
// Argument represents a command argument.
2016-03-05 02:08:16 +03:00
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"`
}
2016-04-03 05:16:36 +03:00
// String returns a string representation of an Argument.
2016-03-05 02:08:16 +03:00
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 {
2017-08-24 20:11:16 +03:00
if len(names) == 0 {
s += " [" + subs + " ...]"
} else {
s += " [" + names[len(names)-1] + " ...]"
}
2016-03-05 02:08:16 +03:00
}
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{}
}
2016-04-03 05:16:36 +03:00
// NameTypes returns the types and names of an argument as separate arrays.
2016-03-05 02:08:16 +03:00
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
}
2016-04-03 05:16:36 +03:00
// Commands is a map of all of the commands.
2016-03-05 02:08:16 +03:00
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
}()
2016-03-08 15:32:39 +03:00
var commandsJSON = `{{.CommandsJSON}}`