Skip to content

Commit 0a20759

Browse files
Configure table titles (#314)
* Update tables * Configure colors in the less pager * Fix title wrapping, add titles to lb * Re-add -w argument and add titles to mongodb and pgflex options
1 parent 442d19c commit 0a20759

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

internal/cmd/load-balancer/describe/describe.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ func renderLoadBalancer(loadBalancer *loadbalancer.LoadBalancer) string {
162162
}
163163

164164
table := tables.NewTable()
165+
table.SetTitle("Load Balancer")
165166
table.AddRow("NAME", *loadBalancer.Name)
166167
table.AddSeparator()
167168
table.AddRow("STATE", *loadBalancer.Status)
@@ -182,7 +183,8 @@ func renderLoadBalancer(loadBalancer *loadbalancer.LoadBalancer) string {
182183

183184
func renderListeners(listeners []loadbalancer.Listener) string {
184185
table := tables.NewTable()
185-
table.SetHeader("LISTENER NAME", "PORT", "PROTOCOL", "TARGET POOL")
186+
table.SetTitle("Listeners")
187+
table.SetHeader("NAME", "PORT", "PROTOCOL", "TARGET POOL")
186188
for i := range listeners {
187189
listener := listeners[i]
188190
table.AddRow(*listener.Name, *listener.Port, *listener.Protocol, *listener.TargetPool)
@@ -192,7 +194,8 @@ func renderListeners(listeners []loadbalancer.Listener) string {
192194

193195
func renderTargetPools(targetPools []loadbalancer.TargetPool) string {
194196
table := tables.NewTable()
195-
table.SetHeader("TARGET POOL NAME", "PORT", "TARGETS")
197+
table.SetTitle("Target Pools")
198+
table.SetHeader("NAME", "PORT", "TARGETS")
196199
for _, targetPool := range targetPools {
197200
table.AddRow(*targetPool.Name, *targetPool.TargetPort, len(*targetPool.Targets))
198201
}

internal/cmd/mongodbflex/options/options.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func renderFlavors(flavors []mongodbflex.HandlersInfraFlavor) string {
222222
}
223223

224224
table := tables.NewTable()
225+
table.SetTitle("Flavors")
225226
table.SetHeader("ID", "CPU", "MEMORY", "DESCRIPTION", "VALID INSTANCE TYPES")
226227
for i := range flavors {
227228
f := flavors[i]
@@ -236,6 +237,7 @@ func renderVersions(versions []string) string {
236237
}
237238

238239
table := tables.NewTable()
240+
table.SetTitle("Versions")
239241
table.SetHeader("VERSION")
240242
for i := range versions {
241243
v := versions[i]
@@ -251,7 +253,8 @@ func renderStorages(resp *mongodbflex.ListStoragesResponse) string {
251253
storageClasses := *resp.StorageClasses
252254

253255
table := tables.NewTable()
254-
table.SetHeader("MIN STORAGE", "MAX STORAGE", "STORAGE CLASS")
256+
table.SetTitle("Storages")
257+
table.SetHeader("MINIMUM", "MAXIMUM", "STORAGE CLASS")
255258
for i := range storageClasses {
256259
sc := storageClasses[i]
257260
table.AddRow(*resp.StorageRange.Min, *resp.StorageRange.Max, sc)

internal/cmd/postgresflex/options/options.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ func renderFlavors(flavors []postgresflex.Flavor) string {
222222
}
223223

224224
table := tables.NewTable()
225+
table.SetTitle("Flavors")
225226
table.SetHeader("ID", "CPU", "MEMORY", "DESCRIPTION")
226227
for i := range flavors {
227228
f := flavors[i]
@@ -236,6 +237,7 @@ func renderVersions(versions []string) string {
236237
}
237238

238239
table := tables.NewTable()
240+
table.SetTitle("Versions")
239241
table.SetHeader("VERSION")
240242
for i := range versions {
241243
v := versions[i]
@@ -251,7 +253,8 @@ func renderStorages(resp *postgresflex.ListStoragesResponse) string {
251253
storageClasses := *resp.StorageClasses
252254

253255
table := tables.NewTable()
254-
table.SetHeader("MIN STORAGE", "MAX STORAGE", "STORAGE CLASS")
256+
table.SetTitle("Storages")
257+
table.SetHeader("MINIMUM", "MAXIMUM", "STORAGE CLASS")
255258
for i := range storageClasses {
256259
sc := storageClasses[i]
257260
table.AddRow(*resp.StorageRange.Min, *resp.StorageRange.Max, sc)

internal/cmd/ske/options/options.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ func renderAvailabilityZones(resp *ske.ProviderOptions) string {
181181
zones := *resp.AvailabilityZones
182182

183183
table := tables.NewTable()
184-
table.SetHeader("AVAILABILITY ZONES")
184+
table.SetTitle("Availability Zones")
185+
table.SetHeader("ZONE")
185186
for i := range zones {
186187
z := zones[i]
187188
table.AddRow(*z.Name)
@@ -193,7 +194,8 @@ func renderKubernetesVersions(resp *ske.ProviderOptions) (string, error) {
193194
versions := *resp.KubernetesVersions
194195

195196
table := tables.NewTable()
196-
table.SetHeader("KUBERNETES VERSION", "STATE", "EXPIRATION DATE", "FEATURE GATES")
197+
table.SetTitle("Kubernetes Versions")
198+
table.SetHeader("VERSION", "STATE", "EXPIRATION DATE", "FEATURE GATES")
197199
for i := range versions {
198200
v := versions[i]
199201
featureGate, err := json.Marshal(*v.FeatureGates)
@@ -213,7 +215,8 @@ func renderMachineImages(resp *ske.ProviderOptions) string {
213215
images := *resp.MachineImages
214216

215217
table := tables.NewTable()
216-
table.SetHeader("MACHINE IMAGE NAME", "VERSION", "STATE", "EXPIRATION DATE", "SUPPORTED CRI")
218+
table.SetTitle("Machine Images")
219+
table.SetHeader("NAME", "VERSION", "STATE", "EXPIRATION DATE", "SUPPORTED CRI")
217220
for i := range images {
218221
image := images[i]
219222
versions := *image.Versions
@@ -241,7 +244,8 @@ func renderMachineTypes(resp *ske.ProviderOptions) string {
241244
types := *resp.MachineTypes
242245

243246
table := tables.NewTable()
244-
table.SetHeader("MACHINE TYPE", "CPU", "MEMORY")
247+
table.SetTitle("Machine Types")
248+
table.SetHeader("TYPE", "CPU", "MEMORY")
245249
for i := range types {
246250
t := types[i]
247251
table.AddRow(*t.Name, *t.Cpu, *t.Memory)
@@ -253,7 +257,8 @@ func renderVolumeTypes(resp *ske.ProviderOptions) string {
253257
types := *resp.VolumeTypes
254258

255259
table := tables.NewTable()
256-
table.SetHeader("VOLUME TYPE")
260+
table.SetTitle("Volume Types")
261+
table.SetHeader("TYPE")
257262
for i := range types {
258263
z := types[i]
259264
table.AddRow(*z.Name)

internal/pkg/print/print.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ func (p *Printer) PagerDisplay(content string) error {
171171
if outputFormat == NoneOutputFormat {
172172
return nil
173173
}
174-
pagerCmd := exec.Command("less", "-F", "-S", "-w")
174+
175+
// less arguments
176+
// -F: exits if the entire file fits on the first screen
177+
// -S: disables line wrapping
178+
// -w: highlight the first line after moving one full page down
179+
// -R: interprets ANSI color and style sequences
180+
pagerCmd := exec.Command("less", "-F", "-S", "-w", "-R")
175181

176182
pagerCmd.Stdin = strings.NewReader(content)
177183
pagerCmd.Stdout = p.Cmd.OutOrStdout()

internal/pkg/tables/tables.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
77

88
"github.com/jedib0t/go-pretty/v6/table"
9+
"github.com/jedib0t/go-pretty/v6/text"
910
)
1011

1112
type Table struct {
@@ -20,6 +21,20 @@ func NewTable() Table {
2021
}
2122
}
2223

24+
// Sets the title of the table
25+
func (t *Table) SetTitle(title string) {
26+
t.table.SetTitle(title)
27+
28+
// prevent title wrapping by setting the width of the first column to the length of the title
29+
// this is a workaround for a bug in the tables pkg, see https://github.com/jedib0t/go-pretty/issues/135
30+
t.table.SetColumnConfigs([]table.ColumnConfig{
31+
{
32+
Number: 1,
33+
WidthMin: len(title),
34+
},
35+
})
36+
}
37+
2338
// Sets the header of the table
2439
func (t *Table) SetHeader(header ...interface{}) {
2540
t.table.AppendHeader(table.Row(header))
@@ -47,6 +62,10 @@ func (t *Table) EnableAutoMergeOnColumns(columns ...int) {
4762
// Returns the table rendered
4863
func (t *Table) Render() string {
4964
t.table.SetStyle(table.StyleLight)
65+
66+
t.table.Style().Title = table.TitleOptionsBlackOnCyan
67+
t.table.Style().Title.Align = text.AlignCenter
68+
5069
t.table.Style().Options.DrawBorder = false
5170
t.table.Style().Options.SeparateRows = false
5271
t.table.Style().Options.SeparateColumns = true

0 commit comments

Comments
 (0)