Skip to content

Commit d2eb4a8

Browse files
authored
feat(printer): add wide output (#2500)
1 parent 9681ee8 commit d2eb4a8

6 files changed

Lines changed: 105 additions & 6 deletions

File tree

cmd/scw/testdata/test-all-usage-help-output-usage.golden

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ You can select the columns that you want to print with commands that return a li
1818
NAME PUBLIC IP
1919
scw-cool-franklin 51.15.251.251
2020

21+
Wide output (Human without column shrinking)
22+
23+
scw instance server list -o wide
24+
25+
ID NAME TYPE STATE ZONE PUBLIC IP
26+
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251
27+
28+
Wide with column selection
29+
30+
You can select the columns that you want to print with commands that return a list
31+
32+
scw instance server list -o wide=Name,PublicIP
33+
34+
NAME PUBLIC IP
35+
scw-cool-franklin 51.15.251.251
36+
37+
2138
Standard JSON output
2239

2340
scw config dump -o json

docs/commands/help.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ You can select the columns that you want to print with commands that return a li
102102
NAME PUBLIC IP
103103
scw-cool-franklin 51.15.251.251
104104

105+
Wide output (Human without column shrinking)
106+
107+
scw instance server list -o wide
108+
109+
ID NAME TYPE STATE ZONE PUBLIC IP
110+
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251
111+
112+
Wide with column selection
113+
114+
You can select the columns that you want to print with commands that return a list
115+
116+
scw instance server list -o wide=Name,PublicIP
117+
118+
NAME PUBLIC IP
119+
scw-cool-franklin 51.15.251.251
120+
121+
105122
Standard JSON output
106123

107124
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
162179
NAME PUBLIC IP
163180
scw-cool-franklin 51.15.251.251
164181

182+
Wide output (Human without column shrinking)
183+
184+
scw instance server list -o wide
185+
186+
ID NAME TYPE STATE ZONE PUBLIC IP
187+
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251
188+
189+
Wide with column selection
190+
191+
You can select the columns that you want to print with commands that return a list
192+
193+
scw instance server list -o wide=Name,PublicIP
194+
195+
NAME PUBLIC IP
196+
scw-cool-franklin 51.15.251.251
197+
198+
165199
Standard JSON output
166200

167201
scw config dump -o json

internal/core/printer.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"strings"
99
"text/template"
1010

11+
"gopkg.in/yaml.v3"
12+
1113
"github.com/scaleway/scaleway-cli/v2/internal/gofields"
1214
"github.com/scaleway/scaleway-cli/v2/internal/human"
13-
"gopkg.in/yaml.v3"
1415
)
1516

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

34+
// PrinterTypeWide defines a human-readable formatted formatter without shrinking.
35+
PrinterTypeWide = PrinterType("wide")
36+
3337
// PrinterTypeTemplate defines a go template to use to format output.
3438
PrinterTypeTemplate = PrinterType("template")
3539

@@ -62,6 +66,8 @@ func NewPrinter(config *PrinterConfig) (*Printer, error) {
6266
switch printerName {
6367
case PrinterTypeHuman.String():
6468
setupHumanPrinter(printer, printerOpt)
69+
case PrinterTypeWide.String():
70+
setupWidePrinter(printer, printerOpt)
6571
case PrinterTypeJSON.String():
6672
err := setupJSONPrinter(printer, printerOpt)
6773
if err != nil {
@@ -120,6 +126,11 @@ func setupHumanPrinter(printer *Printer, opts string) {
120126
}
121127
}
122128

129+
func setupWidePrinter(printer *Printer, opts string) {
130+
setupHumanPrinter(printer, opts)
131+
printer.printerType = PrinterTypeWide
132+
}
133+
123134
type Printer struct {
124135
printerType PrinterType
125136
stdout io.Writer
@@ -146,6 +157,8 @@ func (p *Printer) Print(data interface{}, opt *human.MarshalOpt) error {
146157
switch p.printerType {
147158
case PrinterTypeHuman:
148159
err = p.printHuman(data, opt)
160+
case PrinterTypeWide:
161+
err = p.printWide(data, opt)
149162
case PrinterTypeJSON:
150163
err = p.printJSON(data)
151164
case PrinterTypeYAML:
@@ -212,6 +225,17 @@ func (p *Printer) printHuman(data interface{}, opt *human.MarshalOpt) error {
212225
return err
213226
}
214227

228+
func (p *Printer) printWide(data interface{}, opt *human.MarshalOpt) error {
229+
if opt != nil {
230+
opt.DisableShrinking = true
231+
} else {
232+
opt = &human.MarshalOpt{
233+
DisableShrinking: true,
234+
}
235+
}
236+
return p.printHuman(data, opt)
237+
}
238+
215239
func (p *Printer) printJSON(data interface{}) error {
216240
_, implementMarshaler := data.(json.Marshaler)
217241
err, isError := data.(error)

internal/human/marshal.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"strconv"
99
"strings"
1010

11+
"golang.org/x/text/cases"
12+
"golang.org/x/text/language"
13+
1114
"github.com/fatih/color"
1215
"github.com/scaleway/scaleway-cli/v2/internal/gofields"
1316
"github.com/scaleway/scaleway-cli/v2/internal/tabwriter"
1417
"github.com/scaleway/scaleway-cli/v2/internal/terminal"
1518
"github.com/scaleway/scaleway-sdk-go/logger"
1619
"github.com/scaleway/scaleway-sdk-go/strcase"
17-
"golang.org/x/text/cases"
18-
"golang.org/x/text/language"
1920
)
2021

2122
// Padding between column
@@ -325,7 +326,7 @@ func marshalSlice(slice reflect.Value, opt *MarshalOpt) (string, error) {
325326
}
326327
grid = append(grid, row)
327328
}
328-
return formatGrid(grid)
329+
return formatGrid(grid, !opt.DisableShrinking)
329330
}
330331

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

382-
func formatGrid(grid [][]string) (string, error) {
383+
func formatGrid(grid [][]string, shrinkColumns bool) (string, error) {
383384
buffer := bytes.Buffer{}
384385
maxCols := computeMaxCols(grid)
385386
w := tabwriter.NewWriter(&buffer, 5, 1, colPadding, ' ', tabwriter.ANSIGraphicsRendition)
386387
for _, line := range grid {
387-
fmt.Fprintln(w, strings.Join(line[:maxCols], "\t"))
388+
if shrinkColumns {
389+
line = line[:maxCols]
390+
}
391+
fmt.Fprintln(w, strings.Join(line, "\t"))
388392
}
389393
w.Flush()
390394
return strings.TrimSpace(buffer.String()), nil

internal/human/specs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type MarshalOpt struct {
1414

1515
// Is set to true if we are marshaling a table cell
1616
TableCell bool
17+
18+
// DisableShrinking will disable columns shrinking based on terminal size
19+
DisableShrinking bool
1720
}
1821

1922
type MarshalFieldOpt struct {

internal/namespaces/help/output.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ You can select the columns that you want to print with commands that return a li
2020
NAME PUBLIC IP
2121
scw-cool-franklin 51.15.251.251
2222
23+
Wide output (Human without column shrinking)
24+
25+
scw instance server list -o wide
26+
27+
ID NAME TYPE STATE ZONE PUBLIC IP
28+
088b01da-9ba7-40d2-bc55-eb3170f42185 scw-cool-franklin DEV1-S running fr-par-1 51.15.251.251
29+
30+
Wide with column selection
31+
32+
You can select the columns that you want to print with commands that return a list
33+
34+
scw instance server list -o wide=Name,PublicIP
35+
36+
NAME PUBLIC IP
37+
scw-cool-franklin 51.15.251.251
38+
39+
2340
Standard JSON output
2441
2542
scw config dump -o json

0 commit comments

Comments
 (0)