move ls printing into separate function

This commit is contained in:
Ryan Cavicchioni 2020-03-24 01:35:18 -05:00
parent f31fc298cd
commit 25172da555
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

View File

@ -88,19 +88,7 @@ func main() {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
calculateWidths(lights) PrintLights(lights)
fmt.Printf("total %d\n", len(lights))
for _, l := range lights {
fmt.Printf(
"%*s %*s %*s %*s %*s %-*s\n",
idWidth, l.Id,
locationWidth, l.Location.Name,
groupWidth, l.Group.Name,
labelWidth, l.Label,
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339),
powerWidth, PowerColor(l.Power),
)
}
case "set-state": case "set-state":
setStateCommand.Parse(os.Args[4:]) setStateCommand.Parse(os.Args[4:])
@ -215,6 +203,23 @@ func calculateWidths(lights []lifx.Light) {
} }
} }
func PrintLights(lights []lifx.Light) {
calculateWidths(lights)
fmt.Printf("total %d\n", len(lights))
for _, l := range lights {
fmt.Printf(
"%*s %*s %*s %*s %*s %-*s\n",
idWidth, l.Id,
locationWidth, l.Location.Name,
groupWidth, l.Group.Name,
labelWidth, l.Label,
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339),
powerWidth, PowerColor(l.Power),
)
}
}
func PowerColor(s string) string { func PowerColor(s string) string {
color := fc.New(fc.FgRed).SprintFunc() color := fc.New(fc.FgRed).SprintFunc()
if s == "on" { if s == "on" {