Compare commits

..

No commits in common. "41e6c985100bbbb33ea828f6337eabb1242ac362" and "73123d0806623712b04dfe69214e83e195df0c3b" have entirely different histories.

2 changed files with 33 additions and 59 deletions

View File

@ -4,6 +4,10 @@ import (
"flag" "flag"
) )
var (
idWidth, locationWidth, groupWidth, labelWidth, lastSeenWidth, powerWidth int
)
func init() { func init() {
var cmdName string = "ls" var cmdName string = "ls"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError) fs := flag.NewFlagSet(cmdName, flag.ExitOnError)

View File

@ -2,12 +2,11 @@ package lumecmd
import ( import (
"fmt" "fmt"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
lifx "git.kill0.net/chill9/lume" "git.kill0.net/chill9/lume"
) )
func powerColor(s string) string { func powerColor(s string) string {
@ -29,88 +28,78 @@ func statusColor(s lifx.Status) string {
} }
func PrintResults(res []lifx.Result) { func PrintResults(res []lifx.Result) {
var length int var length, idWidth, labelWidth, statusWidth int
var widths map[string]int
widths = make(map[string]int)
for _, r := range res { for _, r := range res {
length = len(r.Id) length = len(r.Id)
if widths["id"] < length { if idWidth < length {
widths["id"] = length idWidth = length
} }
length = len(r.Label) length = len(r.Label)
if widths["label"] < length { if labelWidth < length {
widths["label"] = length labelWidth = length
} }
length = len(r.Status) length = len(r.Status)
if widths["status"] < length { if statusWidth < length {
widths["status"] = length statusWidth = length
} }
} }
sortResults(res)
for _, r := range res { for _, r := range res {
fmt.Printf("%*s %*s %*s\n", fmt.Printf("%*s %*s %*s\n",
widths["id"], r.Id, idWidth, r.Id,
widths["label"], r.Label, labelWidth, r.Label,
widths["status"], statusColor(r.Status)) statusWidth, statusColor(r.Status))
} }
} }
func PrintLights(lights []lifx.Light) { func PrintLights(lights []lifx.Light) {
var length int var length int
var widths map[string]int
widths = make(map[string]int)
for _, l := range lights { for _, l := range lights {
length = len(l.Id) length = len(l.Id)
if widths["id"] < length { if idWidth < length {
widths["id"] = length idWidth = length
} }
length = len(l.Location.Name) length = len(l.Location.Name)
if widths["location"] < length { if locationWidth < length {
widths["location"] = length locationWidth = length
} }
length = len(l.Group.Name) length = len(l.Group.Name)
if widths["group"] < length { if groupWidth < length {
widths["group"] = length groupWidth = length
} }
length = len(l.Label) length = len(l.Label)
if widths["label"] < length { if labelWidth < length {
widths["label"] = length labelWidth = length
} }
length = len(l.LastSeen.Local().Format(time.RFC3339)) length = len(l.LastSeen.Local().Format(time.RFC3339))
if widths["last_seen"] < length { if lastSeenWidth < length {
widths["last_seen"] = length lastSeenWidth = length
} }
length = len(l.Power) length = len(l.Power)
if widths["power"] < length { if powerWidth < length {
widths["power"] = length powerWidth = length
} }
} }
sortLights(lights)
fmt.Printf("total %d\n", len(lights)) fmt.Printf("total %d\n", len(lights))
for _, l := range lights { for _, l := range lights {
fmt.Printf( fmt.Printf(
"%*s %*s %*s %*s %*s %-*s\n", "%*s %*s %*s %*s %*s %-*s\n",
widths["id"], l.Id, idWidth, l.Id,
widths["loction"], l.Location.Name, locationWidth, l.Location.Name,
widths["group"], l.Group.Name, groupWidth, l.Group.Name,
widths["label"], l.Label, labelWidth, l.Label,
widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339), lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339),
widths["power"], powerColor(l.Power), powerWidth, powerColor(l.Power),
) )
} }
} }
@ -132,22 +121,3 @@ func parseRGB(s string) (lifx.RGBColor, error) {
} }
return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b)) return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b))
} }
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
})
}