diff --git a/cmd/ls.go b/cmd/ls.go index ec71259..c071188 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -24,6 +24,8 @@ func NewCmdLs() Command { } func LsCmd(args CmdArgs) (int, error) { + var p Printer + c := args.Client selector := args.Flags.String("selector") format := args.Flags.String("format") @@ -37,12 +39,8 @@ func LsCmd(args CmdArgs) (int, error) { return ExitFailure, err } - switch format { - case "table": - PrintLightsTable(lights) - default: - PrintLights(lights) - } + p = NewPrinter(format) + p.Lights(lights) return ExitSuccess, nil } diff --git a/cmd/poweroff.go b/cmd/poweroff.go index 14667c4..c094b33 100644 --- a/cmd/poweroff.go +++ b/cmd/poweroff.go @@ -29,6 +29,8 @@ func NewCmdPoweroff() Command { } func PoweroffCmd(args CmdArgs) (int, error) { + var p Printer + c := args.Client duration := args.Flags.Float64("duration") selector := args.Flags.String("selector") @@ -44,12 +46,8 @@ func PoweroffCmd(args CmdArgs) (int, error) { return ExitFailure, err } - switch format { - case "table": - PrintResultsTable(r.Results) - default: - PrintResults(r.Results) - } + p = NewPrinter(format) + p.Results(r.Results) return ExitSuccess, nil } diff --git a/cmd/poweron.go b/cmd/poweron.go index 48dc886..c9054b0 100644 --- a/cmd/poweron.go +++ b/cmd/poweron.go @@ -29,6 +29,8 @@ func NewCmdPoweron() Command { } func PoweronCmd(args CmdArgs) (int, error) { + var p Printer + c := args.Client duration := args.Flags.Float64("duration") selector := args.Flags.String("selector") @@ -44,12 +46,8 @@ func PoweronCmd(args CmdArgs) (int, error) { return ExitFailure, err } - switch format { - case "table": - PrintResultsTable(r.Results) - default: - PrintResults(r.Results) - } + p = NewPrinter(format) + p.Results(r.Results) return ExitSuccess, nil } diff --git a/cmd/print.go b/cmd/print.go index 27de733..d590116 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -10,6 +10,98 @@ import ( "github.com/olekukonko/tablewriter" ) +type Printer interface { + Results(results []lifx.Result) + Lights(lights []lifx.Light) +} + +type defaultPrinter struct{} + +type tablePrinter struct{} + +func NewPrinter(format string) Printer { + switch format { + case "table": + return &tablePrinter{} + default: + return &defaultPrinter{} + } +} + +func (dp *defaultPrinter) Results(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 (tp *tablePrinter) Results(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() +} + +func (dp *defaultPrinter) Lights(lights []lifx.Light) { + sortLights(lights) + + table := tablewriter.NewWriter(os.Stdout) + _, rows := makeLightsTable(lights) + + for _, v := range rows { + table.Append(v) + } + + 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 (tp *tablePrinter) Lights(lights []lifx.Light) { + sortLights(lights) + + table := tablewriter.NewWriter(os.Stdout) + hdr, rows := makeLightsTable(lights) + + for _, v := range rows { + table.Append(v) + } + + table.SetHeader(hdr) + table.Render() +} + func ColorizePower(s string) string { c := color.New(color.FgRed) if s == "on" { @@ -69,77 +161,3 @@ func makeResultsTable(results []lifx.Result) (hdr []string, rows [][]string) { return } - -func PrintLights(lights []lifx.Light) { - sortLights(lights) - - table := tablewriter.NewWriter(os.Stdout) - _, rows := makeLightsTable(lights) - - for _, v := range rows { - table.Append(v) - } - - 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) { - sortLights(lights) - - table := tablewriter.NewWriter(os.Stdout) - hdr, rows := makeLightsTable(lights) - - for _, v := range rows { - table.Append(v) - } - - 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() -} diff --git a/cmd/setcolor.go b/cmd/setcolor.go index 0921162..47a5149 100644 --- a/cmd/setcolor.go +++ b/cmd/setcolor.go @@ -51,6 +51,8 @@ func NewCmdSetColor() Command { } func SetColorCmd(args CmdArgs) (int, error) { + var p Printer + c := args.Client state := lifx.State{} selector := args.Flags.String("selector") @@ -126,12 +128,8 @@ func SetColorCmd(args CmdArgs) (int, error) { } if !fast { - switch format { - case "table": - PrintResultsTable(r.Results) - default: - PrintResults(r.Results) - } + p = NewPrinter(format) + p.Results(r.Results) } return ExitSuccess, nil diff --git a/cmd/setstate.go b/cmd/setstate.go index c4e7bfb..1e044db 100644 --- a/cmd/setstate.go +++ b/cmd/setstate.go @@ -44,6 +44,8 @@ func NewCmdSetState() Command { } func SetStateCmd(args CmdArgs) (int, error) { + var p Printer + c := args.Client state := lifx.State{} selector := args.Flags.String("selector") @@ -92,12 +94,8 @@ func SetStateCmd(args CmdArgs) (int, error) { } if !fast { - switch format { - case "table": - PrintResultsTable(r.Results) - default: - PrintResults(r.Results) - } + p = NewPrinter(format) + p.Results(r.Results) } return ExitSuccess, nil diff --git a/cmd/setwhite.go b/cmd/setwhite.go index 0953ac2..73870b4 100644 --- a/cmd/setwhite.go +++ b/cmd/setwhite.go @@ -47,6 +47,8 @@ func NewCmdSetWhite() Command { } func SetWhiteCmd(args CmdArgs) (int, error) { + var p Printer + c := args.Client state := lifx.State{} selector := args.Flags.String("selector") @@ -110,12 +112,8 @@ func SetWhiteCmd(args CmdArgs) (int, error) { } if !fast { - switch format { - case "table": - PrintResultsTable(r.Results) - default: - PrintResults(r.Results) - } + p = NewPrinter(format) + p.Results(r.Results) } return ExitSuccess, nil diff --git a/cmd/toggle.go b/cmd/toggle.go index 1c79189..af24e68 100644 --- a/cmd/toggle.go +++ b/cmd/toggle.go @@ -27,6 +27,8 @@ func NewCmdToggle() Command { } func ToggleCmd(args CmdArgs) (int, error) { + var p Printer + c := args.Client duration := args.Flags.Float64("duration") selector := args.Flags.String("selector") @@ -41,12 +43,8 @@ func ToggleCmd(args CmdArgs) (int, error) { return ExitFailure, err } - switch format { - case "table": - PrintResultsTable(r.Results) - default: - PrintResults(r.Results) - } + p = NewPrinter(format) + p.Results(r.Results) return ExitSuccess, nil }