2022-09-26 15:25:21 +00:00
|
|
|
package cli
|
2022-03-07 14:57:42 +00:00
|
|
|
|
2022-07-31 08:23:26 +00:00
|
|
|
import (
|
2022-09-06 00:29:02 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-07-31 08:23:26 +00:00
|
|
|
cli "git.tcp.direct/Mirrors/go-prompt"
|
|
|
|
)
|
2022-03-07 14:57:42 +00:00
|
|
|
|
2022-09-06 00:29:02 +00:00
|
|
|
const (
|
|
|
|
grn = "\033[32m"
|
|
|
|
red = "\033[31m"
|
|
|
|
rst = "\033[0m"
|
|
|
|
)
|
|
|
|
|
|
|
|
type completion struct {
|
|
|
|
cli.Suggest
|
2022-09-26 15:25:21 +00:00
|
|
|
inner *ziggsCommand
|
2022-09-06 00:29:02 +00:00
|
|
|
requires map[int][]string
|
2022-09-26 17:10:17 +00:00
|
|
|
isAlias bool
|
2022-09-06 00:29:02 +00:00
|
|
|
root bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c completion) qualifies(line string) bool {
|
|
|
|
args := strings.Fields(line)
|
|
|
|
if len(line) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c.root && len(args) < 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
/*if c.root && len(args) > 0 {
|
|
|
|
return false
|
|
|
|
}*/
|
|
|
|
if len(args) < len(c.requires) {
|
|
|
|
if extraDebug {
|
|
|
|
log.Trace().Int("len(args)", len(args)).Int("len(c.requires)", len(c.requires)).
|
|
|
|
Msg(red + "len(args) < len(c.requires)" + rst)
|
2022-07-29 10:44:08 +00:00
|
|
|
}
|
2022-09-06 00:29:02 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(args)-2 > len(c.requires) {
|
|
|
|
if extraDebug {
|
|
|
|
log.Trace().Int("len(args)-2", len(args)-2).Int("len(c.requires)", len(c.requires)).
|
|
|
|
Msg(red + "len(args)-2 > len(c.requires)" + rst)
|
2022-03-07 14:57:42 +00:00
|
|
|
}
|
2022-09-06 00:29:02 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
has := func(b []string, a string) bool {
|
|
|
|
for _, r := range b {
|
|
|
|
if a == r {
|
|
|
|
return true
|
2022-03-07 14:57:42 +00:00
|
|
|
}
|
2022-09-06 00:29:02 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var count = 0
|
|
|
|
for i, a := range args {
|
|
|
|
if has(c.requires[i], a) {
|
|
|
|
if extraDebug {
|
|
|
|
log.Trace().Msgf("%v%s: found %s%v", grn, c.Text, a, rst)
|
2022-03-07 14:57:42 +00:00
|
|
|
}
|
2022-09-06 00:29:02 +00:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 15:25:21 +00:00
|
|
|
return count >= len(c.requires)
|
2022-09-06 00:29:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:10:17 +00:00
|
|
|
var suggestions map[int][]*completion
|
2022-09-06 00:29:02 +00:00
|
|
|
|
|
|
|
func init() {
|
2022-09-26 17:10:17 +00:00
|
|
|
Commands["ls"] = newZiggsCommand(cmdList, "list all lights, groups, scenes, rules, and schedules")
|
|
|
|
Commands["schedules"] = newZiggsCommand(cmdSchedules, "list schedules", "lssched", "crontab")
|
|
|
|
Commands["rules"] = newZiggsCommand(cmdRules, "list rules", "lsrule")
|
|
|
|
Commands["sensors"] = newZiggsCommand(cmdSensors, "list sensors", "lssens")
|
|
|
|
Commands["scenes"] = newZiggsCommand(cmdScenes, "list scenes", "lsscene")
|
|
|
|
Commands["lights"] = newZiggsCommand(cmdLights, "list lights", "lslight")
|
|
|
|
Commands["groups"] = newZiggsCommand(cmdGroups, "list groups", "lsgrp")
|
|
|
|
Commands["create"] = newZiggsCommand(cmdCreate, "create a new object in bridge", "new", "mk")
|
|
|
|
Commands["delete"] = newZiggsCommand(cmdDelete, "delete objects from bridges", "del", "remove")
|
|
|
|
Commands["scan"] = newZiggsCommand(cmdScan, "scan for bridges/lights/sensors", "search", "find")
|
|
|
|
Commands["rename"] = newZiggsCommand(cmdRename, "rename object in bridge", "mv")
|
|
|
|
Commands["set"] = newZiggsCommand(cmdSet, "update object properties in bridge", "update")
|
|
|
|
initCompletion()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initCompletion() {
|
|
|
|
suggestions = make(map[int][]*completion)
|
|
|
|
suggestions[0] = []*completion{
|
|
|
|
{Suggest: cli.Suggest{Text: "lights"}, inner: Commands["lights"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "groups"}, inner: Commands["groups"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "rules"}, inner: Commands["rules"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "scenes"}, inner: Commands["scenes"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "schedules"}, inner: Commands["schedules"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "sensors"}, inner: Commands["sensors"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "set"}, inner: Commands["set"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "create"}, inner: Commands["create"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "delete"}, inner: Commands["delete"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "scan"}, inner: Commands["scan"]},
|
|
|
|
{Suggest: cli.Suggest{Text: "use", Description: "select bridge to perform actions on"}},
|
2022-09-06 00:29:02 +00:00
|
|
|
{Suggest: cli.Suggest{Text: "clear", Description: "clear screen"}},
|
|
|
|
{Suggest: cli.Suggest{Text: "exit", Description: "exit ziggs"}},
|
|
|
|
}
|
2022-09-26 17:10:17 +00:00
|
|
|
|
2022-09-06 00:29:02 +00:00
|
|
|
for _, sug := range suggestions[0] {
|
|
|
|
sug.requires = map[int][]string{}
|
|
|
|
sug.root = true
|
2022-09-26 17:10:17 +00:00
|
|
|
if sug.inner != nil {
|
|
|
|
sug.Suggest.Description = sug.inner.description
|
|
|
|
}
|
|
|
|
if sug.inner != nil && len(sug.inner.aliases) > 0 {
|
|
|
|
for _, a := range sug.inner.aliases {
|
|
|
|
suggestions[0] = append(suggestions[0], &completion{
|
|
|
|
Suggest: cli.Suggest{Text: a, Description: sug.Description},
|
|
|
|
inner: sug.inner,
|
|
|
|
root: true,
|
|
|
|
isAlias: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 00:29:02 +00:00
|
|
|
}
|
2022-09-26 17:10:17 +00:00
|
|
|
|
|
|
|
suggestions[1] = []*completion{
|
2022-09-06 00:29:02 +00:00
|
|
|
{Suggest: cli.Suggest{Text: "group", Description: "target group"}},
|
|
|
|
{Suggest: cli.Suggest{Text: "light", Description: "target light"}},
|
|
|
|
}
|
|
|
|
for _, sug := range suggestions[1] {
|
2022-09-06 23:31:44 +00:00
|
|
|
sug.requires = map[int][]string{0: {"delete", "del", "set", "s"}}
|
2022-09-06 00:29:02 +00:00
|
|
|
sug.root = false
|
|
|
|
}
|
2022-09-26 17:10:17 +00:00
|
|
|
delCompletion := []*completion{
|
2022-09-06 23:31:44 +00:00
|
|
|
{Suggest: cli.Suggest{Text: "scene", Description: "target scene"}},
|
|
|
|
{Suggest: cli.Suggest{Text: "schedule", Description: "target schedule"}},
|
|
|
|
{Suggest: cli.Suggest{Text: "sensor", Description: "target sensor"}},
|
|
|
|
}
|
|
|
|
for _, sug := range delCompletion {
|
|
|
|
sug.requires = map[int][]string{0: {"delete", "del"}}
|
|
|
|
sug.root = false
|
|
|
|
}
|
|
|
|
suggestions[1] = append(suggestions[1], delCompletion...)
|
2022-09-06 00:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func completer(in cli.Document) []cli.Suggest {
|
|
|
|
c := in.CurrentLine()
|
|
|
|
infields := strings.Fields(c)
|
|
|
|
var head = len(infields) - 1
|
|
|
|
if len(infields) == 0 {
|
|
|
|
head = 0
|
|
|
|
}
|
|
|
|
var sugs []cli.Suggest
|
|
|
|
for _, sug := range suggestions[head] {
|
|
|
|
if sug.qualifies(c) && strings.HasPrefix(sug.Text, in.GetWordBeforeCursor()) {
|
|
|
|
sugs = append(sugs, sug.Suggest)
|
2022-03-07 14:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-06 00:29:02 +00:00
|
|
|
return sugs
|
2022-03-07 14:57:42 +00:00
|
|
|
}
|