From 7c7b06e89333b0dc76cf79a001f06970d95afe5f Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Fri, 27 Mar 2020 02:01:14 -0500 Subject: [PATCH] create utils module --- cmd/ls.go | 65 ------------------------------- cmd/toggle.go | 41 +------------------- cmd/util.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 105 deletions(-) create mode 100644 cmd/util.go diff --git a/cmd/ls.go b/cmd/ls.go index f94eee1..8383132 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -3,9 +3,6 @@ package lumecmd import ( "flag" "fmt" - "time" - - "git.kill0.net/chill9/go-lifx" ) var ( @@ -34,65 +31,3 @@ func LsCmd(args CmdArgs) int { PrintLights(lights) return 0 } - -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 { - fs := "\033[1;31m%s\033[0m" - if s == "on" { - fs = "\033[1;32m%s\033[0m" - } - - return fmt.Sprintf(fs, s) -} - -func calculateWidths(lights []lifx.Light) { - var length int - - for _, l := range lights { - length = len(l.Id) - if idWidth < length { - idWidth = length - } - - length = len(l.Location.Name) - if locationWidth < length { - locationWidth = length - } - - length = len(l.Group.Name) - if groupWidth < length { - groupWidth = length - } - - length = len(l.Label) - if labelWidth < length { - labelWidth = length - } - - length = len(l.LastSeen.Local().Format(time.RFC3339)) - if lastSeenWidth < length { - lastSeenWidth = length - } - - length = len(l.Power) - if powerWidth < length { - powerWidth = length - } - } -} diff --git a/cmd/toggle.go b/cmd/toggle.go index ba0be80..9d54164 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -3,8 +3,6 @@ package lumecmd import ( "flag" "fmt" - - "git.kill0.net/chill9/go-lifx" ) func init() { @@ -29,43 +27,6 @@ func ToggleCmd(args CmdArgs) int { fmt.Println(err) return 1 } - PrintResults(r) + PrintResults(r.Results) return 0 } - -func PrintResults(resp *lifx.Response) { - var length, idWidth, labelWidth, statusWidth int - - for _, r := range resp.Results { - length = len(r.Id) - if idWidth < length { - idWidth = length - } - - length = len(r.Label) - if labelWidth < length { - labelWidth = length - } - - length = len(r.Status) - if statusWidth < length { - statusWidth = length - } - } - - for _, r := range resp.Results { - fmt.Printf("%*s %*s %*s\n", - idWidth, r.Id, - labelWidth, r.Label, - statusWidth, statusColor(r.Status)) - } -} - -func statusColor(s lifx.Status) string { - fs := "\033[1;31m%s\033[0m" - if s == "ok" { - fs = "\033[1;32m%s\033[0m" - } - - return fmt.Sprintf(fs, s) -} diff --git a/cmd/util.go b/cmd/util.go new file mode 100644 index 0000000..7262642 --- /dev/null +++ b/cmd/util.go @@ -0,0 +1,103 @@ +package lumecmd + +import ( + "fmt" + "time" + + "git.kill0.net/chill9/go-lifx" +) + +func powerColor(s string) string { + fs := "\033[1;31m%s\033[0m" + if s == "on" { + fs = "\033[1;32m%s\033[0m" + } + + return fmt.Sprintf(fs, s) +} + +func statusColor(s lifx.Status) string { + fs := "\033[1;31m%s\033[0m" + if s == "ok" { + fs = "\033[1;32m%s\033[0m" + } + + return fmt.Sprintf(fs, s) +} + +func PrintResults(res []lifx.Result) { + var length, idWidth, labelWidth, statusWidth int + + for _, r := range res { + length = len(r.Id) + if idWidth < length { + idWidth = length + } + + length = len(r.Label) + if labelWidth < length { + labelWidth = length + } + + length = len(r.Status) + if statusWidth < length { + statusWidth = length + } + } + + for _, r := range res { + fmt.Printf("%*s %*s %*s\n", + idWidth, r.Id, + labelWidth, r.Label, + statusWidth, statusColor(r.Status)) + } +} + +func PrintLights(lights []lifx.Light) { + var length int + + for _, l := range lights { + length = len(l.Id) + if idWidth < length { + idWidth = length + } + + length = len(l.Location.Name) + if locationWidth < length { + locationWidth = length + } + + length = len(l.Group.Name) + if groupWidth < length { + groupWidth = length + } + + length = len(l.Label) + if labelWidth < length { + labelWidth = length + } + + length = len(l.LastSeen.Local().Format(time.RFC3339)) + if lastSeenWidth < length { + lastSeenWidth = length + } + + length = len(l.Power) + if powerWidth < length { + powerWidth = length + } + } + + 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), + ) + } +}