Skip to content

Commit

Permalink
feat(printer): add wide output (#2500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax authored Sep 14, 2022
1 parent 9681ee8 commit d2eb4a8
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 6 deletions.
17 changes: 17 additions & 0 deletions cmd/scw/testdata/test-all-usage-help-output-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ You can select the columns that you want to print with commands that return a li
NAME PUBLIC IP
scw-cool-franklin 51.15.251.251

Wide output (Human without column shrinking)

scw instance server list -o wide

ID NAME TYPE STATE ZONE PUBLIC IP
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251

Wide with column selection

You can select the columns that you want to print with commands that return a list

scw instance server list -o wide=Name,PublicIP

NAME PUBLIC IP
scw-cool-franklin 51.15.251.251


Standard JSON output

scw config dump -o json
Expand Down
34 changes: 34 additions & 0 deletions docs/commands/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ You can select the columns that you want to print with commands that return a li
NAME PUBLIC IP
scw-cool-franklin 51.15.251.251

Wide output (Human without column shrinking)

scw instance server list -o wide

ID NAME TYPE STATE ZONE PUBLIC IP
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251

Wide with column selection

You can select the columns that you want to print with commands that return a list

scw instance server list -o wide=Name,PublicIP

NAME PUBLIC IP
scw-cool-franklin 51.15.251.251


Standard JSON output

scw config dump -o json
Expand Down Expand Up @@ -162,6 +179,23 @@ You can select the columns that you want to print with commands that return a li
NAME PUBLIC IP
scw-cool-franklin 51.15.251.251

Wide output (Human without column shrinking)

scw instance server list -o wide

ID NAME TYPE STATE ZONE PUBLIC IP
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251

Wide with column selection

You can select the columns that you want to print with commands that return a list

scw instance server list -o wide=Name,PublicIP

NAME PUBLIC IP
scw-cool-franklin 51.15.251.251


Standard JSON output

scw config dump -o json
Expand Down
26 changes: 25 additions & 1 deletion internal/core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"strings"
"text/template"

"gopkg.in/yaml.v3"

"github.com/scaleway/scaleway-cli/v2/internal/gofields"
"github.com/scaleway/scaleway-cli/v2/internal/human"
"gopkg.in/yaml.v3"
)

// Type defines an formatter format.
Expand All @@ -30,6 +31,9 @@ const (
// PrinterTypeHuman defines a human readable formatted formatter.
PrinterTypeHuman = PrinterType("human")

// PrinterTypeWide defines a human-readable formatted formatter without shrinking.
PrinterTypeWide = PrinterType("wide")

// PrinterTypeTemplate defines a go template to use to format output.
PrinterTypeTemplate = PrinterType("template")

Expand Down Expand Up @@ -62,6 +66,8 @@ func NewPrinter(config *PrinterConfig) (*Printer, error) {
switch printerName {
case PrinterTypeHuman.String():
setupHumanPrinter(printer, printerOpt)
case PrinterTypeWide.String():
setupWidePrinter(printer, printerOpt)
case PrinterTypeJSON.String():
err := setupJSONPrinter(printer, printerOpt)
if err != nil {
Expand Down Expand Up @@ -120,6 +126,11 @@ func setupHumanPrinter(printer *Printer, opts string) {
}
}

func setupWidePrinter(printer *Printer, opts string) {
setupHumanPrinter(printer, opts)
printer.printerType = PrinterTypeWide
}

type Printer struct {
printerType PrinterType
stdout io.Writer
Expand All @@ -146,6 +157,8 @@ func (p *Printer) Print(data interface{}, opt *human.MarshalOpt) error {
switch p.printerType {
case PrinterTypeHuman:
err = p.printHuman(data, opt)
case PrinterTypeWide:
err = p.printWide(data, opt)
case PrinterTypeJSON:
err = p.printJSON(data)
case PrinterTypeYAML:
Expand Down Expand Up @@ -212,6 +225,17 @@ func (p *Printer) printHuman(data interface{}, opt *human.MarshalOpt) error {
return err
}

func (p *Printer) printWide(data interface{}, opt *human.MarshalOpt) error {
if opt != nil {
opt.DisableShrinking = true
} else {
opt = &human.MarshalOpt{
DisableShrinking: true,
}
}
return p.printHuman(data, opt)
}

func (p *Printer) printJSON(data interface{}) error {
_, implementMarshaler := data.(json.Marshaler)
err, isError := data.(error)
Expand Down
14 changes: 9 additions & 5 deletions internal/human/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"strconv"
"strings"

"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/gofields"
"github.com/scaleway/scaleway-cli/v2/internal/tabwriter"
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
"github.com/scaleway/scaleway-sdk-go/logger"
"github.com/scaleway/scaleway-sdk-go/strcase"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

// Padding between column
Expand Down Expand Up @@ -325,7 +326,7 @@ func marshalSlice(slice reflect.Value, opt *MarshalOpt) (string, error) {
}
grid = append(grid, row)
}
return formatGrid(grid)
return formatGrid(grid, !opt.DisableShrinking)
}

// marshalInlineSlice transforms nested scalar slices in an inline string representation
Expand Down Expand Up @@ -379,12 +380,15 @@ func marshalSection(section *MarshalSection, value reflect.Value, opt *MarshalOp
return Marshal(field, &subOpt)
}

func formatGrid(grid [][]string) (string, error) {
func formatGrid(grid [][]string, shrinkColumns bool) (string, error) {
buffer := bytes.Buffer{}
maxCols := computeMaxCols(grid)
w := tabwriter.NewWriter(&buffer, 5, 1, colPadding, ' ', tabwriter.ANSIGraphicsRendition)
for _, line := range grid {
fmt.Fprintln(w, strings.Join(line[:maxCols], "\t"))
if shrinkColumns {
line = line[:maxCols]
}
fmt.Fprintln(w, strings.Join(line, "\t"))
}
w.Flush()
return strings.TrimSpace(buffer.String()), nil
Expand Down
3 changes: 3 additions & 0 deletions internal/human/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type MarshalOpt struct {

// Is set to true if we are marshaling a table cell
TableCell bool

// DisableShrinking will disable columns shrinking based on terminal size
DisableShrinking bool
}

type MarshalFieldOpt struct {
Expand Down
17 changes: 17 additions & 0 deletions internal/namespaces/help/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ You can select the columns that you want to print with commands that return a li
NAME PUBLIC IP
scw-cool-franklin 51.15.251.251
Wide output (Human without column shrinking)
scw instance server list -o wide
ID NAME TYPE STATE ZONE PUBLIC IP
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251
Wide with column selection
You can select the columns that you want to print with commands that return a list
scw instance server list -o wide=Name,PublicIP
NAME PUBLIC IP
scw-cool-franklin 51.15.251.251
Standard JSON output
scw config dump -o json
Expand Down

0 comments on commit d2eb4a8

Please sign in to comment.