2021-03-07 16:28:21 +00:00
|
|
|
package lumecmd
|
|
|
|
|
|
|
|
import (
|
2021-03-07 16:45:56 +00:00
|
|
|
"fmt"
|
2021-03-11 02:57:05 +00:00
|
|
|
"os"
|
2021-03-07 16:45:56 +00:00
|
|
|
"time"
|
|
|
|
|
2021-03-07 16:28:21 +00:00
|
|
|
"git.kill0.net/chill9/lifx-go"
|
|
|
|
"github.com/fatih/color"
|
2021-03-11 02:57:05 +00:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2021-03-07 16:28:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ColorizePower(s string) string {
|
|
|
|
c := color.New(color.FgRed)
|
|
|
|
if s == "on" {
|
|
|
|
c = color.New(color.FgGreen)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Sprint(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ColorizeStatus(s lifx.Status) string {
|
|
|
|
c := color.New(color.FgRed)
|
|
|
|
if s == "ok" {
|
|
|
|
c = color.New(color.FgGreen)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Sprint(s)
|
|
|
|
}
|
2021-03-07 16:45:56 +00:00
|
|
|
|
|
|
|
func PrintWithIndent(indent int, s string) {
|
|
|
|
fmt.Printf("%*s%s", indent, "", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err error) {
|
|
|
|
format = fmt.Sprintf("%*s%s", indent, "", format)
|
|
|
|
return fmt.Printf(format, a...)
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) {
|
|
|
|
hdr = []string{"ID", "Location", "Group", "Label", "Last Seen", "Power"}
|
2021-03-07 16:45:56 +00:00
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
for _, l := range lights {
|
|
|
|
rows = append(rows, []string{
|
|
|
|
fmt.Sprint(l.Id),
|
|
|
|
fmt.Sprint(l.Location.Name),
|
|
|
|
fmt.Sprint(l.Group.Name),
|
|
|
|
fmt.Sprint(l.Label),
|
|
|
|
fmt.Sprint(l.LastSeen.Local().Format(time.RFC3339)),
|
|
|
|
fmt.Sprint(ColorizePower(l.Power)),
|
|
|
|
})
|
2021-03-07 16:45:56 +00:00
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
}
|
2021-03-07 16:45:56 +00:00
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-07 16:45:56 +00:00
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
func makeResultsTable(results []lifx.Result) (hdr []string, rows [][]string) {
|
|
|
|
hdr = []string{"ID", "Label", "Status"}
|
2021-03-07 16:45:56 +00:00
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
for _, r := range results {
|
|
|
|
rows = append(rows, []string{
|
|
|
|
fmt.Sprint(r.Id),
|
|
|
|
fmt.Sprint(r.Label),
|
|
|
|
fmt.Sprint(ColorizeStatus(r.Status)),
|
|
|
|
})
|
2021-03-07 16:45:56 +00:00
|
|
|
|
|
|
|
}
|
2021-03-11 02:57:05 +00:00
|
|
|
|
|
|
|
return
|
2021-03-07 16:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PrintLights(lights []lifx.Light) {
|
2021-03-11 02:57:05 +00:00
|
|
|
sortLights(lights)
|
2021-03-07 16:45:56 +00:00
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
_, rows := makeLightsTable(lights)
|
2021-03-07 16:45:56 +00:00
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
for _, v := range rows {
|
|
|
|
table.Append(v)
|
2021-03-07 16:45:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
fmt.Printf("total %d\n", len(lights))
|
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetAutoWrapText(false)
|
|
|
|
table.SetBorder(false)
|
|
|
|
table.SetCenterSeparator("")
|
|
|
|
table.SetColumnSeparator("")
|
|
|
|
table.SetHeaderLine(false)
|
|
|
|
table.SetNoWhiteSpace(true)
|
|
|
|
table.SetRowSeparator("")
|
|
|
|
table.SetTablePadding(" ")
|
|
|
|
table.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintLightsTable(lights []lifx.Light) {
|
2021-03-07 16:45:56 +00:00
|
|
|
sortLights(lights)
|
|
|
|
|
2021-03-11 02:57:05 +00:00
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
hdr, rows := makeLightsTable(lights)
|
|
|
|
|
|
|
|
for _, v := range rows {
|
|
|
|
table.Append(v)
|
2021-03-07 16:45:56 +00:00
|
|
|
}
|
2021-03-11 02:57:05 +00:00
|
|
|
|
|
|
|
table.SetHeader(hdr)
|
|
|
|
table.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintResults(results []lifx.Result) {
|
|
|
|
sortResults(results)
|
|
|
|
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
_, rows := makeResultsTable(results)
|
|
|
|
|
|
|
|
for _, v := range rows {
|
|
|
|
table.Append(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("total %d\n", len(results))
|
|
|
|
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
|
|
table.SetAutoWrapText(false)
|
|
|
|
table.SetBorder(false)
|
|
|
|
table.SetCenterSeparator("")
|
|
|
|
table.SetColumnSeparator("")
|
|
|
|
table.SetHeaderLine(false)
|
|
|
|
table.SetNoWhiteSpace(true)
|
|
|
|
table.SetRowSeparator("")
|
|
|
|
table.SetTablePadding(" ")
|
|
|
|
table.Render()
|
|
|
|
}
|
|
|
|
|
|
|
|
func PrintResultsTable(results []lifx.Result) {
|
|
|
|
sortResults(results)
|
|
|
|
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
hdr, rows := makeResultsTable(results)
|
|
|
|
|
|
|
|
for _, v := range rows {
|
|
|
|
table.Append(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
table.SetHeader(hdr)
|
|
|
|
table.Render()
|
2021-03-07 16:45:56 +00:00
|
|
|
}
|