calculate column widths with less looping
This commit is contained in:
parent
7d8e5f8b92
commit
f31fc298cd
88
cmd/lume.go
88
cmd/lume.go
@ -14,6 +14,15 @@ import (
|
|||||||
"git.kill0.net/chill9/go-lifx"
|
"git.kill0.net/chill9/go-lifx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
idWidth int = 0
|
||||||
|
locationWidth int = 0
|
||||||
|
groupWidth int = 0
|
||||||
|
labelWidth int = 0
|
||||||
|
lastSeenWidth int = 0
|
||||||
|
powerWidth int = 0
|
||||||
|
)
|
||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
}
|
}
|
||||||
@ -79,16 +88,17 @@ func main() {
|
|||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
calculateWidths(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",
|
||||||
IdLen(lights), l.Id,
|
idWidth, l.Id,
|
||||||
LocationLen(lights), l.Location.Name,
|
locationWidth, l.Location.Name,
|
||||||
GroupLen(lights), l.Group.Name,
|
groupWidth, l.Group.Name,
|
||||||
LabelLen(lights), l.Label,
|
labelWidth, l.Label,
|
||||||
LastSeenLen(lights), l.LastSeen.Local().Format(time.RFC3339),
|
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339),
|
||||||
PowerLen(lights), PowerColor(l.Power),
|
powerWidth, PowerColor(l.Power),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case "set-state":
|
case "set-state":
|
||||||
@ -169,64 +179,40 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func IdLen(lights []lifx.Light) int {
|
func calculateWidths(lights []lifx.Light) {
|
||||||
var length int
|
var length int
|
||||||
for _, l := range lights {
|
|
||||||
if len(l.Id) > length {
|
|
||||||
length = len(l.Id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return length
|
|
||||||
}
|
|
||||||
|
|
||||||
func LocationLen(lights []lifx.Light) int {
|
|
||||||
var length int
|
|
||||||
for _, l := range lights {
|
for _, l := range lights {
|
||||||
if len(l.Location.Name) > length {
|
length = len(l.Id)
|
||||||
length = len(l.Location.Name)
|
if idWidth < length {
|
||||||
|
idWidth = length
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return length
|
|
||||||
}
|
|
||||||
|
|
||||||
func GroupLen(lights []lifx.Light) int {
|
length = len(l.Location.Name)
|
||||||
var length int
|
if locationWidth < length {
|
||||||
for _, l := range lights {
|
locationWidth = length
|
||||||
if len(l.Group.Name) > length {
|
|
||||||
length = len(l.Group.Name)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return length
|
|
||||||
}
|
|
||||||
|
|
||||||
func LabelLen(lights []lifx.Light) int {
|
length = len(l.Group.Name)
|
||||||
var length int
|
if groupWidth < length {
|
||||||
for _, l := range lights {
|
groupWidth = length
|
||||||
if len(l.Label) > length {
|
|
||||||
length = len(l.Label)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return length
|
|
||||||
}
|
|
||||||
|
|
||||||
func LastSeenLen(lights []lifx.Light) int {
|
length = len(l.Label)
|
||||||
var length int
|
if labelWidth < length {
|
||||||
for _, l := range lights {
|
labelWidth = length
|
||||||
if len(l.LastSeen.Local().Format(time.RFC3339)) > length {
|
|
||||||
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return length
|
|
||||||
}
|
|
||||||
|
|
||||||
func PowerLen(lights []lifx.Light) int {
|
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
||||||
var length int
|
if lastSeenWidth < length {
|
||||||
for _, l := range lights {
|
lastSeenWidth = length
|
||||||
if len(l.Power) > length {
|
}
|
||||||
length = len(l.Power)
|
|
||||||
|
length = len(l.Power)
|
||||||
|
if powerWidth < length {
|
||||||
|
powerWidth = length
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return length
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func PowerColor(s string) string {
|
func PowerColor(s string) string {
|
||||||
|
Loading…
Reference in New Issue
Block a user