From d2eb4a8ef698d32a2ee61b06ee66d911bd9d878c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jules=20Cast=C3=A9ran?= Date: Wed, 14 Sep 2022 16:10:34 +0200 Subject: [PATCH] feat(printer): add wide output (#2500) --- .../test-all-usage-help-output-usage.golden | 17 ++++++++++ docs/commands/help.md | 34 +++++++++++++++++++ internal/core/printer.go | 26 +++++++++++++- internal/human/marshal.go | 14 +++++--- internal/human/specs.go | 3 ++ internal/namespaces/help/output.go | 17 ++++++++++ 6 files changed, 105 insertions(+), 6 deletions(-) diff --git a/cmd/scw/testdata/test-all-usage-help-output-usage.golden b/cmd/scw/testdata/test-all-usage-help-output-usage.golden index 15ca093063..986ffb687a 100644 --- a/cmd/scw/testdata/test-all-usage-help-output-usage.golden +++ b/cmd/scw/testdata/test-all-usage-help-output-usage.golden @@ -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 diff --git a/docs/commands/help.md b/docs/commands/help.md index ff33168e63..310efefcc1 100644 --- a/docs/commands/help.md +++ b/docs/commands/help.md @@ -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 @@ -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 diff --git a/internal/core/printer.go b/internal/core/printer.go index 35e329ff4b..33ba38adb2 100644 --- a/internal/core/printer.go +++ b/internal/core/printer.go @@ -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. @@ -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") @@ -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 { @@ -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 @@ -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: @@ -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) diff --git a/internal/human/marshal.go b/internal/human/marshal.go index 79394982bc..02c6358630 100644 --- a/internal/human/marshal.go +++ b/internal/human/marshal.go @@ -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 @@ -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 @@ -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 diff --git a/internal/human/specs.go b/internal/human/specs.go index ee640e2504..d9c506d834 100644 --- a/internal/human/specs.go +++ b/internal/human/specs.go @@ -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 { diff --git a/internal/namespaces/help/output.go b/internal/namespaces/help/output.go index 877579030b..4e76122787 100644 --- a/internal/namespaces/help/output.go +++ b/internal/namespaces/help/output.go @@ -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