From 6e1863f374a65a7987c8f9007a0b7592c09fe253 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Thu, 14 Jan 2021 00:03:19 -0600 Subject: [PATCH] Add CLI output sort functions --- cmd/util.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/util.go b/cmd/util.go index a0f9add..ebba208 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -2,11 +2,12 @@ package lumecmd import ( "fmt" + "sort" "strconv" "strings" "time" - "git.kill0.net/chill9/lume" + lifx "git.kill0.net/chill9/lume" ) func powerColor(s string) string { @@ -50,6 +51,8 @@ func PrintResults(res []lifx.Result) { } } + sortResults(res) + for _, r := range res { fmt.Printf("%*s %*s %*s\n", widths["id"], r.Id, @@ -96,6 +99,8 @@ func PrintLights(lights []lifx.Light) { } } + sortLights(lights) + fmt.Printf("total %d\n", len(lights)) for _, l := range lights { fmt.Printf( @@ -127,3 +132,22 @@ func parseRGB(s string) (lifx.RGBColor, error) { } 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 + }) +}