2020-03-27 07:01:14 +00:00
|
|
|
package lumecmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-21 05:09:58 +00:00
|
|
|
"os"
|
2021-01-14 06:03:19 +00:00
|
|
|
"sort"
|
2020-03-28 23:04:53 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-03-27 07:01:14 +00:00
|
|
|
"time"
|
|
|
|
|
2021-02-15 01:02:26 +00:00
|
|
|
"git.kill0.net/chill9/lifx-go"
|
2020-03-27 07:01:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func powerColor(s string) string {
|
|
|
|
fs := "\033[1;31m%s\033[0m"
|
|
|
|
if s == "on" {
|
|
|
|
fs = "\033[1;32m%s\033[0m"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(fs, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func statusColor(s lifx.Status) string {
|
|
|
|
fs := "\033[1;31m%s\033[0m"
|
|
|
|
if s == "ok" {
|
|
|
|
fs = "\033[1;32m%s\033[0m"
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(fs, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintResults(res []lifx.Result) {
|
2021-01-14 06:02:43 +00:00
|
|
|
var length int
|
|
|
|
var widths map[string]int
|
|
|
|
|
|
|
|
widths = make(map[string]int)
|
2020-03-27 07:01:14 +00:00
|
|
|
|
|
|
|
for _, r := range res {
|
|
|
|
length = len(r.Id)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["id"] < length {
|
|
|
|
widths["id"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = len(r.Label)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["label"] < length {
|
|
|
|
widths["label"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = len(r.Status)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["status"] < length {
|
|
|
|
widths["status"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:03:19 +00:00
|
|
|
sortResults(res)
|
|
|
|
|
2020-03-27 07:01:14 +00:00
|
|
|
for _, r := range res {
|
|
|
|
fmt.Printf("%*s %*s %*s\n",
|
2021-01-14 06:02:43 +00:00
|
|
|
widths["id"], r.Id,
|
|
|
|
widths["label"], r.Label,
|
|
|
|
widths["status"], statusColor(r.Status))
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintLights(lights []lifx.Light) {
|
|
|
|
var length int
|
2021-01-14 06:02:43 +00:00
|
|
|
var widths map[string]int
|
|
|
|
|
|
|
|
widths = make(map[string]int)
|
2020-03-27 07:01:14 +00:00
|
|
|
|
|
|
|
for _, l := range lights {
|
|
|
|
length = len(l.Id)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["id"] < length {
|
|
|
|
widths["id"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = len(l.Location.Name)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["location"] < length {
|
|
|
|
widths["location"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = len(l.Group.Name)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["group"] < length {
|
|
|
|
widths["group"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = len(l.Label)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["label"] < length {
|
|
|
|
widths["label"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["last_seen"] < length {
|
|
|
|
widths["last_seen"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
length = len(l.Power)
|
2021-01-14 06:02:43 +00:00
|
|
|
if widths["power"] < length {
|
|
|
|
widths["power"] = length
|
2020-03-27 07:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 06:03:19 +00:00
|
|
|
sortLights(lights)
|
|
|
|
|
2020-03-27 07:01:14 +00:00
|
|
|
fmt.Printf("total %d\n", len(lights))
|
|
|
|
for _, l := range lights {
|
|
|
|
fmt.Printf(
|
|
|
|
"%*s %*s %*s %*s %*s %-*s\n",
|
2021-01-14 06:02:43 +00:00
|
|
|
widths["id"], l.Id,
|
|
|
|
widths["loction"], l.Location.Name,
|
|
|
|
widths["group"], l.Group.Name,
|
|
|
|
widths["label"], l.Label,
|
|
|
|
widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
|
|
|
|
widths["power"], powerColor(l.Power),
|
2020-03-27 07:01:14 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 23:04:53 +00:00
|
|
|
|
|
|
|
func parseRGB(s string) (lifx.RGBColor, error) {
|
|
|
|
var c lifx.RGBColor
|
|
|
|
rgb := strings.SplitN(s, ",", 3)
|
|
|
|
r, err := strconv.ParseUint(rgb[0], 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
g, err := strconv.ParseUint(rgb[1], 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
b, err := strconv.ParseUint(rgb[2], 10, 8)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b))
|
|
|
|
}
|
2021-01-14 06:03:19 +00:00
|
|
|
|
|
|
|
func sortLights(lights []lifx.Light) {
|
|
|
|
sort.Slice(lights, func(i, j int) bool {
|
|
|
|
if lights[i].Group.Name < lights[j].Group.Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if lights[i].Group.Name > lights[j].Group.Name {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return lights[i].Label < lights[j].Label
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortResults(res []lifx.Result) {
|
|
|
|
sort.Slice(res, func(i, j int) bool {
|
|
|
|
return res[i].Label < res[j].Label
|
|
|
|
})
|
|
|
|
}
|
2021-01-21 05:09:58 +00:00
|
|
|
|
|
|
|
func ExitWithCode(code int, err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
}
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
2021-02-15 05:06:12 +00:00
|
|
|
|
|
|
|
func YesNo(v bool) string {
|
|
|
|
if v {
|
|
|
|
return "yes"
|
|
|
|
}
|
|
|
|
return "no"
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintWithIndent(indent int, s string) {
|
|
|
|
fmt.Printf("%*s%s", indent, "", s)
|
|
|
|
}
|