Use a map to track column widths
This commit is contained in:
parent
73123d0806
commit
1161418104
@ -4,10 +4,6 @@ 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)
|
||||||
|
62
cmd/util.go
62
cmd/util.go
@ -28,65 +28,71 @@ func statusColor(s lifx.Status) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func PrintResults(res []lifx.Result) {
|
func PrintResults(res []lifx.Result) {
|
||||||
var length, idWidth, labelWidth, statusWidth int
|
var length 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 idWidth < length {
|
if widths["id"] < length {
|
||||||
idWidth = length
|
widths["id"] = length
|
||||||
}
|
}
|
||||||
|
|
||||||
length = len(r.Label)
|
length = len(r.Label)
|
||||||
if labelWidth < length {
|
if widths["label"] < length {
|
||||||
labelWidth = length
|
widths["label"] = length
|
||||||
}
|
}
|
||||||
|
|
||||||
length = len(r.Status)
|
length = len(r.Status)
|
||||||
if statusWidth < length {
|
if widths["status"] < length {
|
||||||
statusWidth = length
|
widths["status"] = length
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range res {
|
for _, r := range res {
|
||||||
fmt.Printf("%*s %*s %*s\n",
|
fmt.Printf("%*s %*s %*s\n",
|
||||||
idWidth, r.Id,
|
widths["id"], r.Id,
|
||||||
labelWidth, r.Label,
|
widths["label"], r.Label,
|
||||||
statusWidth, statusColor(r.Status))
|
widths["status"], 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 idWidth < length {
|
if widths["id"] < length {
|
||||||
idWidth = length
|
widths["id"] = length
|
||||||
}
|
}
|
||||||
|
|
||||||
length = len(l.Location.Name)
|
length = len(l.Location.Name)
|
||||||
if locationWidth < length {
|
if widths["location"] < length {
|
||||||
locationWidth = length
|
widths["location"] = length
|
||||||
}
|
}
|
||||||
|
|
||||||
length = len(l.Group.Name)
|
length = len(l.Group.Name)
|
||||||
if groupWidth < length {
|
if widths["group"] < length {
|
||||||
groupWidth = length
|
widths["group"] = length
|
||||||
}
|
}
|
||||||
|
|
||||||
length = len(l.Label)
|
length = len(l.Label)
|
||||||
if labelWidth < length {
|
if widths["label"] < length {
|
||||||
labelWidth = length
|
widths["label"] = length
|
||||||
}
|
}
|
||||||
|
|
||||||
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
||||||
if lastSeenWidth < length {
|
if widths["last_seen"] < length {
|
||||||
lastSeenWidth = length
|
widths["last_seen"] = length
|
||||||
}
|
}
|
||||||
|
|
||||||
length = len(l.Power)
|
length = len(l.Power)
|
||||||
if powerWidth < length {
|
if widths["power"] < length {
|
||||||
powerWidth = length
|
widths["power"] = length
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,12 +100,12 @@ func PrintLights(lights []lifx.Light) {
|
|||||||
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",
|
||||||
idWidth, l.Id,
|
widths["id"], l.Id,
|
||||||
locationWidth, l.Location.Name,
|
widths["loction"], l.Location.Name,
|
||||||
groupWidth, l.Group.Name,
|
widths["group"], l.Group.Name,
|
||||||
labelWidth, l.Label,
|
widths["label"], l.Label,
|
||||||
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339),
|
widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
|
||||||
powerWidth, powerColor(l.Power),
|
widths["power"], powerColor(l.Power),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user