Compare commits

..

No commits in common. "dd1ea7c27625208c25a5014994204df5d8255da9" and "23210af0c3277b9beda64f606ddcf1e57db4b8bd" have entirely different histories.

14 changed files with 66 additions and 124 deletions

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -103,7 +102,7 @@ func BreatheCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Results(r.Results)) p.Results(r.Results)
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -3,7 +3,6 @@ package lumecmd
import ( import (
"fmt" "fmt"
"sort" "sort"
"strings"
) )
func NewCmdHelp() Command { func NewCmdHelp() Command {
@ -17,21 +16,15 @@ func NewCmdHelp() Command {
func HelpCmd(ctx Context) (int, error) { func HelpCmd(ctx Context) (int, error) {
if len(ctx.Args) == 0 { if len(ctx.Args) == 0 {
fmt.Print(printHelp(commandRegistry)) printHelp(commandRegistry)
} else if len(ctx.Args) >= 1 { } else if len(ctx.Args) >= 1 {
if cmdHelp, err := printCmdHelp(ctx.Args[0]); err == nil { printCmdHelp(ctx.Args[0])
fmt.Print(cmdHelp)
} else {
fmt.Print(err)
}
} }
return ExitSuccess, nil return ExitSuccess, nil
} }
func printHelp(commands map[string]Command) string { func printHelp(commands map[string]Command) {
var b strings.Builder
var maxLen, cmdLen int var maxLen, cmdLen int
var keys []string var keys []string
@ -43,45 +36,34 @@ func printHelp(commands map[string]Command) string {
} }
} }
fmt.Fprintf(&b, "usage:\n lume <command> [<args...>]") fmt.Printf("usage:\n lume <command> [<args...>]")
fmt.Fprintln(&b) fmt.Println()
fmt.Fprintln(&b, "\ncommands:") fmt.Println("\ncommands:")
sort.Strings(keys) sort.Strings(keys)
for _, k := range keys { for _, k := range keys {
c := commands[k] c := commands[k]
fmt.Fprintf(&b, " %-*s %s\n", maxLen, c.Name, c.Short) fmt.Printf(" %-*s %s\n", maxLen, c.Name, c.Short)
}
} }
return b.String() func printCmdHelp(name string) error {
}
func printCmdHelp(name string) (string, error) {
var b strings.Builder
subCmd, ok := commandRegistry[name] subCmd, ok := commandRegistry[name]
if !ok { if !ok {
return "", fmt.Errorf("unknown commnnd: %s\n", name) return fmt.Errorf("unknown commnnd: %s\n", name)
} }
if subCmd.Use != "" { if subCmd.Use != "" {
fmt.Fprintf(&b, "usage:\n lume %s %s\n", subCmd.Name, subCmd.Use) fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use)
} else { } else {
fmt.Fprintf(&b, "usage:\n lume %s\n", subCmd.Name) fmt.Printf("usage:\n lume %s\n", subCmd.Name)
} }
if subCmd.Flags != nil { if subCmd.Flags != nil {
out := subCmd.Flags.Output() fmt.Println()
defer subCmd.Flags.SetOutput(out) fmt.Print("flags:\n")
fmt.Fprintln(&b)
fmt.Fprint(&b, "flags:\n")
subCmd.Flags.SetOutput(&b)
subCmd.Flags.PrintDefaults() subCmd.Flags.PrintDefaults()
} }
return b.String(), nil return nil
} }

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
) )
func NewCmdLs() Command { func NewCmdLs() Command {
@ -43,7 +42,7 @@ func LsCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Lights(lights)) p.Lights(lights)
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -49,7 +48,7 @@ func PoweroffCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Results(r.Results)) p.Results(r.Results)
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -49,7 +48,7 @@ func PoweronCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Results(r.Results)) p.Results(r.Results)
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -2,8 +2,7 @@ package lumecmd
import ( import (
"fmt" "fmt"
"io" "os"
"strings"
"time" "time"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
@ -12,8 +11,8 @@ import (
) )
type Printer interface { type Printer interface {
Results(results []lifx.Result) string Results(results []lifx.Result)
Lights(lights []lifx.Light) string Lights(lights []lifx.Light)
} }
type defaultPrinter struct{} type defaultPrinter struct{}
@ -29,19 +28,17 @@ func NewPrinter(format string) Printer {
} }
} }
func (dp *defaultPrinter) Results(results []lifx.Result) string { func (dp *defaultPrinter) Results(results []lifx.Result) {
var b strings.Builder
sortResults(results) sortResults(results)
table := tablewriter.NewWriter(&b) table := tablewriter.NewWriter(os.Stdout)
_, rows := makeResultsTable(results) _, rows := makeResultsTable(results)
for _, v := range rows { for _, v := range rows {
table.Append(v) table.Append(v)
} }
fmt.Fprintf(&b, "total %d\n", len(results)) fmt.Printf("total %d\n", len(results))
table.SetAlignment(tablewriter.ALIGN_LEFT) table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false) table.SetAutoWrapText(false)
table.SetBorder(false) table.SetBorder(false)
@ -52,16 +49,12 @@ func (dp *defaultPrinter) Results(results []lifx.Result) string {
table.SetRowSeparator("") table.SetRowSeparator("")
table.SetTablePadding(" ") table.SetTablePadding(" ")
table.Render() table.Render()
return b.String()
} }
func (tp *tablePrinter) Results(results []lifx.Result) string { func (tp *tablePrinter) Results(results []lifx.Result) {
var b strings.Builder
sortResults(results) sortResults(results)
table := tablewriter.NewWriter(&b) table := tablewriter.NewWriter(os.Stdout)
hdr, rows := makeResultsTable(results) hdr, rows := makeResultsTable(results)
for _, v := range rows { for _, v := range rows {
@ -70,23 +63,19 @@ func (tp *tablePrinter) Results(results []lifx.Result) string {
table.SetHeader(hdr) table.SetHeader(hdr)
table.Render() table.Render()
return b.String()
} }
func (dp *defaultPrinter) Lights(lights []lifx.Light) string { func (dp *defaultPrinter) Lights(lights []lifx.Light) {
var b strings.Builder
sortLights(lights) sortLights(lights)
table := tablewriter.NewWriter(&b) table := tablewriter.NewWriter(os.Stdout)
_, rows := makeLightsTable(lights) _, rows := makeLightsTable(lights)
for _, v := range rows { for _, v := range rows {
table.Append(v) table.Append(v)
} }
fmt.Fprintf(&b, "total %d\n", len(lights)) fmt.Printf("total %d\n", len(lights))
table.SetAlignment(tablewriter.ALIGN_LEFT) table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false) table.SetAutoWrapText(false)
table.SetBorder(false) table.SetBorder(false)
@ -97,16 +86,12 @@ func (dp *defaultPrinter) Lights(lights []lifx.Light) string {
table.SetRowSeparator("") table.SetRowSeparator("")
table.SetTablePadding(" ") table.SetTablePadding(" ")
table.Render() table.Render()
return b.String()
} }
func (tp *tablePrinter) Lights(lights []lifx.Light) string { func (tp *tablePrinter) Lights(lights []lifx.Light) {
var b strings.Builder
sortLights(lights) sortLights(lights)
table := tablewriter.NewWriter(&b) table := tablewriter.NewWriter(os.Stdout)
hdr, rows := makeLightsTable(lights) hdr, rows := makeLightsTable(lights)
for _, v := range rows { for _, v := range rows {
@ -115,8 +100,6 @@ func (tp *tablePrinter) Lights(lights []lifx.Light) string {
table.SetHeader(hdr) table.SetHeader(hdr)
table.Render() table.Render()
return b.String()
} }
func ColorizeIndicator(s string) string { func ColorizeIndicator(s string) string {
@ -155,11 +138,6 @@ func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err e
return fmt.Printf(format, a...) return fmt.Printf(format, a...)
} }
func FprintfWithIndent(w io.Writer, indent int, format string, a ...interface{}) (n int, err error) {
format = fmt.Sprintf("%*s%s", indent, "", format)
return fmt.Fprintf(w, format, a...)
}
func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) { func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) {
hdr = []string{"", "ID", "Location", "Group", "Label", "Last Seen", "Power"} hdr = []string{"", "ID", "Location", "Group", "Label", "Last Seen", "Power"}

View File

@ -130,7 +130,7 @@ func SetColorCmd(ctx Context) (int, error) {
if !fast { if !fast {
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Results(r.Results)) p.Results(r.Results)
} }
return ExitSuccess, nil return ExitSuccess, nil

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -97,7 +96,7 @@ func SetStateCmd(ctx Context) (int, error) {
if !fast { if !fast {
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Results(r.Results)) p.Results(r.Results)
} }
return ExitSuccess, nil return ExitSuccess, nil

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -115,7 +114,7 @@ func SetWhiteCmd(ctx Context) (int, error) {
if !fast { if !fast {
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Results(r.Results)) p.Results(r.Results)
} }
return ExitSuccess, nil return ExitSuccess, nil

View File

@ -3,7 +3,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt" "fmt"
"strings"
) )
const Tabstop int = 2 const Tabstop int = 2
@ -27,8 +26,6 @@ func NewCmdShow() Command {
func ShowCmd(ctx Context) (int, error) { func ShowCmd(ctx Context) (int, error) {
var indent int var indent int
var b strings.Builder
c := ctx.Client c := ctx.Client
selector := ctx.Flags.String("selector") selector := ctx.Flags.String("selector")
lights, err := c.ListLights(selector) lights, err := c.ListLights(selector)
@ -41,8 +38,7 @@ func ShowCmd(ctx Context) (int, error) {
for i, l := range lights { for i, l := range lights {
indent = 0 indent = 0
fmt.Fprintf( fmt.Printf(
&b,
"%s Light ID: %s, %s, Power: %s\n", "%s Light ID: %s, %s, Power: %s\n",
ColorizeIndicator(l.Power), ColorizeIndicator(l.Power),
l.Id, l.Id,
@ -50,43 +46,41 @@ func ShowCmd(ctx Context) (int, error) {
ColorizePower(l.Power), ColorizePower(l.Power),
) )
indent += Tabstop + 2 indent += Tabstop + 2
FprintfWithIndent(&b, indent, "Label: %s, ID: %s\n", l.Label, l.Id) PrintfWithIndent(indent, "Label: %s, ID: %s\n", l.Label, l.Id)
FprintfWithIndent(&b, indent, "UUID: %s\n", l.UUID) PrintfWithIndent(indent, "UUID: %s\n", l.UUID)
FprintfWithIndent(&b, indent, "Location: %s, ID: %s\n", l.Location.Name, l.Location.Id) PrintfWithIndent(indent, "Location: %s, ID: %s\n", l.Location.Name, l.Location.Id)
FprintfWithIndent(&b, indent, "Group: %s, ID: %s\n", l.Group.Name, l.Group.Id) PrintfWithIndent(indent, "Group: %s, ID: %s\n", l.Group.Name, l.Group.Id)
FprintfWithIndent(&b, indent, "Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n", PrintfWithIndent(indent, "Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n",
*l.Color.H, *l.Color.S, *l.Color.K) *l.Color.H, *l.Color.S, *l.Color.K)
FprintfWithIndent(&b, indent, "Brightness: %.1f%%\n", l.Brightness*100) PrintfWithIndent(indent, "Brightness: %.1f%%\n", l.Brightness*100)
if l.Effect != "" { if l.Effect != "" {
FprintfWithIndent(&b, indent, "Effect: %s\n", l.Effect) PrintfWithIndent(indent, "Effect: %s\n", l.Effect)
} }
FprintfWithIndent(&b, indent, "Product: %s\n", l.Product.Name) PrintfWithIndent(indent, "Product: %s\n", l.Product.Name)
FprintfWithIndent(&b, indent, "Capabilities: ") PrintfWithIndent(indent, "Capabilities: ")
fmt.Fprintf(&b, "Color: %s, ", YesNo(l.Product.Capabilities.HasColor)) fmt.Printf("Color: %s, ", YesNo(l.Product.Capabilities.HasColor))
fmt.Fprintf(&b, "Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp)) fmt.Printf("Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp))
fmt.Fprintf(&b, "IR: %s, ", YesNo(l.Product.Capabilities.HasIR)) fmt.Printf("IR: %s, ", YesNo(l.Product.Capabilities.HasIR))
fmt.Fprintf(&b, "Chain: %s, ", YesNo(l.Product.Capabilities.HasChain)) fmt.Printf("Chain: %s, ", YesNo(l.Product.Capabilities.HasChain))
fmt.Fprintf(&b, "Multizone: %s, ", YesNo(l.Product.Capabilities.HasMultizone)) fmt.Printf("Multizone: %s, ", YesNo(l.Product.Capabilities.HasMultizone))
fmt.Fprintf(&b, "Min Kelvin: %.1f, ", l.Product.Capabilities.MinKelvin) fmt.Printf("Min Kelvin: %.1f, ", l.Product.Capabilities.MinKelvin)
fmt.Fprintf(&b, "Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin) fmt.Printf("Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin)
fmt.Fprintln(&b) fmt.Println()
// List applicable selectors (most to least specific) // List applicable selectors (most to least specific)
FprintfWithIndent(&b, indent, "Selectors:\n") PrintfWithIndent(indent, "Selectors:\n")
indent += Tabstop indent += Tabstop
FprintfWithIndent(&b, indent, "id:%s\n", l.Id) PrintfWithIndent(indent, "id:%s\n", l.Id)
FprintfWithIndent(&b, indent, "label:%s\n", l.Label) PrintfWithIndent(indent, "label:%s\n", l.Label)
FprintfWithIndent(&b, indent, "group_id:%s\n", l.Group.Id) PrintfWithIndent(indent, "group_id:%s\n", l.Group.Id)
FprintfWithIndent(&b, indent, "group:%s\n", l.Group.Name) PrintfWithIndent(indent, "group:%s\n", l.Group.Name)
FprintfWithIndent(&b, indent, "location_id:%s\n", l.Location.Id) PrintfWithIndent(indent, "location_id:%s\n", l.Location.Id)
FprintfWithIndent(&b, indent, "location:%s\n", l.Location.Name) PrintfWithIndent(indent, "location:%s\n", l.Location.Name)
indent -= Tabstop indent -= Tabstop
FprintfWithIndent(&b, indent, "Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen) PrintfWithIndent(indent, "Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen)
if i < len(lights)-1 { if i < len(lights)-1 {
fmt.Fprintln(&b) fmt.Println()
} }
fmt.Print(b.String())
} }
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -2,7 +2,6 @@ package lumecmd
import ( import (
"flag" "flag"
"fmt"
) )
func NewCmdToggle() Command { func NewCmdToggle() Command {
@ -46,7 +45,7 @@ func ToggleCmd(ctx Context) (int, error) {
} }
p = NewPrinter(format) p = NewPrinter(format)
fmt.Print(p.Results(r.Results)) p.Results(r.Results)
return ExitSuccess, nil return ExitSuccess, nil
} }

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"strings"
"git.kill0.net/chill9/lifx-go" "git.kill0.net/chill9/lifx-go"
) )
@ -24,11 +23,10 @@ func NewCmdValidate() Command {
} }
func ValidateCmd(ctx Context) (int, error) { func ValidateCmd(ctx Context) (int, error) {
var b strings.Builder
c := ctx.Client c := ctx.Client
if len(ctx.Args) != 1 { if len(ctx.Args) != 1 {
fmt.Print(printCmdHelp(ctx.Name)) printCmdHelp(ctx.Name)
return ExitFailure, nil return ExitFailure, nil
} }
@ -40,12 +38,11 @@ func ValidateCmd(ctx Context) (int, error) {
} }
if validColor, ok := i.(*lifx.HSBKColor); ok { if validColor, ok := i.(*lifx.HSBKColor); ok {
fmt.Fprintln(&b, validColor) fmt.Print(validColor)
} else { } else {
return ExitFailure, errors.New("go type %T but wanted *HSBKColor") return ExitFailure, errors.New("go type %T but wanted *HSBKColor")
} }
fmt.Println()
fmt.Print(b.String())
return ExitSuccess, nil return ExitSuccess, nil
} }

2
go.mod
View File

@ -3,7 +3,7 @@ module git.kill0.net/chill9/lume
go 1.15 go 1.15
require ( require (
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73 git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/fatih/color v1.10.0 github.com/fatih/color v1.10.0
github.com/mattn/go-runewidth v0.0.10 // indirect github.com/mattn/go-runewidth v0.0.10 // indirect

2
go.sum
View File

@ -4,8 +4,6 @@ git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621 h1:koWq2W08Hjmvs
git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY= git.kill0.net/chill9/lifx-go v0.0.0-20210323044657-dbe1c40e1621/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447 h1:tN+zR5aszesrZRrhS3uOqAIWLcADCIH7GFJ6SOQS9r0= git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447 h1:tN+zR5aszesrZRrhS3uOqAIWLcADCIH7GFJ6SOQS9r0=
git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY= git.kill0.net/chill9/lifx-go v0.0.0-20210329222320-2107a0586447/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73 h1:fteCAelwAcfam2Q8eeJFyK4+sXGOpR6Me5YMKBi+MYY=
git.kill0.net/chill9/lifx-go v0.0.0-20210418161634-4c1678b62c73/go.mod h1:jInpjEqTBhrFpQKk7zPIWISvgjjfS2djXeKB3yB/8dY=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=